[CSC 335] Python Code from Class
Andrew J. Pounds
pounds_aj at mercer.edu
Wed Sep 20 08:51:29 EDT 2017
So I wanted to send you all the python code I used in class yesterday.
For the fixed point method (using the inverse derived from Mathematica)
#!/usr/bin/python
def f(x):
part1=(848.0 + 356.0*x + 27.0*x**2)**(1.0/2.0)
part2=(178.0+27.0*x+3.0*3.0**(1.0/2.0)*part1)**(1.0/3.0)
part4= 13.0*2.0**(1.0/3.0)/(3*part2)
part5=part2/(3.0*2.0**(1.0/3.0))
return part4+part5-4.0/3.0
x=100
for i in range(1,10):
print x, f(x)
x= f(x)
And then here is the code modified to use Steffenson's Convergence
Algorithm. I am not testing for convergence in either of these, but I
am using them to quickly compute the sequence. I really encourage
those of you that are doing all of this by hand to give this a try.
Althought there is a learning curve, could really make your life easier.
#!/usr/bin/python
def f(x):
part1=(848.0 + 356.0*x + 27.0*x**2)**(1.0/2.0)
part2=(178.0+27.0*x+3.0*3.0**(1.0/2.0)*part1)**(1.0/3.0)
part4= 13.0*2.0**(1.0/3.0)/(3*part2)
part5=part2/(3.0*2.0**(1.0/3.0))
return part4+part5-4.0/3.0
x=100
for i in range(1,10):
p1 = f(x)
p2 = f(p1)
p = x - (p1 - x)**2 / (p2 - 2.0*p1 + x)
print x, p1, p2, p
x=p
Let me know if you have any questions -- I'll do whatever I can.
--
Andrew J. Pounds, Ph.D. (pounds_aj at mercer.edu)
Professor of Chemistry and Computer Science
Mercer University, Macon, GA 31207 (478) 301-5627
http://faculty.mercer.edu/pounds_aj
More information about the csc335
mailing list