Error using * Dimension do not match
9 次查看(过去 30 天)
显示 更早的评论
this is my code :
function [f,J] = NR(~)
% f1 une fonction continue f1(s,phi) de deux variables.
% f2 une fonction continue f2(s,phi) de deux variables.
% gradf1 chaîne de caractères qui nomme le gradient de la fonction
% f1(x,y) de deux variables.
% gradf2 chaîne vectorielle qui nomme le gradient de la fonction
% f2(x,y) 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 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];
%Définir jacobian
J = [diff(f1,'s') diff(f1,'phi')
diff(f2,'s') diff(f2,'phi')];
end
%valeurs supposées de s et phi
s = input('valeurs initiales s0 = ');
phi = input('valeurs initiales phi0 = ');
v(1) = s';
v(2) = phi';
%conditions initials
[f, Jac] = NR();
V0 = (gradf1);
maxIter = 72;
tolV = 1e-4;
% Calcul avec Newton Raphson
V = V0;
Vold = V0;
for i = 1:maxIter
theta = 0:5:2*pi;
[f, Jac] = NR();
V = V -inv(Jac)*f;
err(ones(0,i)) = abs(V-Vold);
Vold =V;
if (err(ones(0,i))<tolV)
break;
end
end
sorry the comments are in french, I hope it's not a problem
When I run my coden I have these two errors and I don't know how to solve them.

0 个评论
采纳的回答
Dyuman Joshi
2023-2-1
As you can see here, Jac is 2x2, thus inv(Jac) will also be 2x2 and f is 1x2. The multiplication of 2x2 with 1x2 is not possible.
[f, Jac] = NR
You can either transpose f
inv(Jac)*f.'
or change the multiplication order
f*inv(Jac)
Also, there is an un-defined variable gradf1 in your code.
function [f,J] = NR
% f1 une fonction continue f1(s,phi) de deux variables.
% f2 une fonction continue f2(s,phi) de deux variables.
% gradf1 chaîne de caractères qui nomme le gradient de la fonction
% f1(x,y) de deux variables.
% gradf2 chaîne vectorielle qui nomme le gradient de la fonction
% f2(x,y) 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];
%Définir jacobian
J = [diff(f1,'s') diff(f1,'phi')
diff(f2,'s') diff(f2,'phi')];
end
5 个评论
Dyuman Joshi
2023-2-4
Adding to Walter's point,
err(ones(0,i))
And if err array (whatever it is supposed to be) is not used afterwards you can directly check for the tolerance in the if statement -
if abs(V-Vold)<tolV
break
end
Dyuman Joshi
2023-2-4
"This is a situation in which using ' would often be more mathematically correct than using .' "
@Walter Roberson - because the output could have imaginary values?
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

