Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
522 views40 pages

BNU 4th Sem Lab Manual

Lab Manual
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)
522 views40 pages

BNU 4th Sem Lab Manual

Lab Manual
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/ 40

Bengaluru North University

Lab Manual
4th Semester B.Sc Mathematics (NEP)
2022-23 BATCH
INDEX

Lab 1 First Order non-linear Partial Differential Equations-Type-1 &


Type-2

Lab 2 First Order non-linear Partial Differential Equations-Type-3 &


Type-4

Lab 3 Solving second order linear PDE in two variables with constant
coefficients
Lab 4 Solution to One Dimensional Heat equation using Fourier series

Lab 5 Solution to One Dimensional Wave equation using Fourier series

Lab 6 Fourier Series

Lab 7 Half Range Fourier Series

Lab 8 Laplace Transform of a function

Lab 9 Inverse Laplace Transforms

Lab10 Solving ODE using Laplace Transform with initial conditions


Lab.1-First Order non-linear Partial Differential Equations-Type-1 & Type-2
Type 1: Equations of the form f(p,q)=0

1.Solve p2+q2=3
from sympy import *
a,b,c,x,y,z,p,q=symbols('a,b,c,x,y,z,p,q')
eqn=Eq(p**2+q**2,3)
print("The given PDE is :")
pprint(eqn)
eqn=eqn.subs([(p,a),(q,b)])
h=solve(eqn,b)
eqn=Eq(z,a*x+b*y+c)
eq1=eqn.subs(b,h[0])
print('The Required Solution is :')
pprint(eq1)

Output:
The given PDE is :
p2 + q2 = 3
The Required Solution is :

z = a⋅x + c - y. √3 − 𝑎2
Exercise:
1.Solve pq=1
Output:The given PDE is :
p⋅q = 1
The Required Solution is :
𝑦
z = a⋅x + c +
𝑎

2.Solve √𝑝 + √𝑞=1

Output:The given PDE is :


√p + √q = 1
The Required Solution is :
z = a⋅x + c + y⋅(√a - 1)2

3.Solve p=eq
Output: The given PDE is :
p=ℯq
The Required Solution is :
z = a⋅x + c + y⋅log(a)

4.Solve pq+p+q=0
Output:The given PDE is :
p⋅q + p + q = 0
The Required Solution is :
𝑎.𝑦
z=a.x - +𝑐
𝑎+1
Type-2 : Clariaut’s Equation
A first order PDE is said to be of Clariaut form if it can be written in the
form z=px+qy+f(p,q)

1.Solve px+qy+(p2+q2)
from sympy import *
x,y,p,q,a,b=symbols('x,y,p,q,a,b')
z=p*x+q*y+(p**2+q**2)
print('The given equation is z=',z)
soln=z.subs([(p,a),(q,b)])
print('The complete integral is z=',soln)

Output:
The given equation is z= p**2 + p*x + q**2 + q*y
The complete integral is z= a**2 + a*x + b**2 + b*y

2.Solve (px+qy-z)2=1+p2+q2
from sympy import *
x,y,z,p,q,a,b=symbols('x,y,z,p,q,a,b')
eq=Eq((p*x+q*y-z)**2,1+p**2+q**2)
print('The given equation is ')
pprint(eq)
soln=eq.subs([(p,a),(q,b)])
print('The complete integral is')
pprint(soln)
Output:
The given equation is
(p⋅x + q⋅y - z)2 = p2 + q2 + 1
The complete integral is (a⋅x + b⋅y - z)2 = a2 + b2 + 1

Exercise:
1.Solve z= px+qy+pq
Output:
The given equation is z= p*q + p*x + q*y
The complete integral is z= a*b + a*x + b*y

2.Solve z-px-qy=c√1 + 𝑝2 + 𝑞 2
Output:
The given equation is

-p⋅x + q⋅y + z = c⋅√𝑝2 + 𝑞 2 + 1


The complete integral is

-a⋅x + b⋅y + z = c⋅√𝑎2 + 𝑏 2 + 1

3.Solve z=px+qy+logpq
Output:
The given equation is z= p*x + q*y + log(p*q)
The complete integral is z=
a⋅x + b⋅y + log(a⋅b)
Lab.2-First Order non-linear Partial Differential Equations-Type-3 & Type-4
Type 3: Equations of the form f(p,q,z)=0

1.Solve p(1+q)=qz

from sympy import*


var('x,y,p,q,a,u')
z=Function('z')(u)
eq=Eq(p*(1+q),q*z)
print("The given PDE is :")
pprint(eq)
zu=z.diff(u)
eq=eq.subs([(p,zu),(q,a*zu)])
h=solve(eq,zu)
print("The required solution is :")
for i in range (0,len(h)):
soln=dsolve(Eq(zu,h[i]),z)
soln=soln.subs(u,x+a*y)
pprint(soln)

Output:
The given PDE is:
p⋅(q + 1) = q⋅z(u)
The required solution is :
1
z(a⋅y + x) = C₁⋅ea.y+x +
𝑎
Exercise:
1.Solve z= pq
Output: The given PDE is:
z(u) = p⋅q
The required solutions is:
𝑐1 2 −2.𝑐1 .(𝑎,𝑦+𝑥)+(𝑎.𝑦+𝑥)2
z(a⋅y + x) =
4.𝑎

2.Solve p+q=z/a
Output: The given PDE is :
𝑧(𝑢)
p+q=
𝑎

The required solutions is:


