How to plot a solution to a difference equation

1.2k Views Asked by At

I'm trying to plots a graph for a difference equation, so that it plots a graph of X(n) for n=1,2,...,20, here's my code:

X(1)=-2;

for n=1:20;
X(n+1) = 0.5 * X(n) + 2;
end

I tried to store X(1) to X(20) as vector then plot the graph, however, it didn't work...and I am not sure what I've missed...

Thank you.

1

There are 1 best solutions below

0
On BEST ANSWER
x(1) = -2;
for n = 1:19,  %the comma ',' is not necessary
    x(n+1) = 0.5*x(n) + 2;
end

n=1:20;
plot(n,x)

Hope this is helpful and you may try to expand this program.