help with writing an equation with iterations

Hi I am trying created a code with aim of finding theta and then alpha(i). Then plotting alpha(i) against i. It is known that alpha(0)=30.
>alpha= 30; % This is alpha(0) i tried writing alpha(0) but kept getting error
>for i=1:10
>theta= 8*alpha*i+6*alpha %I want 8*alpha(i)*i+6*alpha(i)
>alpha(i+1)= theta +alpha(i) %Want to creating a list of alpha(i) values to plot against i
>end
>plot(i,alpha(i))
Also tried this but with no success produced almost all zero values
>alpha=[ ]
>alpha= [30];
>alpha(1) %this is alpha(0) as it gives value 30
>for i=1:10
>theta= 8*alpha(i)*i+6*alpha(i)
>alpha(i+1)= theta +alpha(i)
>end
>plot(i,alpha(i))
Many thanks in advance

Answers (1)

Ken, you could do:
alpha = 30;
n = 10;
for i = 1:n
theta(i) = 8*alpha(i)*i + 6*alpha(i);
alpha(i+1) = theta(i) + alpha(i);
end
plot(1:n+1,alpha)
however, you probably end up with some huge numbers. Note, that indexing in MATLAB starts at 1, not 0. Also, since you are generating n+1 values for alpha you need to have n+1 data points for i, for plotting.

2 Comments

Hi thanks for the reply
For some reason creates a graph like this when plotting i against alpha
When I test to see what alpha is produces this;
0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0014 0.1143
9.9443
Though when i type in alpha(1) returns 30
That's because of the huge numbers you are computing. Have a look at the y-axis scale factor: 10^17. 30 = 0.0000...00030 * 10^17.
If you remove semi-colons after the two commands in the for-loop the values for theta and alpha are displayed in the MATLAB command window. The first values are:
theta =
420 9900 310500 12192300 575604900
alpha =
30 450 10350 320850 12513150 588118050
The results are correct. So I am wondering, if this is indeed the series you need to compute. What exactly is the problem statement?

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Asked:

Ken
on 8 Mar 2014

Commented:

on 9 Mar 2014

Community Treasure Hunt

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

Start Hunting!