WEEK 4 Program
Aim: To find the general solution of non-exact differential equation.
Algorithm:
1 Start the program and input the differential equation
𝑀(𝑥, 𝑦)𝑑𝑥 + 𝑁(𝑥, 𝑦)𝑑𝑦 = 0.
!" !$
2 Check exactness: !# = !%
i. If exact, solve as exact DE.
ii. If non exact, continue.
3 Find an integrating factor 𝐼𝐹 such that 𝐼𝐹 ∗ (𝑀𝑑𝑥 + 𝑁𝑑𝑦) = 0 becomes
exact.
4 Multiply the DE by the integrating factor to get 𝑀& 𝑑𝑥 + 𝑁& 𝑑𝑦 = 0.
5 Solve the resulting exact differential equation using the standard method:
6 Compute 𝐹 = ∫ 𝑀& 𝑑𝑥
!'
7 Compute 𝑔 = ∫ 1𝑁& − !#3 𝑑𝑦.
8 Form the general solution: 𝐹 + 𝑔 = 𝑐 and print it.
9 Display solution and stop the program
Program:
import sympy as sp
import math
x, y, c = [Link]('x y c')
M = [Link](input("Enter M(x,y): "))
N = [Link](input("Enter N(x,y): "))
dM_dy = [Link](M, y)
print("dM_dy=")
[Link](dM_dy)
dN_dx = [Link](N, x)
print("dN_dx=")
[Link](dN_dx)
if dM_dy != dN_dx:
print("\nGiven equation is non exact")
f_x = [Link]((dM_dy - dN_dx)/N)
f_y = [Link]((dN_dx - dM_dy)/M)
IntFact=None
if not f_x.has(y):
IntFact = [Link]([Link](f_x, x))
print("\nIntegrating factor is =")
[Link](IntFact)
elif not f_y.has(x):
IntFact = [Link]([Link](f_y, y))
print("\nIntegrating factor is =")
[Link](IntFact)
else:
print("No simple integrating factor of type f(x) or
g(y) are possible ")
if IntFact is not None:
M1 = [Link](IntFact*M)
N1 = [Link](IntFact*N)
F = [Link](M1, x)
g = [Link](N1 - [Link](F, y), y)
print("\nGeneral Solution is:")
[Link]([Link](F+g, c))
else:
print("Equation is already exact")
Input: 1
Enter M(x,y): 2*x*y + y
Enter N(x,y): x
Output:
dM_dy=
2⋅x + 1
dN_dx=
1
Given equation is non exact
Integrating factor is =
2⋅x
ℯ
General Solution is:
2⋅x
x⋅y⋅ℯ = c
Input: 2
Enter M(x,y)= 3*x**2*y**4 + 2*x*y
Enter M(x,y)= 2*x**3*y**3-x**2
Output:
dM_dy=
12⋅x2 ⋅y3 + 2⋅x
dN_dx=
6⋅x2 ⋅y3 - 2⋅x
Given equation is non exact
Integrating factor is =
1
──
y2
General Solution is:
x2 ⋅(x⋅y3 + 1)
───────────── = c
y
Input: 3
Enter M(x,y)= -x**2-y**2-1
Enter N(x,y)= 2*x*y
Output:
dM_dy=
-2⋅y
dN_dx=
2⋅y
Given equation is non exact
Integrating factor is =
1
──
X2
General Solution is:
- x2 + y2 + 1
─────────────── = c
x
Input: 4
Enter M(x,y)= y**4+2*y
Enter N(x,y)= x*y**3+2*y**4-4*x
Output:
dM_dy=
4⋅y3 + 2
dN_dx=
y3 - 4
Given equation is non exact
Integrating factor is =
1
──
y3
General Solution is:
x⋅(y3 + 2) + y4
─────────────── = c
y