How to append in a while loop?

조회 수: 9 (최근 30일)
Maram
Maram 2022년 12월 5일
댓글: Maram 2022년 12월 5일
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).

채택된 답변

David Hill
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
  댓글 수: 1
Maram
Maram 2022년 12월 5일
Sorry I forgot to mention, a=2.5
It's extremely helpful tho, thank you so much for your quick reply!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by