Python Forum

Full Version: derivatives
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to use a specific formula that sort of uses it self over and over, been trying to figue it out on my own please help me. Thank you
 import math
def main():
   for i in range(20):
      x0 = 0.375
      x0 = NR(x0)
   print(format(x0, '.3f'))
def NR(x0):
   fx = int(x0**2 -3*x0 +1)
   fxp = 2*x0 - 3
   f = x0 - fx/fxp
   f = x0         
   print('x:','\t','\t','\t',format(f,'.3f'))
   return '\t','\t','\t',x0
   #print('\n','Spread sheet completed!')
main()
results:
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
Traceback (most recent call last):
  File "mathe project.py", line 25, in <module>
    main()
  File "mathe project.py", line 16, in main
    print(format(x0, '.3f'))
TypeError: unsupported format string passed to tuple.__format__
I am trying to use a specific formula that sort of uses it self over and over, been trying to figue it out on my own please help me. Thank you
import math
def main():
   for i in range(20):
      x0 = 0.375
      x0 = NR(x0)
   print(format(x0, '.3f'))
def NR(x0):
   fx = int(x0**2 -3*x0 +1)
   fxp = 2*x0 - 3
   f = x0 - fx/fxp
   f = x0         
   print('x:','\t','\t','\t',format(f,'.3f'))
   return '\t','\t','\t',x0
main()
the results suppose to giving be a list of using x0 in the formula f then the results of f back in to the formula f as x0 in other words f = x0 after each cycle but my results are

results:
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
x: 	 	 	 0.375
Traceback (most recent call last):
  File "mathe project.py", line 24, in <module>
    main()
  File "mathe project.py", line 16, in main
    print(format(x0, '.3f'))
TypeError: unsupported format string passed to tuple.__format__
please help me thank you
Duplicate thread. Merged ... Against the rules, please don't post duplicate threads
Sorry bbout that but still needs help
(Mar-19-2018, 03:02 PM)kc4566 Wrote: [ -> ]
def main():
   for i in range(20):
      x0 = 0.375
      x0 = NR(x0)
If you want x0 to change each iteration, then stop resetting it to 0.375 with each iteration.