Matrix dimensions must agree
조회 수: 1 (최근 30일)
이전 댓글 표시
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
John D'Errico
2016년 2월 18일
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?
답변 (1개)
John D'Errico
2016년 2월 18일
편집: John D'Errico
2016년 2월 18일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!