𝑎.𝑦+𝑥
z(a⋅y + x) = C₁⋅𝑒 𝑎.(𝑎+1)

3.Solve p(1+q2 )=q(z-a)


Output:
The given PDE is:
p.q2+1 = q⋅(-a + z(u))
The required solutions are:
𝑐1 2 .𝑎 𝑐1 (𝑎.𝑦+𝑥) (𝑎.𝑦+𝑥)2 1
z(a⋅y + x) = + +𝑎+ +
3 2 4.𝑎 𝑎

4.Solve p2=qz
Output:
The given PDE is:
p2 = q⋅z(u)
The required solutions are:

z(a⋅y + x) = C₁𝑒 𝑎.(𝑎.𝑦+𝑥)


Type 4: Equations of the form f1(p,x)=f2(q,y)

1.Solve p2-q2=x-y
from sympy import *
var('x,y,p,q,a,b,c')
eq=Eq(p**2-x,q**2-y)
print("The given equation is :")
pprint(eq)
p=solve(Eq(eq.lhs,a),p)
q=solve(Eq(eq.rhs,a),q)
z=integrate(p[0],x)+integrate(q[0],y)+c
print("The soln is z =")
pprint(z)

Output:
The given equation is :
p2 - x = q2 - y
The soln is z =
2.(𝑎+𝑥)3/2 2.(𝑎+𝑦)3/2
c- -
3 3
Exercise:

1.Solve √𝑝 + √𝑞 = 𝑥 + 𝑦

Output: The given equation is :


√p - x = -√q + y
The soln is z =
𝑥3 𝑦3
a2 ⋅x + a2 ⋅y + a⋅x 2 - a⋅y 2 + c + +
3 3

2.Solve p+q=sinx+siny
Output: The given equation is :
p - sin(x) = -q + sin(y)
The soln is z =
a⋅x - a⋅y + c - cos(x) - cos(y)

3.Solve pey=qex
Output:The given equation is :
p⋅ℯ-x = q⋅ℯ-y
The soln is z =
a⋅ℯx + a⋅ℯy + c

4.Solve p-x2=q+y2
Output:The given equation is :
p-x2=q+y2
The soln is z =
𝑥3 𝑦3
a⋅x + a⋅y + c + -
3 3
Lab3: Solving second order linear PDE in two variables with constant
coefficients.[Equation of the form F(D,D’)z=f(x,y)]
1.Solve (D2-3DD’-4D’2)z=e4x+y
from sympy import*
var('D,D1,x,y,m,c')
F=D**2-3*D*D1-4*D1**2
f=exp(4*x+y)
F=Poly(F.subs([(D,m),(D1,1)]))
r=F.all_roots(multiple=True)
m1=r[0]
m2=r[1]
print("The roots are=",m1,'and',m2)
f=f.subs(y,c-m1*x)
f=integrate(f,x)
f=f.subs(c,y+m1*x)
f=f.subs(y,c-m2*x)
f=integrate(f,x)
f=f.subs(c,y+m2*x)
PI=simplify(f)
print("Particular integral of given PDE is ")
pprint(PI)
print("The complete soln is")
if r[0]==r[1]:
pprint(f"f(y+{m1}x)+xg(y+{m2}x)+{PI}")
else:
pprint(f"f(y+{m1}x)+g(y+{m2}x)+{PI}")
Output:
The roots are= -1 and 4
Particular integral of given PDE is
𝑥𝑒 4.𝑥+𝑦
5

The complete soln is


f(y+-1x)+g(y+4x)+x*exp(4*x + y)/5

2. Solve (D2-DD’)z=sinxcos2y
from sympy import*
var('D,D1,x,y,m,c')
F=D**2-D*D1
f=sin(x)*cos(2*y)
F=Poly(F.subs([(D,m),(D1,1)]))
r=F.all_roots(multiple=True)
m1=r[0]
m2=r[1]
print("The roots are=",m1,'and',m2)
f=f.subs(y,c-m1*x)
f=integrate(f,x)
f=f.subs(c,y+m1*x)
f=f.subs(y,c-m2*x)
f=integrate(f,x)
f=f.subs(c,y+m2*x)
PI=simplify(f)
print("Particular integral of given PDE is")
pprint(PI)
print("The complete soln is")
if r[0]==r[1]:
pprint(f"f(y+{m1}x)+xg(y+{m2}x)+{PI}")
else:
pprint(f"f(y+{m1}x)+g(y+{m2}x)+{PI}")

Output:
The roots are= 0 and 1
Particular integral of given PDE is
sin(𝑥−2.𝑦) sin(𝑥+2.𝑦)
− +
6 2

The complete soln is


f(y+0x)+g(y+1x)+-sin(x - 2*y)/6 + sin(x + 2*y)/2

Exercise:
𝜕2 𝑧 𝜕2 𝑧 𝜕2 𝑧
1.Solve 2
−5 +4 = cos(2x+3y)
𝜕𝑥 𝜕𝑥𝜕𝑦 𝜕𝑦 2

Output:
The roots are= 1 and 4
Particular integral of given PDE is
cos(2.𝑥+2.𝑦)

10

The complete soln is


f(y+1x)+g(y+4x)+-cos(2*x + 3*y)/10
2.Solve (D2-2DD’+D’2)z=12xy
Output:
The roots are= 1 and 1
Particular integral of given PDE is
x3.(x+2.y)
The complete soln is
f(y+1x)+xg(y+1x)+x**3*(x + 2*y)

