Effacer les filtres
Effacer les filtres

function within nested loop

1 vue (au cours des 30 derniers jours)
Aamna Alshehhi
Aamna Alshehhi le 17 Oct 2019
Commenté : Mil Shastri le 17 Oct 2019
I got this code to find all Th3 and Th4 solutions using function command and for loop as it's repeated process. I got many errors saying
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in Aamna_fun1 (line 5)
F(1) = real(b*exp(1i*Th3)-c*exp(1i*Th4)+r*Th4-Z);
Error in fsolve (line 242)
fuser = feval(funfcn{3},x,varargin{:});
Error in test (line 18)
Sol = fsolve(@Aamna_fun1,x0);
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
could anyone help me to figure out the problem and fix it ,please.
function F = Aamna_fun1(x) %function to be solved
Th3 = x(1); %first solution
Th4 = x(2); %second solution
global b c r Z % constant inputs
F(1) = real(b*exp(1i*Th3)-c*exp(1i*Th4)+r*Th4-Z); %real part of the eqution for the unknowns
F(2) = imag(b*exp(1i*Th3)-c*exp(1i*Th4)+r*Th4-Z) %imeginery part of the equation for the unknowns
Th2= 90 : (360+90); %Th2 input range
for e= 2:length(Th2); %for loop for Th2
global b c r Z
a=2;
b=8;
c=2;
r=4;
d0=6.92;
ap=6;
Th1=0;
Th5=90;
Z = d0 +r*pi/2+r*1i-a*exp(1i*Th2); %equation with known variables
x0 = [0 0]; % guess for Th3 and Th4
Sol = fsolve(@Aamna_fun1,x0); % solve the above equation
Th3s = rad2deg(Sol(1)) %first solution for Th3
Th4s = rad2deg(Sol(2)) %first solution for Th4
for x0 = [Th3s(e-1) Th4s(e-1)] %repeat the above process but with soultions instead of guessing 0 and 0
Th3s(:,e+1) = [rad2deg(Sol(1))] %create an array with all Th3 solutions
Th4s(:,e+1) = [rad2deg(Sol(2))] %create an array with all Th4 solutions
end
end
Th2 = 90:360+90; %th2 range as above
figure(1) %plot Th2 vs Th3
plot (Th2,Th3(1,:));
xlabel ('TH2')
ylabel ('TH3')
figure(2) %plot Th2 vs Th4
plot (Th2,Th4(1,:));
xlabel ('TH2') ;
ylabel ('TH4') ;
  2 commentaires
Adam
Adam le 17 Oct 2019
For starters I would rework the code to get rid of
global b c r Z
Whether it causes any of the problems you see here frankly I have no idea, but that is part of their problem, it is very hard to understand exactly what is going on with global variables.
Either way, your comments claim they are constants so define them once, outside the loop, in that case, although Z clearly isn't a constant.
Mil Shastri
Mil Shastri le 17 Oct 2019
Can you provide a sample input to the function. That will help identifying the issue.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by