when i was doing newton's method for nonlinear system, when I entered following code it tells me that it could not do subtraction between two vectors with different dimension. The thing is F is a 2×1 vector, and J is jacobian matrix of F which is 2×2. so I dont know what is going on with my code. the following is the code.
a = newton(@F, @J, [1,1])========================================= function y = newton(f,j,x) step = 1;finalstep = 10;y = zeros (2,1);while step < finalstep d = j(x)\(-f(x)); y = x + d; disp(y); x = y; end end========================================== function f = F(x) x1 = x(1); x2 = x(2); f = zeros(2,1); f(1) = x1^2-x2^2+2*x2; % f1(x1,x2)
f(2) = 2*x1+x2^2-6; % f2(x1,x2);
end========================================== function j = J(x) x1 = x(1); x2 = x(2); j = zeros(2,2); j(1,1) = 2*x1; % df1x1
j(1,2) = -2*x2+2; % df1x2
j(2,1) = 2; % df2x1
j(2,2) = 2*x2; % df2x2;
end
Best Answer