How to solve Multivariate equations

조회 수: 5 (최근 30일)
Laemi
Laemi 2023년 2월 2일
댓글: Laemi 2023년 2월 4일
So I tried to use newton-Raphson method to find 2 variables but I have an error in my code.
%Scripte illustre les valeurs de phi et s
%script Méthode Newton-Raphson (for 2 variables)
clear, clc
disp('NEWTON-RAPHSON METHOD (Simulation 1)')
NEWTON-RAPHSON METHOD (Simulation 1)
disp('')
% f1 une fonction continue f1(s,phi) de deux variables.
% f2 une fonction continue f2(s,phi) de deux variables.
%
%Définir les variables
r = 3; %[cm] longueur de la manivelle
l = 10;%[cm] longueur du maillon flottant
syms s phi theta;
%définir les Fonctions
f1 = r* sin(theta)-l*sin(phi)-((1/3)*cos(s))-((2/3)*(cos(s/44))^2);
f2 = r*cos(theta)-l*cos(phi)-s;
f = [f1,f2];
var = [s phi];
%Définir la matrice jacobienne
J = [diff(f1,'s') diff(f1,'phi')
diff(f2,'s') diff(f2,'phi')];
disp (J)
%valeurs estimées de s et phi
s = 4 ;
phi = 4*pi/3;
%conditions initials
maxIter = 10;
sphi = [s phi];
tol = 0.001;
% Calcul à l'aide de Newton Raphson
for theta = 0:5:2*pi
for i = 1:maxIter
var = var - f*inv(J);
errX = abs(var - sphi);
eqns = [(var(1) == 0) (var(2) == 0)];
disp(eqns)
[s_f, phi_f] = solve(eqns,[s phi]);
disp('')
disp(s_f)
disp(phi_f)
disp(errX)
fprintf('%3.0f' ,i)
fprintf('%11.4f %11.4\n', s_f, phi_f)
sphi = var ;
if(errX(1,1) < tol) && (errX(1,2) < tol) && (errX(2,1) < tol) && (errX(2,2) < tol)
break
end
sphi = var ;
end
end
Error using sym.getEqnsVars>checkVariables
Second argument must be a vector of symbolic variables.

Error in sym.getEqnsVars (line 62)
checkVariables(vars);

Error in sym/solve>getEqns (line 429)
[eqns, vars] = sym.getEqnsVars(argv{:});

Error in sym/solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
%graphique
plot(theta, s, theta, phi);
grid on
xlabel('theta (degre)');
ylabel('valeurs de s (cm) et phi (degre)')
title('Phi et s en fonction de theta');
legend({'y = r','y = phi'},'Location','southwest');
the error say : Error using sym.getEqnsVars>checkVariables (line 92)
Second argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 56)
checkVariables(vars);
Error in sym/solve>getEqns (line 429)
[eqns, vars] = sym.getEqnsVars(argv{:});
Error in sym/solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
Error in test_2 (line 41)
[s_f, phi_f] = solve(eqns,[s phi]);
Can I have an hint how to solve this ?
Thanks in advance

채택된 답변

Torsten
Torsten 2023년 2월 2일
편집: Torsten 2023년 2월 2일
%Scripte illustre les valeurs de phi et s
%script Méthode Newton-Raphson (for 2 variables)
clear, clc
disp('NEWTON-RAPHSON METHOD (Simulation 1)')
NEWTON-RAPHSON METHOD (Simulation 1)
disp('')
% f1 une fonction continue f1(s,phi) de deux variables.
% f2 une fonction continue f2(s,phi) de deux variables.
%
%Définir les variables
r = 3; %[cm] longueur de la manivelle
l = 10;%[cm] longueur du maillon flottant
syms s phi theta
%définir les Fonctions
f1 = r* sin(theta)-l*sin(phi)-((1/3)*cos(s))-((2/3)*(cos(s/44))^2);
f2 = r*cos(theta)-l*cos(phi)-s;
f = [f1;f2];
f = matlabFunction(f,'Vars',[theta s phi])
f = function_handle with value:
@(theta,s,phi)[cos(s).*(-1.0./3.0)-sin(phi).*1.0e+1+sin(theta).*3.0-cos(s./4.4e+1).^2.*(2.0./3.0);-s-cos(phi).*1.0e+1+cos(theta).*3.0]
%Définir la matrice jacobienne
J = [diff(f1,s) diff(f1,phi)
diff(f2,s) diff(f2,phi)];
J = matlabFunction(J,'Vars',[s phi])
J = function_handle with value:
@(s,phi)reshape([sin(s)./3.0+(cos(s./4.4e+1).*sin(s./4.4e+1))./3.3e+1,-1.0,cos(phi).*-1.0e+1,sin(phi).*1.0e+1],[2,2])
Theta = linspace(0,2*pi,50);
%valeurs estimées de s et phi
s = 4 ;
phi = 4*pi/3;
%conditions initials
maxIter = 50;
sphi = [s ; phi];
tolX = 0.001;
tolF = 0.001;
sol = zeros(2,numel(Theta));
for i = 1:numel(Theta)
theta = Theta(i);
% Calcul à l'aide de Newton Raphson
errX = 1.0;
errF = 1.0;
iter = 0;
while iter <= maxIter && (errX > tolX || errF > tolF)
var = sphi - inv(J(sphi(1),sphi(2)))*f(theta,sphi(1),sphi(2));
errX = norm(abs(var - sphi));
errF = norm(f(theta,var(1),var(2)));
sphi = var ;
iter = iter + 1;
end
sol(:,i) = var;
end
plot(Theta,sol)
grid on
xlabel('theta (degre)');
ylabel('valeurs de s (cm) et phi (degre)')
title('Phi et s en fonction de theta');
legend({'y = r','y = phi'},'Location','southwest');
  댓글 수: 3
Torsten
Torsten 2023년 2월 4일
편집: Torsten 2023년 2월 4일
Sorry, but frankly speaking: there are so many mathematical and syntax errors in your code that it didn't make sense to correct just a single one.
I tried to keep the main structure of your code - so maybe you can just check how to correct certain parts.
Laemi
Laemi 2023년 2월 4일
I'll try that.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 2월 4일
syms s phi theta;
First you create symbolic variables. The MATLAB level variables s phi and theta will be assigned references to objects that live in the symbolic engine. For example you might end up with
s.numdigits = 32;
s.s = '__symans[3151,1624]'
phi.numdigits = 32;
phi.s = '__symans[46,151516]';
where __symans[3151,1624] is something that can be looked up inside the symbolic engine and will return the unevaluated symbolic variable s . The __symans[3151,1624] part does not need to make sense as MATLAB syntax; it is a mostly-arbitrary unique string that the symbolic engine knows how to deal with.
Notice that the MATLAB level has no idea that the s at the MATLAB level is associated with a symbolic variable named s . It just knows that s is associated with some symbolic expression. So if you were, for example, to use
y = s*sin(phi)
then you would end up with y at the MATLAB being the same kind of internal _symans construct, with the MATLAB level having no idea what y means
s = 4 ;
phi = 4*pi/3;
Those are numeric values and overwrite the existing s and phi at the MATLAB level. But the symbolic engine would continue to know about __symans[3151,1624] and so on. Writing a new value to s at the MATLAB level absolutely does not have any effect on whatever is associated with symbolic variable s inside the Symbolic Engine. The Symbolic Engine is never notified that the MATLAB level s is changed, and as far as the symbolic engine is concerned, the reference associated with y continues to evaluate to s*sin(phi) inside the symbolic engine, and does not start evaluating to s*sin(4*pi/3)
The situation is similar to when you do
a = 5
b = 4*a + 1
a = 7
then b does not start evaluating to 4*7+1 . The value of a as of the time the assignment was executed is what was used for b and likewise when y = s*sin(phi) was executed, phi is a reference to a symbolic variable and after you assign a numeric value to phi then y would continue to have that reference to the symbolic variable phi without being updated to phi being replaced by 4*pi/3
[s_f, phi_f] = solve(eqns,[s phi]);
you are passing in s and phi as the second parameter to solve . But you overwrote s and phi at the MATLAB level, so you are calling solve(eqns, [4 4*pi/3] . Which is not valid syntax for solve -- the second parameter to solve must be a list of symbolic variable names.
%valeurs estimées de s et phi
But solve() does not permit you to pass in estimates or initial values to start from. vpasolve() on the other hand does permit initial values
You can do
sol = vpasolve(eqns, [s; phi]);
s_f = sol.s;
phi_f = sol.f;
because it is valid to pass in numeric estimates to vpasolve() without specifying the variables to solve for (symvar will be used to determine which variables to solve for in this case)
  댓글 수: 2
Laemi
Laemi 2023년 2월 4일
Thank for your answer,
I want to know if there a way to indicate at matlab the variable I used above is now equal to 7 ?
Is it possible?
Laemi
Laemi 2023년 2월 4일
I just find a way, you don't need to answer me.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by