473,785 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

derivative and newton raphson

16 New Member
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 8832
bvdet
2,851 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Moderator Specialist
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
KaezarRex
52 New Member
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 Recognized Expert Moderator Specialist
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

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

Similar topics

2
16163
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 of x0, tolerance, and a boolean as to whether the approximate or exact df/dx is to be used. and its specified that the function names and their signatures have to be as they are below. thats where im getting all buggered up really. i totally...
6
29319
by: sekitoleko | last post by:
c program for newton raphson algorithm for finding roots of a polynomial
2
7024
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
9133
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 my 18 equations and give me the result
3
3449
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 it is diverging. Can someone please help me? Thank's! 'Cálculo do Sistema para obter os cossenos directores através do método Newton-Raphson Dim nr, aux1, aux2, aux3, lll, mmm, nnn, ff, gg, hh, ite As Double 'Condição...
2
2471
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!! THanks a lot!!
1
3009
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
6815
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
4552
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 the game is to find a zero of this function f (the point at which f crosses the x-axis). This zero-of-f which solves our problem is the double which NR returns. It remains to explain what the "double x" represents. This is the...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.