how to perform fsolve within for loop

조회 수: 4 (최근 30일)
Aamna Alshehhi
Aamna Alshehhi 2019년 11월 6일
댓글: Walter Roberson 2019년 11월 6일
how I can perform fsolve inside for loop if I have the following function:
function Q = Om2Afun(x)
global L2_2 LA Th2_2 ThA_2 v
OmA = x(1);
Om2 = x(2);
Q(1) = real(L2_2.*exp(1i.*Th2_2)*1i.*Om2 +LA.*exp(1i.*ThA_2).*1i.*OmA -v*exp(1i*ThA_2));
Q(2) = imag(L2_2.*exp(1i.*Th2_2)*1i.*Om2 +LA.*exp(1i.*ThA_2).*1i.*OmA -v*exp(1i*ThA_2));
I wrote fsolve within loop like this but I keep getting errors:
for t=0:10
LA(t+1) = 1.15-(v*t);
Z = L1_2*exp(1i*deg2rad(Th1_2));
Zc = conj(Z);
Ka = -LA.*Zc ; Kb = Z*Zc + LA.^2 -L2_2^2; Kc = -LA.*Z;
R_1 = (-Kb + sqrt(Kb.^2 - 4.*Ka.*Kc))./(2*Ka);
F_1 =(Z-(LA.*R_1))/L2_2;
ThA_1= rad2deg(angle(R_1));
Th2_1= rad2deg(angle(F_1));
global L2_2 LA Th2_2 ThA_2 v
x0= [0 0];
xSol(t+1, :)= fsolve(@Om2Afun , x0);
OmA(t+1)= xSol (1, t+1)
Om2(t+1)= xSol (2,t+1)
end
note that L2_2 and v are constant values and Th2_2 , ThA_2 and LA are changing in every loop iteration. so I wanna find the solution for Om2 and OmA in every instant to make a vector for all values.
  댓글 수: 3
Aamna Alshehhi
Aamna Alshehhi 2019년 11월 6일
I did not get it. how I am supposed to initalize my global variable?
all parameters are already in the worksapce.
actually when I use the function without loop I get the solution but when I added it to the loop I end up with error saying :
"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
Walter Roberson
Walter Roberson 2019년 11월 6일
Where do you assign a value to L2_2 ?
You should get rid of all of the global variables and follow the guidelines about parameterizing functions that I linked to.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by