Thread Subject: Solving Non Linear Equations =(

Subject: Solving Non Linear Equations =(

From: Matlab Lab

Date: 24 Nov, 2009 22:05:25

Message: 1 of 4

OK so here goes my problem. I have spent ageeeess on this! Any help would be soo appreciated! Even if you can point out where I'm wrong..!


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

Subject: Solving Non Linear Equations =(

From: Richard Hindmarsh

Date: 24 Nov, 2009 22:35:18

Message: 2 of 4

Good try but your Jaco is wrong I think - my guess is


Jo= [-8-6*xo(1)^2 4 ; -4 3-2*xo(2)]

It's always best to check by numerical differentiation.

> 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

Subject: Solving Non Linear Equations =(

From: Matlab Lab

Date: 24 Nov, 2009 23:47:04

Message: 3 of 4

"Richard Hindmarsh" <rcah@bas.ac.uk> wrote in message <hehn36$1g6$1@fred.mathworks.com>...
> Good try but your Jaco is wrong I think - my guess is
>
>
> Jo= [-8-6*xo(1)^2 4 ; -4 3-2*xo(2)]
>
> It's always best to check by numerical differentiation.
>
> > 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








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

Subject: Solving Non Linear Equations =(

From: Darren Rowland

Date: 25 Nov, 2009 02:20:17

Message: 4 of 4

I haven't tested your code, but I spot at least one error (in addition to the Jacobian as spotted by Richard).

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

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com