Error using * Dimension do not match

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

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

This is a situation in which using ' would often be more mathematically correct than using .'
To be honest I have never understood why conjugate transpose is often used with linear algebra... but I know that it is common.
Your NR() takes no parameters and always returns the same result. Then when you use the f and J results on the line after you call NR(), inv(J)*f or inv(J)*f.' is always going to be the same value. You should be considering calculating the value once and storing it
The result of inv(J)*f is going to be symbolic involving s, phi, theta. When you subtract that symbolic result from numeric V, the result written into V is going to be symbolic involving s, phi, theta. When you get down to testing whether the error is within tolerance, you are going to get an error because you cannot compare an expression involving unresolved symbolic variables and get out something that you can use with if
To be honest I have never understood why conjugate transpose is often used with linear algebra... but I know that it is common.
I don't know exactly all the applications in theoretical physics, but I've heard Hermitian matrices are very important therein, thus matrices with the property H = H'.
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
"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 Mathematics in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 1 Feb 2023

Edited:

on 4 Feb 2023

Community Treasure Hunt

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

Start Hunting!