Once I wrote this function to find the zero of any continuous and differentiable function def newton(fun,y): #A function_name and an initial guess is provided as arguments of the function 'newton' n=len(y);epsilon=10**(-12); e=np.eye(n)*epsilon; fval=fun(y);i=1 while (np.linalg.norm(fval))>1e-10 and i
Dear mechtutor, I would like to apply the same code, but for a list of elements, It means my initial values are X0 = {1,2,3,4,5,6} and my functions give me a result for each of these initial values it means f = f(x0), same my derivative f'=f'(x0). So as you see at the end I will get a list of values. Do you think is possible?
Yes, it is possible. You will have to modify your code such that if a list of values were given, the code would loop through the values given. You would also need to allocate for another list/array where you would store the roots that were found. Your use case is interesting, but most root-solver codes don't have a "vectorized" implementation as the one you're asking. They only take one input guess at a time. So if you want to find the root for a given initial values, just put the Newton solver function inside a loop that loops through your given initial values.
Because this method works only in the real domain, you cannot use it for complex roots. The alternative way is to use the function Scipy.optimize.fsolve() Thanks for your comment.
import numpy as np from scipy.misc import derivative def f(x): return 2*x**3-9.5*x+7.5 x1 = 2 x = np.linspace(0.2, 2, 500) m = derivative(f, x1, dx=0.1) for n in range(20): xn = x1 -( f(x1) / m) print(xn) x1 = xn
def newton_approximation(f,appr): x = sym.Symbol('x') fprime = sym.diff(f,x) f = sym.lambdify(x,f) fprime = sym.lambdify(x,fprime) steps = 0 x = appr for i in range(0,100):
new_x = x - (f(x) / fprime(x)) steps+=1 if round(x,8) == round(new_x, 8): break else: x = new_x
return new_x, steps x = sym.Symbol('x') f = x**2 - 104 print("Total steps taken in order to approximate the root {0} are {1}".format(*newton_approximation(f,3)))
If you are interested, here's a free NumPy mini-course link: rb.gy/pk99l ... I hope you'll find it useful.
It is a long time, welcome again on UA-cam. Please, make more videos on Matlab
You are the best man in the world. I spent one week for doing this shit. I love so much. Thank you
Amazing video thank you helped me so much in my computational class!
What to do if I don’t know the initial guesses, i know i can use range , but what if the roots are far apart from each other?
Once I wrote this function to find the zero of any continuous and differentiable function
def newton(fun,y): #A function_name and an initial guess is provided as arguments of the function 'newton'
n=len(y);epsilon=10**(-12);
e=np.eye(n)*epsilon;
fval=fun(y);i=1
while (np.linalg.norm(fval))>1e-10 and i
Dear mechtutor, I would like to apply the same code, but for a list of elements, It means my initial values are X0 = {1,2,3,4,5,6} and my functions give me a result for each of these initial values it means f = f(x0), same my derivative f'=f'(x0). So as you see at the end I will get a list of values. Do you think is possible?
Yes, it is possible. You will have to modify your code such that if a list of values were given, the code would loop through the values given. You would also need to allocate for another list/array where you would store the roots that were found.
Your use case is interesting, but most root-solver codes don't have a "vectorized" implementation as the one you're asking. They only take one input guess at a time. So if you want to find the root for a given initial values, just put the Newton solver function inside a loop that loops through your given initial values.
Awesome video sir! Thank you!
How can I do it but for the unreal roots?
Because this method works only in the real domain, you cannot use it for complex roots. The alternative way is to use the function
Scipy.optimize.fsolve()
Thanks for your comment.
the best explanation ever :)
Zoom ur code...really appearing small
How to solve an equation with logx
Terimakasih bapak
thank you very much that was so helpful and well explained.
how do u add the bee sound every time u run the program
Hi friends. I have launched a new course. To learn about it, click here please: mechtutor.thinkific.com/courses/python-for-science-and-engineering
That was really useful 🧡👍🏼
ua-cam.com/video/kxftUHk7NDk/v-deo.html
any laptopassignment 2 calculusers?
did you just like your own comment?
@@TPLCompany i did not, imagine having so little to do in life, till the point where you mock others for doing so
@@kaasbaas5906 damn
What if I have 7 nonlinear equations
YOU SAVED ME
import numpy as np
from scipy.misc import derivative
def f(x):
return 2*x**3-9.5*x+7.5
x1 = 2
x = np.linspace(0.2, 2, 500)
m = derivative(f, x1, dx=0.1)
for n in range(20):
xn = x1 -( f(x1) / m)
print(xn)
x1 = xn
♥♥♥♥♥♥♥♥♥♥♥♥♥♥
def newton_approximation(f,appr):
x = sym.Symbol('x')
fprime = sym.diff(f,x)
f = sym.lambdify(x,f)
fprime = sym.lambdify(x,fprime)
steps = 0
x = appr
for i in range(0,100):
new_x = x - (f(x) / fprime(x))
steps+=1
if round(x,8) == round(new_x, 8):
break
else: x = new_x
return new_x, steps
x = sym.Symbol('x')
f = x**2 - 104
print("Total steps taken in order to approximate the root {0} are {1}".format(*newton_approximation(f,3)))
Do not forget to "import sympy as sym"