필터 지우기
필터 지우기

Matrix dimensions must agree

조회 수: 1 (최근 30일)
Roberto Antonino Ruggeri
Roberto Antonino Ruggeri 2016년 2월 18일
편집: John D'Errico 2016년 2월 18일
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
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
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.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by