Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Newton-Raphson

Subject: Newton-Raphson

From: Ahmad Chkeir

Date: 2 Dec, 2009 23:08:02

Message: 1 of 19

Hey
I have to implement this in matlab that corresponds to power flow but without using functions :

 f1(x) = x1 + x2 - 15 = 0
 f2(x) = x1*x2 - 50 = 0

 x^r = x^r-1 - Jacobean^-1 * f(x^r-1)
 
Taking as initial value x(0) = [ 4 ; 9 ] for example

Subject: Newton-Raphson

From: TideMan

Date: 2 Dec, 2009 23:58:25

Message: 2 of 19

On Dec 3, 12:08 pm, "Ahmad Chkeir" <chkeir_ah...@Hotmail.com> wrote:
> Hey
> I have to implement this in matlab that corresponds to power flow but without using functions :
>
>  f1(x) = x1 + x2 - 15  = 0
>  f2(x) = x1*x2    - 50 = 0
>
>  x^r  =  x^r-1    -     Jacobean^-1  *  f(x^r-1)
>
> Taking  as initial value  x(0) = [ 4 ; 9 ] for example

You don't need to go to all the trouble of using Newton Raphson to
solve this.
By algebraic re-arrangement, you get a quadratic equation that gives:
x2=10 or 5,
then
x1=5 or 10

Subject: Newton-Raphson

From: Sadik

Date: 3 Dec, 2009 01:49:02

Message: 3 of 19

@ TideMan
I think the purpose is not to find x1 and x2 mathematically, lots of people can do this. But it is rathre to deploy Newton's method.

@ Ahmad,

You can use the following code. But before that, I am writing the output of the code:

*******************************************************************
Iteration 1: x = [4.0000000000;9.0000000000], |deltaX| = 2
Iteration 2: x = [5.2000000000;9.8000000000], |deltaX| = 0.41739
Iteration 3: x = [4.9913043478;10.0086956522], |deltaX| = 0.017361
Iteration 4: x = [4.9999849295;10.0000150705], |deltaX| = 3.0141e-005
Iteration 5: x = [5.0000000000;10.0000000000], |deltaX| = 9.0846e-011
*******************************************************************

As you can see here, x1 = 5 and x2 = 10.

Here is the code. If you have any questions, please let me know at sadik.hava@gmail.com

Best.

%%***************************************************************
function NewtonRaphsonForAhmad()

clc

x_kMinus1 = [4;9];

f_x_kMinus1 = [x_kMinus1(1)+x_kMinus1(2)-15;x_kMinus1(1)*x_kMinus1(2)-50];

J_x_kMinus1 = [1 1;x_kMinus1(2) x_kMinus1(1)];

x_k = x_kMinus1 - inv(J_x_kMinus1)*f_x_kMinus1 ;

disp(['Iteration 1: x = [' num2str(x_kMinus1(1),'%11.10f') ';' num2str(x_kMinus1(2),'%11.10f') '], |deltaX| = ' num2str(sum(abs(x_k - x_kMinus1)))])

iterationCounter = 1;

while sum(abs(x_k - x_kMinus1)) >= 1e-5
 
    x_kMinus1 = x_k;
    
    iterationCounter = iterationCounter + 1;
    
    f_x_kMinus1 = [x_kMinus1(1)+x_kMinus1(2)-15;x_kMinus1(1)*x_kMinus1(2)-50];

    J_x_kMinus1 = [1 1;x_kMinus1(2) x_kMinus1(1)];

    x_k = x_kMinus1 - inv(J_x_kMinus1)*f_x_kMinus1 ;

    disp(['Iteration ' num2str(iterationCounter) ': x = [' num2str(x_kMinus1(1),'%11.10f') ';' num2str(x_kMinus1(2),'%11.10f') '], |deltaX| = ' num2str(sum(abs(x_k - x_kMinus1)))])
    
end

Subject: Newton-Raphson

From: Ahmad Chkeir

Date: 3 Dec, 2009 13:22:03

Message: 4 of 19

THAnk Sadik a lot....you're the king...

Subject: Newton-Raphson

From: isil

Date: 18 Jan, 2010 12:02:04

Message: 5 of 19

Pls help me I can not solve this equation.
using Newton-Raphson method.

equation is 1/2(3+x^2)exp{-x}-1+x=0

Please help meee...:( And also i need matlab coding and algorithm.....

Subject: Newton-Raphson

From: Steven Lord

Date: 18 Jan, 2010 14:53:13

Message: 6 of 19


"isil " <isil_ultraslann@hotmail.com> wrote in message
news:hj1ijs$1ah$1@fred.mathworks.com...
> Pls help me I can not solve this equation. using Newton-Raphson method.
> equation is 1/2(3+x^2)exp{-x}-1+x=0
> Please help meee...:( And also i need matlab coding and algorithm.....

This sounds an awful lot like a homework problem, and the general rule on
CSSM (this newsgroup) for homework problems (or even problems that sound
like homework problems) is that you need to show what you've tried first and
need to ask a SPECIFIC question about where you're stuck. No one on this
group will simply give you the code to solve your problem.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Newton-Raphson

From: Muhamad

Date: 8 Apr, 2010 17:04:07

Message: 7 of 19

Please help i have problem..how to make listing for this equation

sin(α)-(sinθ ) e^(-(2π+α-θ)/ωRC)=0

please help me, thanx you

Subject: Newton-Raphson

From: Walter Roberson

Date: 8 Apr, 2010 17:26:10

Message: 8 of 19

Muhamad wrote:
> Please help i have problem..how to make listing for this equation
>
> sin(α)-(sinθ ) e^(-(2π+α-θ)/ωRC)=0
>
> please help me, thanx you

It is a rare mathematical notation indeed that uses semi-colons as part
of an equation.

Please review the way your posting actually came out, and repost in a
way that we can read it, without any ampersand number-sign nine five two
semi-colon or the like. Probably you attempted to use characters that
are not part of the basic ASCII or ISO-8896-1 character sets.

Subject: Newton-Raphson

From: Muhamad

Date: 8 Apr, 2010 17:45:28

Message: 9 of 19

Walter Roberson <roberson@hushmail.com> wrote in message <hpl3jj$jm7$1@canopus.cc.umanitoba.ca>...
> Muhamad wrote:
> > Please help i have problem..how to make listing for this equation
> >
> > sin(α)-(sinθ ) e^(-(2π+α-θ)/ωRC)=0
> >
> > please help me, thanx you
>
> It is a rare mathematical notation indeed that uses semi-colons as part
> of an equation.
>
> Please review the way your posting actually came out, and repost in a
> way that we can read it, without any ampersand number-sign nine five two
> semi-colon or the like. Probably you attempted to use characters that
> are not part of the basic ASCII or ISO-8896-1 character sets.

oke,, i will insert a number to this equation

sin(A) - sin (16.62)e^-(180+A-16.62)/18.85

we must find a result for A with use newthon raphson,,

Subject: Newton-Raphson

From: Muhamad

Date: 8 Apr, 2010 17:52:24

Message: 10 of 19

oke, i will insert number to this equation :

sin(A) - sin(16.62)*e^-(180 + A - 16.62) / 18.85

my goal is to find a result for A, and i must to finish with use newton rapshon..

Subject: Newton-Raphson

From: James Allison

Date: 8 Apr, 2010 18:53:00

Message: 11 of 19

Some tips for your assignment:

You will need a while loop and two functions - one for the function you
are finding the zero of, and one for its derivative. I assume you
already have the formula:

http://en.wikipedia.org/wiki/Newton%27s_method

-James


Muhamad wrote:
> oke, i will insert number to this equation :
>
> sin(A) - sin(16.62)*e^-(180 + A - 16.62) / 18.85
>
> my goal is to find a result for A, and i must to finish with use newton
> rapshon..

Subject: Newton-Raphson

From: Walter Roberson

Date: 8 Apr, 2010 19:05:26

Message: 12 of 19

Muhamad wrote:

> oke,, i will insert a number to this equation
>
> sin(A) - sin (16.62)e^-(180+A-16.62)/18.85
> we must find a result for A with use newthon raphson,,

Careful, there are two solutions.

Other than that, it appears to be a normal application of Newton-Raphson,
which has been discussed here a number of times before. I presume that your
text or course notes outlines the method; what do you have so far in
translating the method into Matlab ?

Subject: Newton-Raphson

From: Muhamad

Date: 8 Apr, 2010 21:24:06

Message: 13 of 19

Walter Roberson <roberson@hushmail.com> wrote in message <hpl9do$sai$1@canopus.cc.umanitoba.ca>...
> Muhamad wrote:
>
> > oke,, i will insert a number to this equation
> >
> > sin(A) - sin (16.62)e^-(180+A-16.62)/18.85
> > we must find a result for A with use newthon raphson,,
>
> Careful, there are two solutions.
>
> Other than that, it appears to be a normal application of Newton-Raphson,
> which has been discussed here a number of times before. I presume that your
> text or course notes outlines the method; what do you have so far in
> translating the method into Matlab ?

can you help me with program's listing ? i still confused

Subject: Newton-Raphson

From: Muhamad

Date: 8 Apr, 2010 21:39:06

Message: 14 of 19

James Allison <james.allison@mathworks.com> wrote in message <hpl8mc$2pd$1@fred.mathworks.com>...
> Some tips for your assignment:
>
> You will need a while loop and two functions - one for the function you
> are finding the zero of, and one for its derivative. I assume you
> already have the formula:
>
> http://en.wikipedia.org/wiki/Newton%27s_method
>
> -James

i know it, i just have make a notes for it, but i am still confused to run it with matlab because i newbie for matlab, can you help me with program's listing, please.

Subject: Newton-Raphson

From: Walter Roberson

Date: 8 Apr, 2010 22:02:16

Message: 15 of 19

Muhamad wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <hpl9do$sai$1@canopus.cc.umanitoba.ca>...

>> Other than that, it appears to be a normal application of
>> Newton-Raphson, which has been discussed here a number of times
>> before. I presume that your text or course notes outlines the method;
>> what do you have so far in translating the method into Matlab ?

> can you help me with program's listing ? i still confused

I presume that your text or course notes outlines the method; what are some
particular parts you are having difficulty translating into Matlab? Have you
read the Getting Started section of the Matlab documentation (press F1 to
bring up the documentation) ? Do you have previous computer programming
experience? If so then it might help to write it in your more familiar
computer language first, and then translate step by step into Matlab.

We answer specific Matlab questions, but we do not do people's assignments for
them.

Subject: Newton-Raphson

From: Muhamad

Date: 8 Apr, 2010 22:18:22

Message: 16 of 19

 
> I presume that your text or course notes outlines the method; what are some
> particular parts you are having difficulty translating into Matlab? Have you
> read the Getting Started section of the Matlab documentation (press F1 to
> bring up the documentation) ? Do you have previous computer programming
> experience? If so then it might help to write it in your more familiar
> computer language first, and then translate step by step into Matlab.
>
> We answer specific Matlab questions, but we do not do people's assignments for
> them.

oke, i am still learning for this and i make something trial but it error can you help me

this is my listing

% Newton_Raphson.m
clear;
x = input('Nilai Awal :');
teta = input('Nilai Teta : ')
W = input('Nilai Omega(W) : ')
R = input('Nilai R : ')
C = input('Nilai C : ')

tolerance = 0.0001;
iterations = 0;
while (iterations<30) & (abs(func(x))>tolerance)
    x = x-func(x)/func_prime(x);
    iterations = iterations+1;
end

if iterations==30
    disp('No root found')
else
    disp([' Root = ' num2str(x,10) ' found in ' int2str(iterations) ' iterations.'])
end

-----------------------------------------------------------------------------------------------------------

%func_prime.m

function [value] = func_prime(x)
value = sin(A)-(sin(teta)*e^(-(180+A-teta)/WRC);

x = 1;

for j = 1:10
    x = x - func(x)/func_prime(x);
end

----------------------------------------------------------------------------------------------------------

%func.m

function[f] = func(x)
f = cos(A)-sin(teta)*e^(-(180+A-teta)/WRC);

paramnames =['A'];

---------------------------------------------------------------------------------------------------------

if you find a problem in my program please help me,,thank you

Subject: Newton-Raphson

From: Walter Roberson

Date: 8 Apr, 2010 22:36:01

Message: 17 of 19

Muhamad wrote:

> %func_prime.m
>
> function [value] = func_prime(x)
> value = sin(A)-(sin(teta)*e^(-(180+A-teta)/WRC);
>
> x = 1;
>
> for j = 1:10
> x = x - func(x)/func_prime(x);
> end

When you have a function, the only values that are returned to the calling
routine are the ones named before the '=' on the 'function' line. In this
code, 'value' is the only variable named there. Once you have computed value,
there is no point in doing any further work (unless it has to with graphics or
  outputs to a file or the display), because whatever you do will just be
thrown away. Thus the 'for' loop is not contributing anything to this routine
and can be discarded, just as the assignment to 'x' can be.

When you have a function, the only values that you can use in the function are
numeric constants, named constants such as pi, values you have already
computed in the routine, and values that you have named after the '(' on the
'function' line. [I'm simplifying slightly; there are some more advanced
facilities.] You name x in that position, but you do not use x. You do,
though, attempt to use A, teta, and WRC, none of which fall into the
categories I describe. Thus A, teta, and WRC are undefined as far as this
routine is concerned. If you want to use the values that you input from the
user, then you need to give names for them on the 'function' line, and you
need to pass the values in when you call the routine. For example, you could call

func_prime(A, WRC, teta)

and your corresponding 'function' line would be

function value = func_prime(A, WRC, teta)

Subject: Newton-Raphson

From: Muhamad

Date: 8 Apr, 2010 23:03:05

Message: 18 of 19

Walter Roberson <roberson@hushmail.com> wrote in message <hpllok$hac$1@canopus.cc.umanitoba.ca>...
> Muhamad wrote:
>
> > %func_prime.m
> >
> > function [value] = func_prime(x)
> > value = sin(A)-(sin(teta)*e^(-(180+A-teta)/WRC);
> >
> > x = 1;
> >
> > for j = 1:10
> > x = x - func(x)/func_prime(x);
> > end
>
> When you have a function, the only values that are returned to the calling
> routine are the ones named before the '=' on the 'function' line. In this
> code, 'value' is the only variable named there. Once you have computed value,
> there is no point in doing any further work (unless it has to with graphics or
> outputs to a file or the display), because whatever you do will just be
> thrown away. Thus the 'for' loop is not contributing anything to this routine
> and can be discarded, just as the assignment to 'x' can be.
>
> When you have a function, the only values that you can use in the function are
> numeric constants, named constants such as pi, values you have already
> computed in the routine, and values that you have named after the '(' on the
> 'function' line. [I'm simplifying slightly; there are some more advanced
> facilities.] You name x in that position, but you do not use x. You do,
> though, attempt to use A, teta, and WRC, none of which fall into the
> categories I describe. Thus A, teta, and WRC are undefined as far as this
> routine is concerned. If you want to use the values that you input from the
> user, then you need to give names for them on the 'function' line, and you
> need to pass the values in when you call the routine. For example, you could call
>
> func_prime(A, WRC, teta)
>
> and your corresponding 'function' line would be
>
> function value = func_prime(A, WRC, teta)

why but still error ?
------
??? Input argument 'teta' is undefined.

Error in ==> C:\Documents and Settings\madz\My Documents\Downloads\elda\func.m
On line 4 ==> f = cos(A)-sin(teta)*e^(-(180+A-teta)/W*R);

Error in ==> C:\Documents and Settings\madz\My Documents\Downloads\elda\Newton_Raphson.m
On line 11 ==> while (iterations<30) & (abs(func(x))>tolerance)
----

Subject: Newton-Raphson

From: Walter Roberson

Date: 8 Apr, 2010 23:21:07

Message: 19 of 19

Muhamad wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <hpllok$hac$1@canopus.cc.umanitoba.ca>...

>> When you have a function, the only values that you can use in the
>> function are numeric constants, named constants such as pi, values you
>> have already computed in the routine, and values that you have named
>> after the '(' on the 'function' line.

> why but still error ?
> ------
> ??? Input argument 'teta' is undefined.
>
> Error in ==> C:\Documents and Settings\madz\My
> Documents\Downloads\elda\func.m
> On line 4 ==> f = cos(A)-sin(teta)*e^(-(180+A-teta)/W*R);
>
> Error in ==> C:\Documents and Settings\madz\My
> Documents\Downloads\elda\Newton_Raphson.m
> On line 11 ==> while (iterations<30) & (abs(func(x))>tolerance)

If we look at that last error message, we see that you are calling func()
passing in only x. Inside func, you attempt to use teta. Review the paragraph
I have quoted above. Is teta a numeric literal, a named constant such as pi, a
value already computed in func(), or a variable you have named after the '('
of the 'function' line of func()? If the latter, have you passed in a value
for teta from the calling routine?


I never claimed to have fixed all of the problems in your code: I showed you
what was wrong with a particular routine and why, and assumed you would be
apply to take those explanations and apply the thought processes to your other
routines and fix them yourself.

Tags for this Thread

No tags are associated with this thread.

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.

Contact us