Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Numerical Methods Homework

Views: 706
Kernel: SageMath 8.2
#pg 59: 1,3,7
import numpy as np import scipy.optimize as sci x0 = 0 def f1a(x): return x**3-2*x+2 def f1b(x): return np.exp(x)-7 def f1c(x): return np.exp(x)+np.sin(x)-4+x # My implementation ran into overflow errors # def noot(fx,x0=x0, maxiter=200): # xs = [x0] # i = 0 # while (i < maxiter): # fx0 = (fx(xs[i])) # dfx0 = derivative(fx,x)(xs[i]) # xs.append(x0-(fx0/dfx0)) # i = i +1 # print(xs[i]) # return xs[i] # # print('1(a) = ',noot(f1a,x0)) # # print('1(b) = ',noot(f1b,x0)) # # print('1(c) = ',noot(f1c,x0)) print(sci.newton(f1a, x0, fprime=None, args=(), tol=1.48e-08, maxiter=50, fprime2=None)) print(sci.newton(f1b, x0, fprime=None, args=(), tol=1.48e-08, maxiter=50, fprime2=None)) print(sci.newton(f1c, x0, fprime=None, args=(), tol=1.48e-08, maxiter=50, fprime2=None)) # noot(f1a) # noot(f1b) # noot(f1c)
-1.76929235424 1.94591014906 0.864726265677
#and so on and so on