clear all
xmatlab1 = fsolve(@NonlinearExample,[1;1]);
options = optimoptions(@fsolve,’Display’,’iter’,’SpecifyObjectiveGradient’,true);
[xmatlab2,F,exitflag,output,JAC] = fsolve(@NonlinearExampleWithJacobian,[1;1],options);
x=[1;1]
[f,J]=NonlinearExampleWithJacobian(x)
while max(abs(f)) >1.0e-6
det=J(1,1)*J(2,2)-J(1,2)*J(2,1);
x(1)=x(1)+(f(2)*J(1,2)-f(1)*J(2,2))/det;
x(2)=x(2)+(f(1)*J(2,1)-f(2)*J(1,1))/det;
[f,J]=NonlinearExampleWithJacobian(x)
end
function [f,J] = NonlinearExampleWithJacobian(x)
f(1) = x(1)*x(1)+1-x(2);
f(2) = 3*cos(x(1))-x(2);
J(1,1)=2*x(1);J(1,2)=-1;
J(2,1)=-3*sin(x(1));J(2,2)=-1
end
function f = NonlinearExample(x)
f(1) = x(1)*x(1)+1-x(2);
f(2) = 3*cos(x(1))-x(2);
end