how to perform fsolve within for loop

1 view (last 30 days)
Aamna Alshehhi
Aamna Alshehhi on 6 Nov 2019
Commented: Walter Roberson on 6 Nov 2019
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
Aamna Alshehhi
Aamna Alshehhi on 6 Nov 2019
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 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.

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB 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!