3.Solve (D2+3DD’+2D’2)z=x+y
Output:
The roots are= -2 and -1
Particular integral of given PDE is
𝑥 𝑦
x2.|- + |
3 3

The complete soln is


f(y+-2x)+g(y+-1x)+x**2*(-x/3 + y/2)
Lab4: Solution to One Dimensional Heat equation using Fourier series

𝜕𝑢 𝜕2 𝑢
1.Solve = 16 subject to the condition i)u(0,t)=0 , u(1,t)=0 ∀ t
𝜕𝑡 𝜕𝑥 2

ii)u(x,0)=x2-x ∀0≤𝑥 ≤1

from sympy import*


x,n,t=symbols('x,n,t',positive='True',real='True')
L=1
c=4
f=lambda x:x**2-x
bn=integrate(f(x)*sin(n*pi*x/L),[x,0,L])*2/L
s=Sum(bn*sin(n*pi*x/L)*exp(-n**2*pi**2*c**2*t/L**2),(n,1,5))
s=nsimplify(s).doit()
print("The solution up to n=5 terms")
pprint(s)

Output:
The solution up to n=5 terms
2 2 2
8𝑒 −16𝜋 .𝑡 . 𝑠𝑖𝑛𝜋. 𝑥 8𝑒 −144𝜋 .𝑡 . 𝑠𝑖𝑛3𝜋. 𝑥 8𝑒 −400𝜋 .𝑡 . 𝑠𝑖𝑛5𝜋. 𝑥
− − −
𝜋3 27𝜋 3 125𝜋 3
𝜕𝑢 𝜕2 𝑢
2.Solve = subject to the condition i)u(0,t)=0 , u(l,t)=0 ∀ t
𝜕𝑡 𝜕𝑥 2

ii)u(x,0)=(l-x )x

from sympy import*


x,n,t,L,c=symbols('x,n,t,L,c',positive='True',real='True')
f=lambda x:(L-x)*x
bn=integrate(f(x)*sin(n*pi*x/L),[x,0,L])*2/L
s=Sum(bn*sin(n*pi*x/L)*exp(-n**2*pi**2*c**2*t/L**2),(n,1,5))
s=nsimplify(s).doit()
print("The solution up to n=5 terms")
pprint(s)

Output:
𝜋2 .𝑐 2 .𝑡 2
9𝜋 .𝑐 .𝑡 2 25𝜋 .𝑐 .𝑡 2 2
3 − 𝐿2 𝜋. 𝑥 − 3𝜋. 𝑥 − 5𝜋. 𝑥
8𝐿 𝑒 . 𝑠𝑖𝑛 8𝐿3 𝑒 𝐿2 . 𝑠𝑖𝑛 8𝐿3 𝑒 𝐿2 . 𝑠𝑖𝑛
𝐿 + 𝐿 + 𝐿
𝜋 3 27𝜋 3 125𝜋 3
𝐿

Exercise:
𝜕𝑢 𝜕2 𝑢
1.Solve =4 subject to the condition i)u(0,t)=0 , u(1,t)=0 ∀ t
𝜕𝑡 𝜕𝑥 2

ii)u(x,0)=x-x2, ∀ 0 ≤ 𝑥 ≤ 1
Output:
The solution up to n=5 terms
2 2 2
8𝑒 −4𝜋 .𝑡 . 𝑠𝑖𝑛𝜋. 𝑥 8𝑒 −36𝜋 .𝑡 . 𝑠𝑖𝑛3𝜋. 𝑥 8𝑒 −100𝜋 .𝑡 . 𝑠𝑖𝑛5𝜋. 𝑥
− − −
𝜋3 27𝜋 3 125𝜋 3
Lab5: Solution to One Dimensional Wave equation using Fourier series

𝜕2 𝑢 𝜕2 𝑢
1.Solve = subject to the condition i)u(0,t)=0 , u(1,t)=0 ∀ t
𝜕𝑡 2 𝜕𝑥 2

𝜕𝑢
ii)u(x,0)=0 ,( ) = 𝑥 − 𝑥2 ∀0≤𝑥 ≤1
𝜕𝑡 𝑡=0

from sympy import*


x,n,L,t=symbols('x,n,L,t',positive='True',real='True')
c=1
L=1
f=lambda x:0
g=lambda x:x-x**2
bn=(2/L)*integrate(f(x)*sin(n*pi*x/L),[x,0,L])
cn=(2/n*pi*c)*integrate(g(x)*sin(n*pi*x/L),[x,0,L])
s=Sum(sin(n*pi*x/L)*(bn*cos(n*pi*c*t/L)+cn*sin(n*pi*c*t/L)),(n,1,5))
s=nsimplify(s).doit()
print("The solution up to n=5 terms")
pprint(s)

Output:
The solution up to n=5 terms
4.sin(𝜋.𝑡) sin(𝜋.𝑥) 4.sin(3.𝜋.𝑡)sin(3.𝜋.𝑥) 4.sin(5.𝜋.𝑡)sin(5.𝜋.𝑥)
2.𝜋. | + + |
𝜋3 81𝜋3 625𝜋3
𝜕2 𝑢 𝜕2 𝑢
2.Solve = subject to the condition i)u(0,t)=0 , u(𝜋,t)=0 ∀ t
𝜕𝑡 2 𝜕𝑥 2

𝜕𝑢
ii)u(x,0)=𝑥 ,( ) =0 ∀0≤𝑥 ≤𝜋
𝜕𝑡 𝑡=0

from sympy import*


