How to append in a while loop?

5 vues (au cours des 30 derniers jours)
Maram
Maram le 5 Déc 2022
Commenté : Maram le 5 Déc 2022
Hello,
I am trying to run a loop to get the values of lambda in an array, number of rows is unknown, which to be determined when a certian limit is met. For each n there supposed to be 2 values each for W ,c ,lambda ,phi
The goal is to keep lambda values and ph values as they will be used in other steps. I was trying to append
When all added at the end it should be like,
when n =1 , we get:
lambda 1
lambda 2
n=2,
lambda 3
lambda 4
.
.
and so on.
The code I wrote seem to have some issue with the indices, I am fairly new to matlab so I am not sure how to the fix should be. Any help is appreciated.
limit =0.01;
repeat= true;
Ad= (linspace(-2.5,2.5,26))';
b=2;
n=1;
while repeat==true
syms z
W(n)= vpasolve(1/b -z*tan(z*a), z, [(n-1)*(pi/a) (n-(1/2))*(pi/a)]);
c(n)= 1/((a+(sin(W(n)*a)/2*W(n)))^0.5);
lambda(n)= (2*b)/(1+((W(n)^2)*(b^2)));
ph(n)= c(n)*cos(W(n)*Ad(n));
W(n)= vpasolve(((1/b *tan(z*a)) + z), z, [(n-(1/2))*(pi/a) (n)*(pi/a)]);
c(n)= 1/((a-(sin(W(n)*a)/2*W(n)))^0.5);
lambda=[(2*b)/(1+((W(n)^2)*(b^2))),n];
ph= [c(n)*sin(W(n)*Ad(n)),n];
if lambda(n)/lambda(1) < 0.01
repeat= false;
end
n=n+1;
end
I get an error for the if line ,
Index exceeds the number of array elements (2).

Réponse acceptée

David Hill
David Hill le 5 Déc 2022
Where is 'a' assigned? Lots of guessing here, but this might help you.
limit =0.01;
Ad= (linspace(-2.5,2.5,26))';
b=2;a=1;
n=1;
while 1
syms z
W(n,1)= double(vpasolve(1/b -z*tan(z*a), z, [(n-1)*(pi/a) (n-(1/2))*(pi/a)]));
c(n,1)= 1/((a+(sin(W(n,1)*a)/2*W(n,1)))^0.5);
lambda(n,1)= (2*b)/(1+((W(n,1)^2)*(b^2)));
ph(n,1)= c(n,1)*cos(W(n,1)*Ad(n));
W(n,2)= double(vpasolve(((1/b *tan(z*a)) + z), z, [(n-(1/2))*(pi/a) (n)*(pi/a)]));
c(n,2)= 1/((a-(sin(W(n,2)*a)/2*W(n,2)))^0.5);
lambda(n,2)=(2*b)/(1+((W(n,2)^2)*(b^2)));
ph(n,2)= c(n,2)*sin(W(n,2)*Ad(n));
if lambda(n,1)/lambda(1,1) < 0.01
break;
end
n=n+1;
end
  1 commentaire
Maram
Maram le 5 Déc 2022
Sorry I forgot to mention, a=2.5
It's extremely helpful tho, thank you so much for your quick reply!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by