Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Thu Oct 19 11:22:10 2017
- @author: student
- """
- import numpy as np
- import matplotlib.pyplot as plt
- import scipy.optimize as opt
- def f(x):
- #return 2.0*x**2-4.0
- return np.cos(10*x)
- #a = input('unesi lijevu granicu: ')
- xl = 0
- #xl = float(a)
- #xl = float(input('unesi lijevu granicu: '))
- xd = 0.4
- print(f(xl), f(xd))
- plt.plot([xl], [f(xl)], 'ob') #zahtjevaju listu brojeva pa su u [], iako je to lista koja
- plt.plot([xd], [f(xd)], 'ob') #ima samo jedan clan
- it = 0
- while it < 30:
- xs = (xl+xd)/2
- plt.plot([xs], [f(xs)], 'or') #or je Red, ob je Blue
- plt.annotate(str(it), xy=[xs, f(xs)]) # str pretvara broj u string
- if abs(f(xs)) < 0.01:
- break
- #plt.annotate(it, xy=[xs, f(xs)]) #annotate pise rijec (1) na poziciji (xy =...)
- if f(xs)*f(xl) < 0:
- xd = xs
- else:
- xl = xs
- xs = (xl+xd)/2
- it += 1
- #print('rjesenje: f({})={}'.format(xs, f(xs)) )
- print('x: ', xs)
- print('f(x): ', f(xs))
- print('it : ', it)
- x_opt = opt.fsolve(f, 0)
- print(x_opt)
- x_f = np.linspace(0, 0.4, 401) #0.4 u 30
- y_f = f(x_f)
- plt.plot(plt.xlim(), [0, 0], color='red')
- plt. plot(x_f, y_f)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement