How to append in a while loop?
14 次查看(过去 30 天)
显示 更早的评论
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).
0 个评论
采纳的回答
David Hill
2022-12-5
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!