Matrix Dimensions Must Agree Error, Error using +

조회 수: 2 (최근 30일)
Josefine  Jonsson
Josefine Jonsson 2015년 9월 22일
댓글: David Young 2015년 9월 22일
This is Newton Raphsons Method for systems. Row 1 and 2 in f= [x1 etc is 2 equations for 2 circles, and I want to find 2 intersections between these circles. I have already plotted and got guess 1 = [51 28] and 2 [41 47]. I need to find 2 x1, and x2 values who solves the system.
if true
format short e, format compact
disp(' x f(x) h=J\f(x)');
x=[51 28]'; %Startvektor
h=x;
iter=1;
while ( (norm(h,inf) > 1.0e-10*norm(x,inf)) & (iter < 20)),
f =[x(1).^2 + x(2).^2 -2*93*x(1) -2*63*x(2)+ 9.5820e+03
x(1).^2 + x(2).^2 -12*x(1) -32*x(2)- 1.8332e+03 ];
%Jacobian
J=[2*x(1)-2*93 2*x(2)-2*63
2*x(1)-12 2*x(2)-32];
h =-J\f;
disp(iter)
disp([x f h])
x=x+h; iter=iter+1;
end
format long
x
% code
end
Error using + Matrix dimensions must agree. Error in uppg6aNRAP (line 20) x=x+h; iter=iter+1;
PLS HELP! I'm a beginner

답변 (2개)

the cyclist
the cyclist 2015년 9월 22일
If you put a breakpoint at line 18, temporarily halting execution there, you will see that x is a 2x1 vector, and h is a 2x3 array. (Is that what you expect?) Arrays have to be the same shape to add them.
  댓글 수: 2
Josefine  Jonsson
Josefine Jonsson 2015년 9월 22일
It's supposed to be 2 lines in f, one for each circle equation. I initially took this code from a newton raphson for system with a f = 2x3. Matris, but deleted line 3 and wrote my
X^2 +y^2 - f1
X^2 + y^2 - f2
2x2 matris for the 2 circle equations instead
David Young
David Young 2015년 9월 22일
See my answer below for why this didn't happen, and how to fix it.

댓글을 달려면 로그인하십시오.


David Young
David Young 2015년 9월 22일
It's necessary to put spaces in the expression for f so that + and - are interpreted as binary operators rather than unary signs. Like this:
f =[x(1).^2 + x(2).^2 - 2*93*x(1) - 2*63*x(2) + 9.5820e+03
x(1).^2 + x(2).^2 - 12*x(1) - 32*x(2) - 1.8332e+03 ];

카테고리

Help CenterFile Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by