[Fortran] 纯文本查看 复制代码
real function f(x)
implicit none
real,intent(in)::x
f=x**3-2*x**2+x-1
end function f
real function df(x)
implicit none
real,intent(in)::x
df=3*x**2-4*x+1
end function df
subroutine newton_raphson()
implicit none
integer::i=0
real::x0
real::temp
real,external::f,df
write(*,*) "Enter origin x0 : "
read(*,*) x0
do
temp=x0-f(x0)/df(x0)
if (abs(temp-x0)<1.0e-5) exit
x0=temp
i=i+1
write(*,100) f(x0),temp,i
100 format (2x,"f(x)=",f7.3," when x= ",f6.3," at iteration",i3)
end do
! i=i+1.0
write(*,*) temp
end subroutine newton_raphson
Fortran代码写的不行,还请多担待,我其实才学fortran没多久。笑脸!![Python] 纯文本查看 复制代码
{lamb} python
Python 2.7.11 |Anaconda 2.4.1 (64-bit)| (default, Dec 7 2015, 14:10:42) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import newton
>>> print newton.__doc__
This module 'newton' is auto-generated with f2py (version:2).
Functions:
f = f(x)
df = df(x)
newton_raphson()
.
>>> newton.newton_raphson()
Enter origin x0 :
5
f(x)= 23.064 when x= 3.589 at iteration 1
f(x)= 6.533 when x= 2.677 at iteration 2
f(x)= 1.680 when x= 2.124 at iteration 3
f(x)= 0.317 when x= 1.845 at iteration 4
f(x)= 0.024 when x= 1.762 at iteration 5
f(x)= 0.000 when x= 1.755 at iteration 6
f(x)= -0.000 when x= 1.755 at iteration 7
1.754878
>>>