help with writing an equation with iterations
Afficher commentaires plus anciens
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
Réponses (1)
Mischa Kim
le 8 Mar 2014
Modifié(e) : Mischa Kim
le 8 Mar 2014
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 commentaires
Ken
le 9 Mar 2014
Mischa Kim
le 9 Mar 2014
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?
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
