Newton's Method for nonlinear system vector operation.

63 views (last 30 days)
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 2x1 vector, and J is jacobian matrix of F which is 2x2. 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

Accepted Answer

Geoff Hayes
Geoff Hayes on 25 Sep 2014
Lechen - if I run through your code, I observe the following error
Error using +
Matrix dimensions must agree.
Error in newton.m
y = x + d;
because x is a 1x2 matrix and d is a 2x1 matrix. The simplest fix for this is to just change the input x from a 1x2 matrix to one that is 2x1
a = newton(@F, @J, [1;1])
That will fix the error message. Now look closer at the newton function
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
Note how the code does not increment the step local variable, so the code will become stuck in an infinite loop. Add a line to increment this variable. Also, consider adding it a line of code that compares x and y - if the difference between the two vectors (for each element) is less than some tolerance, then assume that no better solution will be found and so break out of the loop. The above then becomes something like
function y = newton(f,j,x)
step = 1;
finalstep = 10;
y = zeros (2,1);
tol = 1e-5;
while step < finalstep
d = j(x)\(-f(x));
y = x + d;
disp(y);
% increment step
step = step + 1;
% compare each element of x and y
exitEarly = true;
for k=1:length(x)
if abs(x(k)-y(k))>tol
% pair k exceeds tolerance so do not exit
% early
exitEarly=false;
end
end
if exitEarly
break;
end
x = y;
end
end
Try the above and see what happens!
  1 Comment
Lechen Yuan
Lechen Yuan on 25 Sep 2014
for the love of mother of god, finally........ i was trying to fix the x into a column vector the whole time! and never thought about fixing it outside of the script. Thank you so much!!!!!!!!!!

Sign in to comment.

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 25 Sep 2014
Edited: Andrei Bobrov on 25 Sep 2014
function x = newtonf(x,finalstep)
function out = F(x)
out = [x(1).^2-x(2).^2+2*x(2);
2*x(1)+x(2).^2-6];
end
function out = J(x)
out = [2*x(1),-2*x(2)+2;
2, 2*x(2) ];
end
step = 1;
y = [];
while step < finalstep
d = -J(x)\F(x);
x = x + d;
y = [y, x];
step = step + 1;
end
disp(y);
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!