x,n,L,t=symbols('x,n,L,t',positive='True',real='True')
c=1
L=pi
f=lambda x:x
g=lambda x:0
bn=(2/L)*integrate(f(x)*sin(n*pi*x/L),[x,0,L])
cn=(2/n*pi*c)*integrate(g(x)*sin(n*pi*x/L),[x,0,L])
s=Sum(sin(n*pi*x/L)*(bn*cos(n*pi*c*t/L)+cn*sin(n*pi*c*t/L)),(n,1,5))
s=nsimplify(s).doit()
print("The solution up to n=5 terms")
pprint(s)

Output:
The solution up to n=5 terms
2.sin(3.𝑥).cos(3.𝑡) .sin(4.𝑥).cos(4.𝑡)
2.sin(x).cos(t)-sin(2.x).cos(2.t)+ − +
3 2
2.sin(5.𝑥).cos(5.𝑡)
5
3.A string is stretched and fastened to points l apart.Motion is started by
displacing the string into the form y=k(lx-x2) from which it is released at
time t=0.Find the displacement of any point on the string at a distance of x
from one end at time t.

from sympy import*


x,n,L,k,c,t=symbols('x,n,L,k,c,t',positive='True',real='True')
f=lambda x:k*(L*x-x*x)
g=lambda x:0
bn=(2/L)*integrate(f(x)*sin(n*pi*x/L),[x,0,L])
cn=(2/n*pi*c)*integrate(g(x)*sin(n*pi*x/L),[x,0,L])
s=Sum(sin(n*pi*x/L)*(bn*cos(n*pi*c*t/L)+cn*sin(n*pi*c*t/L)),(n,1,5))
s=nsimplify(s).doit()
print("The solution up to n=5 terms")
pprint(s)

Output:
The solution up to n=5 terms
𝜋. 𝑥 𝜋. 𝑐. 𝑡 3. 𝜋. 𝑥 3. 𝜋. 𝑐. 𝑡 5. 𝜋. 𝑥 5. 𝜋. 𝑐. 𝑡
4𝐿3 𝑘. 𝑠𝑖𝑛 𝑐𝑜𝑠 4𝐿3 𝑘. 𝑠𝑖𝑛 𝑐𝑜𝑠 4𝐿3 𝑘. 𝑠𝑖𝑛 𝑐𝑜𝑠
2| 𝐿 𝐿 + 𝐿 𝐿 + 𝐿 𝐿 |
𝜋3 27. 𝜋 3 125𝜋 3

Exercise:
1.A lightly stretched string with fixed end points x=0 and x=l is initially ia a
𝜋.𝑥
position given by y=y0sin3( ).If it is released from rest in this position,find
𝑙
the displacement u(x,t).
Output:
The solution up to n=5 terms
𝜋. 𝑥 𝜋. 𝑐. 𝑡 3. 𝜋. 𝑥 3. 𝜋. 𝑐. 𝑡
3𝑦0 . 𝑠𝑖𝑛 𝑐𝑜𝑠 𝑦0 . 𝑠𝑖𝑛 𝑐𝑜𝑠
𝐿 𝐿 − 𝐿 𝐿
4 4
Lab6: Fourier Series
1.Find the Fourier series of 𝑓(𝑥) = 𝑥 2 𝑖𝑛 [ −𝜋, 𝜋]
from sympy import*
x=symbols('x')
L=pi
f=lambda x:x**2
print("Given function:",f(x))
f_series=fourier_series(f(x),(x,-L,L))
f_series=f_series.truncate(n=6)
print("f(x)=",f_series)
p=plot(f_series,f(x),(x,-2*L,2*L),legend=True)
p[0].line_color='green'
p[0].label='Fourier series'
p[1].line_color='red'
p[1].label=f(x)
Output:
Given function: x**2
f(x)= -4*cos(x) + cos(2*x) - 4*cos(3*x)/9 + cos(4*x)/4 - 4*cos(5*x)/25 + pi**2/3
Graph:

