Solving system of equations involving for loop.

조회 수: 4 (최근 30일)
HUST Student
HUST Student 2018년 6월 11일
답변: Jinal 2024년 7월 23일
I'm trying to write a matlab (for loop) code to solve a system of equations eqn1 eqn2 eqn3 (get all solutions for the variables Z Q and F in the predetermined intervals) when parameter A varies in the range of 0 to 10. Then I would like to get the graphs: Z as a function of A; Q as a function of A; F as a function of A. Code that I produced
syms w e d n A Z F Q;
w=pi;
e=0.5;
d=10;
n=1;
eqn1 = Z+A.*w./(1-Q.*(1+e))*cos(F)==0;
eqn2 = cos(F+2.*Q.*n.*pi)+Z.*((e-Q.*(1+e))./A.*w)==0;
eqn3 = A-1/(2.*w.*abs(sin(Q.*n.*pi))).*(((1-e).*Z).^2+(d.*w+2.*(1+e).*(1-Q).*Q.*n.*pi.*Z).^2).^0.5==0;
equations = [eqn1 eqn2 eqn3];
vars = [A Z F Q];
range = [0 10;-40 40;0 6;NaN NaN];
for A=0:0.5:10;
for i=1:5
temp=vpasolve(equations, vars, range, 'random', true);
sol(i,1)=temp.Z;
sol(i,2)=temp.F;
sol(i,3)=temp.Q;
end
figure (1);
subplot(2,2,1)
plot(A,sol(i,1));
xlabel('A');
ylabel('Z');
subplot(2,2,2)
plot(A,sol(i,2));
xlabel('A');
ylabel('F');
subplot(2,2,3)
plot(A,sol(i,3));
xlabel('A');
ylabel('Q');
end
give this type of error:
Error using getEqnsVars (line 50) Expecting two arguments: a vector of equations and a vector of variables.
Error in sym/vpasolve (line 95) [eqns,vars] = getEqnsVars(varargin{1:N});
Error in Sucesso (line 15) temp=vpasolve(equations, vars, range, 'random', true);
I do not know how to solve this error, if someone can help me I would thank you a lot.

답변 (1개)

Jinal
Jinal 2024년 7월 23일
You can refer to the following MATLAB Answer which provides solution to the similar question.

카테고리

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