Matrix dimensions must agree
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
John D'Errico
am 18 Feb. 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?
Antworten (1)
John D'Errico
am 18 Feb. 2016
Bearbeitet: John D'Errico
am 18 Feb. 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!