Matrix Dimensions Must Agree Error, Error using +

2 次查看(过去 30 天)
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 ];

Community Treasure Hunt

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

Start Hunting!

Translated by