π
𝒙 𝒇𝒐𝒓 𝒙 > 𝟎
2.Find the Fourier series of f(x)=|x| ={ in [−𝝅, 𝝅]
−𝒙 𝒇𝒐𝒓𝒙 < 𝟎
from sympy import*
x=symbols('x')
L=pi
f=lambda x:Piecewise((-x,(x<0)),(x,(x>0)))
print("Given function:",f(x))
f_series=fourier_series(f(x),(x,-L,L))
f_series=f_series.truncate(n=6)
print("f(x)=",f_series)
p=plot(f_series,f(x),(x,-6*L,6*L),legend=True)
p[0].line_color='green'
p[0].label='Fourier series'
p[1].line_color='red'
p[1].label=f(x)
Output:
Given function: Piecewise((-x, x < 0), (x, x > 0))
f(x)= -4*cos(x)/pi - 4*cos(3*x)/(9*pi) - 4*cos(5*x)/(25*pi) - 4*cos(7*x)/(49*pi) -
4*cos(9*x)/(81*pi) + pi/2
Graph:
OR
from sympy import*
x=symbols('x')
L=pi
f=lambda x:abs(x)
print("Given function f(x) = :",f(x))
f_series=fourier_series(f(x),(x,-L,L))
f_series=f_series.truncate(n=6)
print("f(x)=",f_series)
p=plot(f_series,f(x),(x,-4*L,4*L),show=False,legend=True)
p[0].line_color='green'
p[0].label='Fourier series'
p[1].line_color='red'
p[1].label=f(x)
p.show()

Output:
Given function f(x) = : Abs(x)
f(x)= -4*cos(x)/pi - 4*cos(3*x)/(9*pi) - 4*cos(5*x)/(25*pi) - 4*cos(7*x)/(49*pi) -
4*cos(9*x)/(81*pi) + pi/2
Graph:

Exercise:
𝜋−𝑥
1.Find the fourier series of f(x) = in [0,2𝜋]
2

Output:
Given function: -x/2 + pi/2
f(x)= sin(x) + sin(2*x)/2 + sin(3*x)/3 + sin(4*x)/4 + sin(5*x)/5 + sin(6*x)/6

𝜋−𝑥 2
2.Find the fourier series of f(x) = ( ) in [0,2𝜋]
2

Output:
Given function: (-x/2 + pi/2)**2
f(x)= cos(x) + cos(2*x)/4 + cos(3*x)/9 + cos(4*x)/16 + cos(5*x)/25 + pi**2/12
Lab7: Half Range Fourier Series
1. .Find the half range cosine series of (𝑥) = 𝑥 𝑖𝑛 [0, 𝜋]

from sympy import*


from scipy.integrate import quad
x,n=symbols('x,n')
L=pi
f=lambda x:x
print("Given function f(x):",f(x))
def f1(x,n):
return f(x)*cos(n*pi*x/L)

a0=quad(f,0,L)[0]*2/L
f_series=round(a0/2,2)
for n in range(1,6):

an=round(2/L*quad(f1,0,L,args=(n))[0],2)
f_series=f_series+an*cos(n*pi*x/L)
print("Required half range cosine series :\n",f_series)
plot(f_series,f(x),(x,-4*L,4*L))

Output:
Given function f(x): x
Required half range cosine series :

-1.27*cos(x) - 0.14*cos(3*x) - 0.05*cos(5*x) + 1.57


Graph:

2.Find the half range cosine series of 𝑓(𝑥) = 𝑥 𝑖𝑛 [0, 𝜋] (Using


Integration Method)
from sympy import*
x,n=symbols('x,n')
L=pi
f=lambda x:x
print("Given function f(x):",f(x))
a0=(2/L)*integrate(f(x),[x,0,L])
f_series=a0/2
for n in range(1,6):
an=integrate(f(x)*cos(n*pi*x/L),[x,0,L])*2/L
f_series=f_series+an*cos(n*pi*x/L)
print("Required half range cosine series :\n",f_series)
plot(f_series,f(x),(x,-2*L,2*L),legend=True)
Output:
Given function f(x): x
Required half range cosine series :
-4*cos(x)/pi - 4*cos(3*x)/(9*pi) - 4*cos(5*x)/(25*pi) + pi/2
Graph:

3. Find the half range sine series of f(𝑥) = 𝑥2 𝑖𝑛 [0,𝜋]


from sympy import*
from scipy.integrate import quad
x,n=symbols('x,n')
L=pi
f=lambda x:x**2
print("Given function f(x):",f(x))
def f1(x,n):
return f(x)*sin(n*pi*x/L)
f_series=0
for n in range(1,6):
bn=round(2/L*quad(f1,0,L,args=(n))[0],2)
f_series=f_series+bn*sin(n*pi*x/L)
print("Required half range sine series :\n",f_series)
plot(f_series,f(x),(x,-4*L,4*L))
Output:
Given function f(x): x**2
Required half range sine series :
3.74*sin(x) - 3.14*sin(2*x) + 2.0*sin(3*x) - 1.57*sin(4*x) + 1.24*sin(5*x)
Graph:

Exercise:
1.Find the half range sine series of 𝑓(𝑥) = (𝑥 -1)2 𝑖𝑛 [0, 1]
Output:
Given function f(x): (x - 1)**2
Required half range sine series :
0.38*sin(pi*x) + 0.32*sin(2*pi*x) + 0.2*sin(3*pi*x) + 0.16*sin(4*pi*x) +
0.13*sin(5*pi*x)

2.Find the half range cosine series of 𝑓(𝑥) = sin(x) 𝑖𝑛 [0, 𝜋]


Output:
Given function f(x): sin(x)
Required half range cosine series :
-0.42*cos(2*x) - 0.08*cos(4*x) + 0.64
Lab-8 :LAPLACE TRANSFORMS
The Laplace transform(one of the integral transforms) has many
applications in science and engineering because it is a tool fro solving
differential equations.In particular, it transforms linear differential equations
into algebraic equations and convolution into multiplication.

Definition of Laplace Transform


Given f(t), t≥ 𝟎 its laplace transform is given by, F(s) = L[f(t)] =

∫𝟎 𝒆−𝒔𝒕 𝒇(𝒕)𝒅𝒕

Python code to find Laplace Transform of a function

1.from sympy import*


import math
from sympy.integrals import laplace_transform
from sympy.abc import t,s,a
f=input("Enter the function = ")
Ft=factor(f)
Fs=simplify(laplace_transform(Ft,t,s,noconds=True))
print(“The laplace transform of given function is: “,Fs)

Output:

Function Laplace Transform (Output)


a Enter the function = a
The laplace transform of given function is: a/s
Enter the function = exp(a*t)
eat The laplace transform of given function is: 1/(-a + s)
Enter the function = t^n
tn The laplace transform of given function is: gamma(n
+1)/(s*s**n)
Enter the function = sin(a*t)
sin (at) The laplace transform of given function is: a/(a**2 + s**2)
Enter the function = cos(a*t)
cos (at) The laplace transform of given function is: s/(a**2 + s**2)
Enter the function = sinh(a*t)
sinh (at) The laplace transform of given function is: a/(-a**2 + s**2)
Enter the function = cosh(a*t).rewrite(exp)
cosh (at) The laplace transform of given function is: s/(-a**2 + s**2)
Enter the function = a^t
The laplace transform of given function is:
at LaplaceTransform(a**t, t, s)
Enter the function = exp(log(a)*t)
The laplace transform of given function is: 1/(s - log(a))

Note 1:
If Laplace transform of function does not exist then python returns the
same function without evaluating it.

Note 2:
f(t)=tn
𝚪(𝐧+𝟏) 𝒏!
L[f(t)]= =
𝒔(𝒏+𝟏) 𝒔(𝒏+𝟏)

Note 3:
The function at should be re written as exp(log(a)*t) to get the required
Laplace Transform
Note 4:
rewrite() – A common way to deal special functions is to rewrite them in
terms of one another. This works for any function in sympy,not just special
functions.To rewrite an expression in terms of a function use
expr.rewrite(function)

Example:
>>>from sympy import*
>>> x=symbols('x')
>>> pprint(tan(x).rewrite(sin))

𝟐𝒔𝒊𝒏𝟐 (𝒙)
𝐬𝐢𝐧(𝟐𝒙)

>>>pprint(factorial(x).rewrite(gamma))
Γ(x + 1)

Note5:

apart() performs a partial fraction decomposition on a rational function

Example:

>>>from sympy import*


>>> x=symbols('x')
>>> expr=(4*x**3+21*x**2+10*x+12)/(x**4+5*x**3+5*x**2+4*x)
>>> pprint(expr)

𝟒𝒙𝟑 + 𝟐𝟏𝒙𝟐 + 𝟏𝟎𝒙 + 𝟏𝟐


𝒙𝟒 + 𝟓𝒙𝟑 + 𝟓𝒙𝟐 + 𝟒𝒙
>>>pprint(apart(expr))

𝟐𝒙 − 𝟏 𝟏 𝟑
− +
𝒙𝟐 + 𝒙 + 𝟏 𝒙 + 𝟒 𝒙

2. from sympy import*


import math
from sympy.integrals import laplace_transform
from sympy.abc import t,s,a
f=input("Enter the function = ")
Ft=factor(f)
Fs=apart(laplace_transform(Ft,t,s,noconds=True))
print("The Laplace transform of given function is : ",Fs)

Ser
ial Function [f(t)] (OUTPUT)Laplace Transform [F(s)]
No
.
Enter the function = (sin(3*t))**2
1 sin23t The Laplace transform of given function is : -s/(2*(s**2
+ 36)) + 1/(2*s)
Enter the function = cos(3*t)*cos(4*t)
2 cos 3t cos 4t The Laplace transform of given function is : s/(2*(s**2
+ 49)) + s/(2*(s**2 + 1))

3 3e2t + 2 cos 5t − 3 Enter the function = 3*exp(2*t)+2*cos(5*t)-3*sinh(2*t)


sinh 2t The Laplace transform of given function is : 2*s/(s**2 +
25) + 3/(2*(s + 2)) + 3/(2*(s - 2))
Enter the function = exp(-5*t)*(cos(t))^2
4 e5tcos2t
The Laplace transform of given function is : (s +
5)/(2*(s**2 + 10*s + 29)) + 1/(2*(s + 5))
Enter the function = t*sin(5*t)
5 t sin 5t The Laplace transform of given function is : 10*s/(s**2
+ 25)**2
Enter the function = t*exp(-t)*sin(t)
6 t e − t sin t The Laplace transform of given function is : 2*(s +
1)/(s**2 + 2*s + 2)**2

7 𝐬𝐢𝐧 𝒂𝒕 Enter the function = sin(a*t)/t


𝒕 The Laplace transform of given function is : atan(a/s)
Enter the function = exp(-3*t)*(2*cos(5*t)-3*sin(5*t))
8 e−3t(2 cos 5t − 3 sin The Laplace transform of given function is : (2*s -
5t) 9)/(s**2 + 6*s + 34)
Enter the function = exp(-3*t)*sinh(5*t)
9 e-3tsinh(5t) The Laplace transform of given function is : -1/(2*(s +
8)) + 1/(2*(s - 2))
Enter the function = cos(a*t)/t
10∗ 𝒄𝒐𝒔𝒂𝒕 The Laplace transform of given function is :
𝒕 LaplaceTransform(cos(a*t)/t, t, s)

𝒄𝒐𝒔 𝒂𝒕
Note : Since the Laplace transform of does not exists, Python returns
𝒕
the same input without evaluation
Lab 9-Inverse Laplace Transforms
If L[f(t)]=F(s) then L-1[F(s)]=f(t)

1. from sympy.integrals.transforms import inverse_laplace_transform


from sympy.abc import s,t,n,b
from sympy import exp,Symbol,factorial
a=Symbol("a",real=True)
t=Symbol('t',positive=True)
Fs=a/s
Ft=inverse_laplace_transform(Fs,s,t,noconds=True)
print("The given Inverse Laplace transform is :", Fs)
print("The required Laplace transform is :",Ft)

Note: Type the Inverse Laplace transform at Fs to get required L-1[F(s)] or f(t)
Output:

Serial F(s) f(t)=L-1[F(s)]


Number
1. 𝒂 𝒂
𝓛−𝟏 ( 𝒔 ) = 𝒂
𝒔
The given Inverse Laplace transform is : a/s

The required Laplace transform is : a

2. 𝟏 𝟏
𝓛−𝟏 ( ) = 𝒆𝒂𝒕
(𝒔 − 𝒂) (𝒔 − 𝒂)

The given Inverse Laplace transform is : 1/(-a + s)

The required Laplace transform is : exp(a*t)


3. 𝒏!
𝒏! 𝓛−𝟏 ( ) = 𝒕𝒏
𝒔
𝒔
The given Inverse Laplace transform is : s**(-n)*factorial(n)/s

The required Laplace transform is : t**n

4. 𝒂
𝓛−𝟏 ((𝒔𝟐 +𝒂𝟐 ))=sin at
𝒂
(𝒔𝟐 + 𝒂𝟐 ) The given Inverse Laplace transform is : a/(a**2+s**2)

The required Laplace transform is : sin(a*t)

𝒔
𝒔 𝓛−𝟏 ( ) = 𝒄𝒐𝒔 𝒂𝒕
5. (𝒔𝟐 + 𝒂𝟐 )
(𝒔𝟐 + 𝒂𝟐 )
The given Inverse Laplace transform is : s/(a**2+s**2)

The required Laplace transform is : cos(a*t)

𝒔
𝒔 𝓛−𝟏 ( 𝟐 ) = 𝐜𝐨𝐬𝐡 𝒂𝒕
6. (𝒔 − 𝒂𝟐 )
(𝒔𝟐 − 𝒂𝟐 )
The given Inverse Laplace transform is : s/(-a**2+s**2)

The required Laplace transform is : cosh(a*t)

𝟏
𝓛−𝟏 (𝒔(𝒔+𝟏)) = 𝟏 − 𝒆−𝒕
7. 𝟏
𝒔(𝒔 + 𝟏) The given Inverse Laplace transform is : 1/(s*(s+1))

The required Laplace transform is : 1- exp(-t)

𝟏 𝒆𝟒𝒕 𝒆𝟐𝒕 𝒆−𝟑𝒕


𝓛−𝟏 ((𝒔−𝟒)(𝒔−𝟐)(𝒔+𝟑)) = − +
𝟏 𝟏𝟒 𝟏𝟎 𝟑𝟓

8. (𝒔 − 𝟒)(𝒔 − 𝟐)(𝒔 + 𝟑) The given Inverse Laplace transform is :1/((s-4)*(s-2)*(s+3))

The required Laplace transform is : (5*exp(7*t)-7exp(5*t


𝟔
𝓛−𝟏 ( ) = 𝟑𝒆𝟑𝒕 𝐬𝐢𝐧(𝟐𝒕)
𝒔𝟐 −𝟔𝒔+𝟏𝟑

9. 𝟔 The given Inverse Laplace transform is : 6/(s**2 - 6*s + 13)


𝒔𝟐 − 𝟔𝒔 + 𝟏𝟑
The required Laplace transform is : 3*exp(3*t)*sin(2*t)

𝒔−𝟓
𝓛−𝟏 (𝒔𝟐 −𝟔𝒔+𝟏𝟑) =(-sin(2t)+cos(2t))𝒆𝟑𝒕
10. 𝒔−𝟓
𝒔𝟐 − 𝟔𝒔 + 𝟏𝟑 The given Inverse Laplace transform is : (s - 5)/(s**2 - 6*s + 13)

The required Laplace transform is : (-sin(2*t) + cos(2*t))*exp(3*t)

𝒔+𝟐 𝒕𝒆−𝟐𝒕 𝐬𝐢𝐧(𝒕)


𝓛−𝟏 ( 𝟐 )=
𝒔+𝟐 (𝒔𝟐 +𝟒𝒔+𝟓) 𝟐
11. 𝟐
(𝒔𝟐 +𝟒𝒔+𝟓)
The given Inverse Laplace transform is : (s + 2)/(s**2 + 4*s +
5)**2

The required Laplace transform is : t*exp(-2*t)*sin(t)/2

𝟐𝒆−𝒔
𝓛−𝟏 ( ) = (𝒕 − 𝟏)𝟐 𝜽(𝒕 − 𝟏)
𝟐𝒆 −𝒔 𝒔𝟑
12.
𝒔𝟑 The given Inverse Laplace transform is : 2*exp(-s)/s**3

The required Laplace transform is : (t**2 - 2*t + 1)*Heaviside(t -


1)

𝟏 𝒕 𝒔𝒊𝒏𝟑𝒕
𝓛−𝟏 (𝒔𝟐 (𝒔𝟐 +𝟗)) = 𝟗 −
𝟏 𝟐𝟕
𝒔𝟐 (𝒔𝟐 +𝟗)
13. The given Inverse Laplace transform is : 1/(s**2*(s**2 + 9))

The required Laplace transform is : t/9 - sin(3*t)/27

𝒔 𝐜𝐨𝐬(𝒕) 𝐜𝐨𝐬(𝟐𝒕)
𝓛−𝟏 ((𝒔𝟐 +𝟏)(𝒔𝟐 +𝟒)) = −
14. 𝒔 𝟑 𝟑

(𝒔𝟐 + 𝟏)(𝒔𝟐 + 𝟒) The given Inverse Laplace transform is : s/((s**2 + 1)*(s**2 + 4))

The required Laplace transform is : cos(t)/3 - cos(2*t)/3


𝟏 𝟏 𝐜𝐨𝐬(𝟐𝒕)
𝓛−𝟏 ( )= −
𝟏 𝒔(𝒔𝟐 +𝟒) 𝟒 𝟒
15.
𝟐
𝒔(𝒔 + 𝟒) The given Inverse Laplace transform is : 1/(s*(s**2 + 4))

The required Laplace transform is : sin(t)**2/2

𝟔𝒔−𝟓 𝟓√𝟕 𝐬𝐢𝐧(√𝟕𝒕)


𝓛−𝟏 ((𝒔𝟐 +𝟕)) = − +6cos(√𝟕𝒕)
𝟔𝒔−𝟓 𝟕
16.
(𝒔𝟐 +𝟕)
The given Inverse Laplace transform is : (6*s - 5)/(s**2 + 7)

The required Laplace transform is : -5*sqrt(7)*sin(sqrt(7)*t)/7 +


6*cos(sqrt(7)*t)
Lab-10 Solving ODE using Laplace transform with initial conditions
𝑑2𝑦
1.Solve by using Laplace Transforms + 4𝑦 = 0 given that y(0)=2, y’(0)=0
𝑑𝑡 2

import sympy as sp
t,s=sp.symbols('t,s',positive=True)
y=sp.Function('y')(s)
y0=2
y1=0
Fs=sp.Eq(s**2*y-s*y0-y1+4*y,0)
Fs=sp.apart(sp.solve(Fs,y))
print("L[y(t)]= ",Fs)
yt=sp.inverse_laplace_transform(Fs[0],s,t)
print("Solution y(t) is :",yt)

Output:
L[y(t)]= [2*s/(s**2 + 4)]
Solution y(t) is : 2*cos(2*t)

𝒅𝟐 𝒚 𝒅𝒚
2.Solve by using Laplace Transforms +𝟑 + 𝟐𝒚 = 𝟎 given that y(0)=1,
𝒅𝒕𝟐 𝒅𝒕
y’(0)=0

import sympy as sp
t,s=sp.symbols('t,s',positive=True)
y=sp.Function('y')(s)
y0=1
y1=0
Fs=sp.Eq(s**2*y-s*y0-y1+3*s*y-3*y0+2*y,0)
Fs=sp.apart(sp.solve(Fs,y))
print("L[y(t)]= ",Fs)
yt=sp.inverse_laplace_transform(Fs[0],s,t)
print("Solution y(t) is :",yt)

Output:
L[y(t)]= [-1/(s + 2) + 2/(s + 1)]
Solution y(t) is : (2*exp(t) - 1)*exp(-2*t)

𝒅𝒚
3. Solve by using Laplace Transforms + 𝒚 = 𝒕𝒆−𝒕 given that y(0)=0
𝒅𝒕

import sympy as sp
t,s=sp.symbols('t,s',positive=True)
y=sp.Function('y')(s)
y0=0
Fs=sp.Eq(s*y-y0+y,sp.laplace_transform(sp.exp(-
t)*t,t,s,noconds=True))
Fs=sp.solve(Fs,y)
Fs=sp.apart(Fs)
print("L[y(t)] =",Fs)
yt=sp.inverse_laplace_transform(Fs[0],s,t)
print("Solution y(t) is :",yt)
Output:
L[y(t)] = [(s + 1)**(-3)]
Solution y(t) is : t**2*exp(-t)/2

𝒅𝟐 𝒚 𝒅𝒚
4.Solve by using Laplace Transforms 𝟐 + 𝟐 − 𝟑𝒚 = 𝒔𝒊𝒏𝒕 given that
𝒅𝒕 𝒅𝒕
y(0)=0, y’(0)=0

import sympy as sp
t,s=sp.symbols('t,s',positive=True)
y=sp.Function('y')(s)
y0=0
y1=0
Fs=sp.Eq(s**2*y-s*y0-y1+2*s*y-2*y0-
3*y,sp.laplace_transform(sp.sin(t),t,s,noconds=True))
Fs=sp.solve(Fs,y)
Fs=sp.apart(Fs)
print("L[y(t)] =",Fs)
yt=sp.inverse_laplace_transform(Fs[0],s,t)
print("Solution y(t) is :",yt)

Output:
L[y(t)] = [-(s + 2)/(10*(s**2 + 1)) - 1/(40*(s + 3)) + 1/(8*(s - 1))]
Solution y(t) is : exp(t)/8 - sin(t)/5 - cos(t)/10 - exp(-3*t)/40
Exercise:
1. Solve by using Laplace Transforms y”+9y= 25e4t given that y(0)=3 ,
y’(0)=7

Output:
L[y(t)] = [(2*s + 3)/(s**2 + 9) + 1/(s - 4)]
Solution y(t) is : exp(4*t) + sin(3*t) + 2*cos(3*t)

2. Solve by using Laplace Transforms y”+y=6cos2t given that y(0)=3 ,


y’(0)=1

Output:
L[y(t)] = [-2*s/(s**2 + 4) + (5*s + 1)/(s**2 + 1)]
Solution y(t) is : sin(t) + 5*cos(t) - 2*cos(2*t)

3. Solve by using Laplace Transforms y”-y’-2y=20sin2t given that y(0)=-1 ,


y’(0)=2

Output:
L[y(t)] = [(s - 6)/(s**2 + 4) - 4/(s + 1) + 2/(s - 2)]
Solution y(t) is : 2*exp(2*t) - 3*sin(2*t) + cos(2*t) - 4*exp(-t)

You might also like