Effacer les filtres
Effacer les filtres

Matrix dimensions must agree

1 vue (au cours des 30 derniers jours)
Roberto Antonino Ruggeri
Roberto Antonino Ruggeri le 18 Fév 2016
Modifié(e) : John D'Errico le 18 Fév 2016
Hi, I am trying to compile this code but I don't know why it gives me this error. I think matrix dimensions are right
n=0
eps=1
x=[1,1]
%f=x^2+y^2
%Computation loop
while eps>1e-10&n<100
gradf=[2*x(1);2*x(2)];
eps=abs(gradf(1))+abs(gradf(2));
Hf=[2,0;0,2];
y=x-Hf\gradf
x=y
n=n+1
end
n,x,eps
Could someone help me? I am new in Matlab! The excercise i'm solving is for the newton method!
  1 commentaire
John D'Errico
John D'Errico le 18 Fév 2016
It is a REALLY bad idea to use eps as a variable. eps is already in existence as a very useful function in MATLAB. Save yourself many headaches in the future and use a different variable name. Else your next question will be why does my code not run properly?

Connectez-vous pour commenter.

Réponses (1)

John D'Errico
John D'Errico le 18 Fév 2016
Modifié(e) : John D'Errico le 18 Fév 2016
Ok, so LOOK carefully at your code. Don't just give up. If necessary, step through it ONE line at a time.
x starts out as a row vector, x=[1,1].
But later on, you have this line:
y=x-Hf\gradf
What is the result of Hf\gradf? Is it a row vector? Or is it a column vector?
What happens in MATLAB when you try to subtract a column vector from a row vector?
Learn to be careful when you write code. And learn to step through it, to see what is happening. Finally, learn NOT to use eps (or other useful function names) as a variable name. Your code will be the better for it.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by