Matrix dimensions must agree
2 views (last 30 days)
Show older comments
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 Comment
John D'Errico
on 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?
Answers (1)
John D'Errico
on 18 Feb 2016
Edited: John D'Errico
on 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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!