Unfortunately, your update doesn't convince me you did the correct job, based on your script, your x
variable is already the answer and residing in the workspace, the second time when your own scripts are being executed, only one iteration has been made, not the whole Newton's method.
If you are allowed to use Symbolic Toolbox
to compute the Jacobian, there is a more universal way of doing instead of just carrying out a problem-oriented script. Here is a modified version to match your notation of an old implementation of mine for Newton's method, and this could be easily vectorized for a multi-dimensional nonlinear equation system using varargin
input, and do a string size check on the inline function you passed to the following function.
function [s J] = newton(f,p0,tol,MaxIter)
format long
x = sym('x'); y = sym('y');
F = f([x,y]);
% Compute the Jacobian matrix symbolically
J = jacobian(F);
invJ = inv(J);
s = zeros(MaxIter,2);
s(1,:) = p0;
dsnorm = inf;
iter = 1;
while dsnorm>tol && iter<MaxIter
ds = -subs(invJ,[x y],s(iter,:))*f(s(iter,:));
s(iter+1,:) = s(iter,:) + ds';
dsnorm = norm(ds,inf);
iter = iter+1;
end
s = s(1:iter,:);
end
This function accepts input f
as an inline function, p0
is the initial guess, tol
is the tolerance and MaxIter
is the maximum iteration allowed, if the vectorized version of your input is:
>>f=inline('[p(:,1).^2+p(:,2).^2-2.12; ...
p(:,2).^2-p(:,1).^2.*p(:,2)-0.04]','p');
Then
>>s = newton(f,[1 1],0.4e-6,10)
would return:
s =
1.000000000000000 1.000000000000000
1.006666666666667 1.053333333333333
1.006852189012140 1.051784722315402
1.006852720492674 1.051783057117558
1.006852720493521 1.051783057115295
As you can see the last output is different from yours, because basically what you had was fsolve
output, however fsolve
use trust-region-dogleg
algorithm by default, not Newton's method, if your output coincides with fsolve
's result, then your implementation should have a problem.
Best Answer
I think that you should obtain solution using Mathematica NSolve command :
$x=1.4091$