Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Problem with Newton Raphson Method for Two Variables
Replies: 5   Last Post: Jun 25, 2013 2:27 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Torsten

Posts: 1,477
Registered: 11/8/10
Re: Problem with Newton Raphson Method for Two Variables
Posted: Oct 12, 2011 6:41 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 12 Okt., 11:20, "Emre " <emredemir1...@yahoo.com> wrote:
> clc,clear
> % Newton Raphson solution of two nonlinear algebraic equations
> % set up the iteration
> error1 = 1.e8;
> xx(1) = 0; % initial guesses
> xx(2) = 0.5;
> iter=0;
> maxiter=40
> % begin iteration
> while error1>1.e-12
> iter=iter+1;
> x = xx(1);
> y = xx(2);


Here, you use y as a scalar.

> % calculate the functions
> f(1) = 4*x^2-20*x+0.25*y^2+8;
> f(2) = 0.5*x*y^2+2*x+8;
> % calculate the Jacobian
> J(1,1) = 8*x-20;
> J(1,2) = 0.5*y;
> J(2,1) = 0.5*y^2+2;
> J(2,2) = x*y;
> % solve the linear equations
> y = -J\f';


Here, y becomes a vector.
Use z instead: z=-J\f.

> % move the solution, xx(k+1) - xx(k), to xx(k+1)
> xx = xx + y';


xx = xx + z;

> % calculate norms
> error1=sqrt(y(1)^2+y(2)^2);


error1=sqrt(z(1)^2+z(2)^2);

> error(iter)=sqrt(f(1)^2+f(2)^2);

To be correct here, you would have to evaluate f with the new xx -
you calculate the error(iter) with the old xx - z.

> ii(iter)=iter;
> if (iter > maxiter)
> error1 = 0;
> s=sprintf('****Did not converge within %3.0f iterations.****',maxiter);
> disp(s)
> end
> % check if error1 < 1.e-12
> end
> x = xx(1);
> y = xx(2);
> f(1) = 4*x^2-20*x+0.25*y^2+8;
> f(2) = 0.5*x*y^2+2*x+8;
> % print results
> f
> xx
> iter
> % plot results
> semilogy(ii,error)
> xlabel('iteration number')
> ylabel('norm of functions')
> clear ii
> clear error
>
> what am i doing wrong here, it doesn't converge and find the correct values for x and y?
> Is this part
> "error1=sqrt(y(1)^2+y(2)^2);
> error(iter)=sqrt(f(1)^2+f(2)^2);" right?
> Please help me, thanks.


Best wishes
Torsten.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.