Advertisement
cybereq

nonlinear

Nov 29th, 2022 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | Source Code | 0 0
  1. x = 1
  2. e = 0.01
  3. n = 100
  4.  
  5. def f(x):
  6.         return (1/2)*((x-1)**3)-2*(x-1)
  7.  
  8. # def df(x,ea):
  9. #         h = 0.5
  10. #         return (f(x + h * ea) - f(x - h * ea)) / ea
  11.  
  12. def df(x):
  13.         return 3/2*((x-1)**2) - 2
  14.  
  15. def rozw(r,ea,iter):
  16.         for k in range (0,iter):
  17.                 r0 = r
  18.                 r = r - f(r) / df(r)#df(r,ea)
  19.                 er = abs((r-r0)/r)*100
  20.                 print(f"\n\n{r}    {er}\n\n")
  21.                 if er < ea:
  22.                        break
  23.         return r,er,k
  24.  
  25. x0,err,k = rozw(x,e,n)
  26. print(f'x = {x}\n x0 = {x0}\nerr = {err}\nk = {k}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement