473,289 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

derivative and newton raphson

16
i have made a code for finding a derivative and now im trying to use it to help me with a code for the newton raphson method:
Expand|Select|Wrap|Line Numbers
  1. def derivative (f,x,h):
  2.     import math
  3.     return float(1/(2*h)) * (f(x+h) - f(x-h))
  4.  
  5. def solve (f,x0,h):
  6.     delta= f(x(n))/fp(x(n)
  7.         for x(n+1) in solve():
  8.             x(n)-delta
the first def works fine but i cant get the second def to work can anyone see what im doing wrong?
Oct 29 '07 #1
10 8790
bvdet
2,851 Expert Mod 2GB
i have made a code for finding a derivative and now im trying to use it to help me with a code for the newton raphson method:
Expand|Select|Wrap|Line Numbers
  1. def derivative (f,x,h):
  2.     import math
  3.     return float(1/(2*h)) * (f(x+h) - f(x-h))
  4.  
  5. def solve (f,x0,h):
  6.     delta= f(x(n))/fp(x(n)
  7.         for x(n+1) in solve():
  8.             x(n)-delta
the first def works fine but i cant get the second def to work can anyone see what im doing wrong?
In function solve(), you have passed arguments 'f', 'x0' and 'h'. Where are 'x', 'fp', and 'n' defined? You are calling function 'solve()' recursively, but you are not passing any arguments to it. 'solve()' requires three arguments. The format of a 'for' loop is:
Expand|Select|Wrap|Line Numbers
  1. for item in iterable:
  2.     ......code......
'item' cannot be an expression.
Expand|Select|Wrap|Line Numbers
  1. >>> for x+1 in range(10):
  2. ...     print x
  3. Traceback (SyntaxError: can't assign to operator
  4. >>> 
Oct 29 '07 #2
DDCane
16
how am im supposed to fix it? fp in solve is the fisrt function. u use the first function to get the derivative of f in the first function. do u know how i fix this?

In function solve(), you have passed arguments 'f', 'x0' and 'h'. Where are 'x', 'fp', and 'n' defined? You are calling function 'solve()' recursively, but you are not passing any arguments to it. 'solve()' requires three arguments. The format of a 'for' loop is:
Expand|Select|Wrap|Line Numbers
  1. for item in iterable:
  2.     ......code......
'item' cannot be an expression.
Expand|Select|Wrap|Line Numbers
  1. >>> for x+1 in range(10):
  2. ...     print x
  3. Traceback (SyntaxError: can't assign to operator
  4. >>> 
Oct 30 '07 #3
bvdet
2,851 Expert Mod 2GB
how am im supposed to fix it? fp in solve is the fisrt function. u use the first function to get the derivative of f in the first function. do u know how i fix this?
I am not familiar with the calculation you are attempting. I know that it will not work if 'fp()' is not defined inside of 'solve()' or globally to your module.
Oct 30 '07 #4
DDCane
16
globally in the module?

I am not familiar with the calculation you are attempting. I know that it will not work if 'fp()' is not defined inside of 'solve()' or globally to your module.
Oct 30 '07 #5
bvdet
2,851 Expert Mod 2GB
globally in the module?
Let's say your code is in a file called function.py.
Expand|Select|Wrap|Line Numbers
  1. # code.py
  2. def fp():
  3.     ....code....
  4.  
  5. def derivative (f,x,h):
  6.     import math
  7.     return float(1/(2*h)) * (f(x+h) - f(x-h))
  8.  
  9. def solve (f,x0,h):
  10.     delta= f(x(n))/fp(x(n)
  11.         for x(n+1) in solve():
  12.             x(n)-delta
  13.  
  14. if __name__ == __main__:
  15.     ....call your functions...
Function 'fp()' is global to module 'function'.
Oct 30 '07 #6
DDCane
16
ok. is there a simpler way to write the newton raphsons method in python? ive looked at the discussions in the forums here and havent found anything that could help m efurther than what i already know. do u have any clue on how to write the method?

Let's say your code is in a file called function.py.
Expand|Select|Wrap|Line Numbers
  1. # code.py
  2. def fp():
  3.     ....code....
  4.  
  5. def derivative (f,x,h):
  6.     import math
  7.     return float(1/(2*h)) * (f(x+h) - f(x-h))
  8.  
  9. def solve (f,x0,h):
  10.     delta= f(x(n))/fp(x(n)
  11.         for x(n+1) in solve():
  12.             x(n)-delta
  13.  
  14. if __name__ == __main__:
  15.     ....call your functions...
Function 'fp()' is global to module 'function'.
Oct 30 '07 #7
bvdet
2,851 Expert Mod 2GB
ok. is there a simpler way to write the newton raphsons method in python? ive looked at the discussions in the forums here and havent found anything that could help m efurther than what i already know. do u have any clue on how to write the method?
The Newton Raphson method uses an iterative process to approximate the root of a function. That's all I know about the subject, and I had to look that up.
Oct 30 '07 #8
ok. is there a simpler way to write the newton raphsons method in python? ive looked at the discussions in the forums here and havent found anything that could help m efurther than what i already know. do u have any clue on how to write the method?
Here's what I have come up with:
import math
Expand|Select|Wrap|Line Numbers
  1. def derivative (f, x, h):
  2.     return float((f(x + h) - f(x))) / h
  3.  
  4. def solve(f, x0, h, depth):
  5.     if depth > 0:
  6.         delta = f(x0) / derivative(f, x0, h)
  7.         return solve(f, x0 - delta, h, depth - 1)
  8.     else:
  9.         return x0
I changed the formulas in your function to ones I'm more familiar with, and I had to add the parameter "depth" to tell the solve function when to stop. That's necessary because when working with the Newton-Raphson Method, you have to choose how many times you use it.
To use the solve function, you can either do something like this:
Expand|Select|Wrap|Line Numbers
  1. def function(x):
  2.     return math.cos(x) - x**3
  3.  
  4. solve(function, 0.5, 0.001, 6)
or:
Expand|Select|Wrap|Line Numbers
  1. function = lambda x: math.cos(x) - x**3
  2. solve(function, 0.5, 0.001, 6)
Either way in that example your approximating the root of "cos(3) - x**3" by applying the Newton-Raphson Method 6 times with a starting guess of "0.5". The "0.001" is the "h" value used to approximate your derivative. The lower it is the more accurate the derivative will be.
Oct 30 '07 #9
bvdet
2,851 Expert Mod 2GB
Here's what I have come up with:
import math
Expand|Select|Wrap|Line Numbers
  1. def derivative (f, x, h):
  2.     return float((f(x + h) - f(x))) / h
  3.  
  4. def solve(f, x0, h, depth):
  5.     if depth > 0:
  6.         delta = f(x0) / derivative(f, x0, h)
  7.         return solve(f, x0 - delta, h, depth - 1)
  8.     else:
  9.         return x0
I changed the formulas in your function to ones I'm more familiar with, and I had to add the parameter "depth" to tell the solve function when to stop. That's necessary because when working with the Newton-Raphson Method, you have to choose how many times you use it.
To use the solve function, you can either do something like this:
Expand|Select|Wrap|Line Numbers
  1. def function(x):
  2.     return math.cos(x) - x**3
  3.  
  4. solve(function, 0.5, 0.001, 6)
or:
Expand|Select|Wrap|Line Numbers
  1. function = lambda x: math.cos(x) - x**3
  2. solve(function, 0.5, 0.001, 6)
Either way in that example your approximating the root of "cos(3) - x**3" by applying the Newton-Raphson Method 6 times with a starting guess of "0.5". The "0.001" is the "h" value used to approximate your derivative. The lower it is the more accurate the derivative will be.
That's great KaezarRex - nice solution to an interesting problem.
BV
Oct 31 '07 #10
Hello!
(Newton-Raphson Method)
Can anyone describe how this code work?
What is depth, and how is solve working?
The function derivative only give us the derivative of a function:
for example:
Expand|Select|Wrap|Line Numbers
  1. >>> derivative(math.sin, math.pi, 0.0001)
  2. -0.9999999983354435
  3.  
But how is solve work?
solve use derivative function.
Expand|Select|Wrap|Line Numbers
  1. def derivative (f, x, h):
  2.     return float((f(x + h) - f(x))) / h
  3.  
  4. def solve(f, x0, h, depth):
  5.     if depth > 0:
  6.         delta = f(x0) / derivative(f, x0, h)
  7.         return solve(f, x0 - delta, h, depth - 1)
  8.     else:
  9.         return x0
  10.  
Sep 26 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: moi | last post by:
Can someone please help with this problem im having. i have to use the newton-raphson technique to find the root of a function, in this case X^2 - 1. basically, the program has to read in values...
6
by: sekitoleko | last post by:
c program for newton raphson algorithm for finding roots of a polynomial
2
by: JamesUmokoro | last post by:
Please Im writing a project on mathematics here in school. Can someone help me with the source code for solving Numerical Analysis problem with Newton Raphson using Java programming? Thank you all...
11
by: kartikegarg | last post by:
can you help me please with this problem.. i want a c program using newton raphson method for solving 18 equations... the equations are not of degree greater than 1... i need the program to input...
3
by: spranto | last post by:
Hi guys I allready oppened another thread to know if someone can help me to solve a 3 non linear equation system. I tryed this code to make the newton raphson method to work, but insted of converging...
2
by: kolnit | last post by:
Find a soln to the following eqtn by Newton-Raphson's method e^(0.05x)+x^2=132254 Let f(x)= e^(0.05x)+x^2-132254 perform iterations until abs f(x)<10^-6 I just have no idea wt to do!!...
1
by: dynamo | last post by:
Hi guys,i was wondering if anyone knows the code to solve equations using the newton raphson method in matlab.Or at least the algorithm.
4
by: precioasisbest | last post by:
Please,I need a program in visual basic to solve the question below: -By applying Newton Raphson method,find the root of 3x-2tanx=0 given that there is a root between pie/6 and pie/3.Thank you.
6
by: pauldepstein | last post by:
Let double NR( double x, double(*)(const double&) f ) be the signature of a Newton-Raphson function NR. Here, f is a function which returns a double and accepts a const double&. The aim of...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.