0% found this document useful (0 votes)
3 views7 pages

Math Python

Uploaded by

rushimathkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Math Python

Uploaded by

rushimathkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

NEWTON RAPHSON METHOD

import math
def f(x):
return(x**3+3*x+5)
def df(x):
return(3*x**2+3)
def NR(x):
print(('x=',x,'f(x)=',f(x),'df(x)=',df(x)))
if(df(x)==0):
return "invaid input"
if(abs(f(x))<0.0001):
return ('answer=',x)
return NR(x-f(x)/df(x))
RF

import math
def f(x):
return(x**3+x**2+3*x+4)
def RF(a,b):
fa=f(b)
fb=f(a)
if(fa<0):
t,a=a,b
b=t
t,fa=fa,fb
fb=t
if(fa*fb>0):
return "Invalid inputs"
c=(a*fb-b*fa)/(fb-fa)
fc=f(c)
print(('a=',a,'fa=',fa,'b=',b,'fb=',fb,'c=',c,'fc=',fc))
if(abs(fc)<0.0001):
return('answer=', c)
elif(fc<0):
return RF(c,b)
return RF(a,c)
BISECTION METHOD
import math
def f(x):
return(x**3+3*x+5)
def bisection(a,b):
fa=f(b)
fb=f(a)
if(fa>0):
t,a=a,b
b=t
t,fa=fa,fb
fb=t
if(fa*fb>0):
return "Invalid inputs"
mid=(a+b)/2
fmid=f(mid)
print(('a=',a,'fa=',fa,'b=',b,'fb=',fb,'mid=',mid,'fmid=',fmid))
if(abs(fmid)<0.0001):
return('answer=', mid)
elif(fmid<0):
return bisection(mid,b)
return bisection(a,mid)
TRAP

import math
def f(x):
return(1/1+x*x)
def trap(x0,xn,n):
h=(xn-x0)/n
intg=f(x0)+f(xn)
for i in range(1,n):
x=x0+i*h
intg=intg+2*f(x)

intg=intg*(h/2)
return intg
SIM13

import math
def f(x):
return(1/1+x*x)
def sim13(x0,xn,n):
h=(xn-x0)/n
intg=f(x0)+f(xn)
for i in range(1,n):
x=x0+i*h
if(i%2==0):
intg=intg+2*f(x)
else:
intg=intg+4*f(x)
intg=intg*(h/3)
return intg
WEDD

import math
def f(x):
return(1/1+x*x)
def wed(x0,xn,n):
h=(xn-x0)/n
intg=f(x0)+f(xn)
for i in range(1,n):
x=x0+i*h
if(i%2==0):
intg=intg+f(x)
elif(i%3==0):
intg=intg+6*f(x)
else:
intg=intg+5*f(x)
intg=intg*(3*h/10)
return intg
SIM38

import math
def f(x):
return(1/1+x*x)
def sim38(x0,xn,n):
h=(xn-x0)/n
intg=f(x0)+f(xn)
for i in range(1,n):
x=x0+i*h
if(i%3==0):
intg=intg+2*f(x)
else:
intg=intg+3*f(x)
intg=intg*(3*h/8)
return intg

You might also like