how to perform fsolve within for loop
1 view (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 6 Nov 2019
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.
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!