Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Tue Oct 22 11:17:19 2019
- @author: student
- """
- import numpy as np
- import matplotlib.pyplot as plt
- import scipy.optimize as opt
- def f1(t):
- return t**3 - 6*t**2 + 11*t +np.cos(t/10) - 50
- def f2(t, a, b):
- return t**3 - 6*t**2 + 11*t +np.cos(t/a) - b
- def der_f1(t):
- return 3*t**2 - 12*t + 11 - 0.1*np.sin(t/10)
- x1 = np.linspace(-5,8,200)
- y1 = f1(x1)
- #xl = float(input('odaberi xlijevo: '))
- #print(xl, type(xl))
- def bisekcija():
- xl =-4
- xd = 7
- if f1(xl)*f1(xd)>0:
- print(' nije dobro!')
- sol = 1
- yl =f1(xl)
- yd = f1(xd)
- brojac = 0
- while sol > 1.e-04:
- #plt.plot(xl, yl, 'o')
- #plt.plot(xd, yd, 'o')
- xs = (xl+xd)/2
- ys = f1(xs)
- #plt.plot(xs, ys, 'D')
- sol = np.abs(ys)
- if yl *ys <0:
- xd = xs
- else:
- xl = xs
- if brojac > 3:
- break
- brojac += 1
- print('xs :', xs)
- xb = opt.bisect(f1, -5, 8)
- print("xb: ", xb)
- plt.plot(x1, y1)
- #plt.plot([xl,xd], [yl,yd], #linewidth = 4,
- # linestyle ='--',
- # color = 'salmon',
- # marker = 'D',
- # markersize = 10)
- plt.axhline(0, color='r') #traženje nultoče
- plt.show()
- def newton():
- x = np.linspace(-5, 8, 200)
- y = f1(x)
- x0 = 8
- y0 = f1(x0)
- sol = np.abs(y0)
- brojac = 0
- while sol > 1.e-04:
- x1 = x0 - f1(x0)/ der_f1(x0)
- y1 = f1(x1)
- plt.plot([x0, x1, x1], [y0, 0, y1], "-D")
- x0 = x1
- y0 = f1(x0)
- sol = np.abs(y0)
- brojac += 1
- if brojac > 100:
- break
- print("x1: ", x1)
- nx = opt.newton(f1, 8)
- print("nx: ", nx)
- fx = opt.fsolve(f1, 8)
- print("fx: ", fx, type(fx))
- plt.plot(x, y)
- plt.axhline(0, c = "r")
- plt.show()
- #newton()
- def nova():
- x = np.linspace(-5, 8, 200)
- y = f2(x, a = 10, b = 50)
- fx = opt.fsolve(f1, 8)
- print("fx: ", fx, type(fx))
- fx = opt.fsolve(f2, 8, args = (10, 50)) # args = argumenti
- print("fx: ", fx, type(fx))
- plt.plot(x, y)
- plt.axhline(0, c = "r")
- plt.plot(plt.xlim(), [100, 100], "g")
- plt.show()
- if __name__ == "__main__":
- nova()
Add Comment
Please, Sign In to add comment