Error using * Dimension do not match

26 views (last 30 days)
Laemi
Laemi on 1 Feb 2023
Edited: Laemi on 4 Feb 2023
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.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 1 Feb 2023
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
f = 
Jac = 
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 Comments
Dyuman Joshi
Dyuman Joshi on 4 Feb 2023
Adding to Walter's point,
err(ones(0,i))
This is a bit peculiar as well, @Laemi. Why are you using an empty matrix as an index?
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
Dyuman Joshi on 4 Feb 2023
"This is a situation in which using ' would often be more mathematically correct than using .' "
@Walter Roberson - because the output could have imaginary values?

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!