MATLAB Answers

How to plot x(n) for many values of n?

49 views (last 30 days)
SAS
SAS on 6 Jun 2020
Commented: SAS on 7 Jun 2020
How to plot 𝑥(𝑛) = 𝛼𝑛 𝑥(0) for n=1-30 and x(0)=100?? I have tried this code, but it is repeating it's value again and again... What I am missing?
alpha = 2
x0 = 100; % Initial conditions
for n = 1:30
x(n) = (alpha^n)*(x0)
end
disp(x(n))

  0 Comments

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 6 Jun 2020
Edited: madhan ravi on 6 Jun 2020
Shorter code eliminating loop:
Alpha = 2
x0 = 100; % Initial conditions
n = 1:30;
x = Alpha.^n*x0;
plot(n,x)
Note: You were doing it correctly all you had to use was disp(x)

  9 Comments

Show 6 older comments
SAS
SAS on 6 Jun 2020
Still the same answers, Showing all graphs in one plot...

Sign in to comment.

More Answers (1)

David Hill
David Hill on 6 Jun 2020
alpha = 2
x0 = 100; % Initial conditions
for n = 1:30
x(n) = (alpha^n)*(x0)
end
disp(x);
plot(1:30,x);

  3 Comments

SAS
SAS on 6 Jun 2020
x(n) is returning too many values. Shouldn't it give x(1) to x(30)? only 30 values?
David Hill
David Hill on 6 Jun 2020
x has only 30 values...use ; to suppress
alpha = 2;
x0 = 100; % Initial conditions
for n = 1:30
x(n) = (alpha^n)*(x0);
end
disp(x);
plot(1:30,x);

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!