Question:
Solve the following system of equations by using Newton's method by implementing it in Matlab:
f1 = 4 - 8*x1 + 4*x2 - 2*(x1)^3 = 0
f2 = 1 - 4*x1 + 3*x2 + (x2)^2 = 0
by using x^0 = [0.5 0.5]^T as the starting point. Report the final solution obtained and plot progress of the solution as a function of iteration counter, i.e. plot x1, x2, f1 and f2 as a function of the iteration counter. Use a small tolerance such as 0.001 for final convergence.
Note: The set of linear equations of the form Ap = b obtained at each iteration may be solved by using a command of the form p=A\b in Matlab, where A is the conatsnt matrix, b is a constant vector and p is the vector of variables for which the equations must be solved.
Here is my working:
%NON LINEAR EQUATIONS USING MATLAB
xo= [0.5 0.5]'; %where x0= [x1 x2]'
disp (xo)
k=0
Jo= [-8-6*xo(1) 4 ; -4 3+2*xo(2)^2]%jacobian matrix
disp (Jo)
fo= [4-8*xo(1)+4*xo(2)-2*xo(1)^3 ; 1-4*xo(1)+3*xo(2)-xo(2)^2]
disp(fo)
epsilon=0.001;
maxiter=200;
for k=1: maxiter
error= sqrt(fo(1)^2+fo(2)^2)
if error <=epsilon
disp 'calculation converged'
end
if k >= maxiter
end
p=Jo\fo
x=xo
x=x+p
fnew= [4-8*x(1)+4*x(2)-2*x(1)^3 ; 1-4*x(1)+3*x(2)-x(2)^2]
Jnew= [-8-6*x(1) 4 ; -4 3+2*x(2)^2]
k=k+1
disp 'number of iterations='
disp (k)
disp 'fnew ='
disp(fnew)
disp 'Jnew='
disp (Jnew)
plot (fnew,k)
return
end
Jo= [-8-6*xo(1)^2 4 ; -4 3-2*xo(2)]
It's always best to check by numerical differentiation.
thank you!
I tried your guess for Jacobian but still failed . matlab wont work!
any ideas why? (anyone?) maybe its something to do with the loop?
it has to converge and im not getting any convergence....... which is a nightmare
p = Jo\(-fo);
x = x + p;
Note the minus sign on fo. Of course it can go in a number of places without changing the result. Hope that works now,
Darren