H.W.5

Q1.
# SM Q1
import math as m
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return m.exp(x)*m.log(x)-x**2
# from NRM change df -> SM
def df(x, dx):
return (f(x)-f(x-dx))/dx
x=1
delta=1
tolerance=1e-6
dx=0.1
while abs(delta)>tolerance:
delta=-f(x)/df(x, dx)
x+=delta
dx=delta
print(x)
Q2.
# SM Q2 HW5
import math as m
import numpy as np
def f(x):
return x**5-3*x**4-5*x**3+x**2+x+3
# from NRM change df -> SM
def df(x, dx):
return (f(x)-f(x-dx))/dx
if __name__=="__main__":
# 3
x=[-2, 1, 4]
sol=[]
tolerance=1e-6
for xx in x:
delta=1
dx=0.1
while abs(delta)>tolerance:
delta=-f(xx)/df(xx, dx)
xx+=delta
sol.append(xx)
print("Solution x:", sol[0], "and", sol[1], "and", sol[2])
print("With interval x:", end=" ")
for s in sol:
if s>=-2 and s<=4:
print(s, end=" ")
print()