The problem was to write a MATLAB script to solve the
Linear Non-Homogeneous DE:
x(n+1) = a x(n) + w(n)
The programming challenge was how to input w(n) since
it had to be defined for every value of n.
I came up with two different solutions, neither of which is totally
satisfying.
Solution 1 Define w(n) as a function:
w = input('Enter a function for w(n):','s'); % the 's' forces it to be a string
w = inline(w,'n'); % this makes a function out of what was typed
We could also use
w = input('Enter a function for w(n):','s');
.
.
.
x(n+1) = a*x(n) + eval(w); % eval evaluates the expression in w
This form has the flexibility if we somehow wanted w to depend on x(n)
or other values.
Solution 2 Define the values of the vector w:
w = input('Enter a vector of 100 values for w:');
The user would have to type: [1, 0, 0, 23, 2, etc. ] to get all
the values of w in.
I could have also put this in a loop:
for n = 1:100
w(n) = input(['Enter w(',int2str(n),'):']); % int2str converts an
% integer into a string
% so it can be concatenated
% to other characters
end
Alternatively, I thought of only allowing w to change every
10 steps, so that the user only had to enter 10 values.
w = input('Enter 10 values for w:');
.
.
.
x(n+1) = a*x(n) + w(floor(n/10)+1); % floor rounds down to
% the nearest integer