Uploaded by Ali Bilal

22K-4227+22K-4566 DE

advertisement
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
DE ASSIGNMENT # 2
Team members:
1) ALI BILAL 22K-4227
2) ALIZA ASHFAQ 22K-4566
Q1 part 1)
Code :
from sympy.interactive import printing
printing.init_printing(use_latex=True)
from sympy import *
import sympy as sp
from IPython.display import display, Math
x = sp.Symbol('x')
y1 = sp.Symbol('y_1')
y2 = sp.Symbol('y_2')
m = sp.Symbol('m')
W = sp.Symbol('W')
g = sp.Symbol('g')
F = sp.Symbol('F')
E = sp.Symbol('E')
D = sp.Symbol('D')
C = sp.Symbol('C')
B = sp.Symbol('B')
A = sp.Symbol('A')
y = sp.Symbol('y')
yp = sp.Symbol('y_p')
yc = sp.Symbol('y_c')
f = sp.Function('f')(x)
diffeq = Eq(f.diff(x, x) - 7 * f.diff(x) + 12 * f, exp(2 * x) * (x ** 3 - 5 *
x ** 2))
display(diffeq)
display(Eq(m ** 2 - 7 * m + 12, 0))
y_c = m ** 2 - 7 * m + 12
roots1 = sp.solve(y_c, m)
print(roots1)
print("\nYc:")
sol1 = dsolve(diffeq, f)
homogeneous_eq = Eq(f.diff(x, x) - 7 * f.diff(x) + 12 * f, 0)
display(homogeneous_eq)
complementary_solution = sp.dsolve(homogeneous_eq, f).rhs
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
display(Math(r"y_c=" + sp.latex(complementary_solution)))
y_p = exp(2 * x) * (A * x ** 3 + B * x ** 2 + C * x + D)
display(Math(r"y_p=" + sp.latex(y_p)))
y_p_prime = diff(y_p, x)
display(Math(r"y_p'=" + sp.latex(y_p_prime)))
y_p_prime_prime = diff(diff(y_p, x), x)
display(Math(r"y_p''=" + sp.latex(y_p_prime_prime)))
some_eq = y_p_prime_prime - 7 * y_p_prime + 12 * y_p
some_eq = simplify(some_eq)
Lefty_yp = Eq(some_eq, exp(2 * x) * (x ** 3 - 5 * x ** 2))
display(Lefty_yp)
display(Math(r"A = \frac{1}{2} \quad B = -\frac{1}{4} \quad C = -\frac{9}{4}
\quad D =
-\frac{25}{8}"))
print()
print("\nYp: ")
Lefty_yp = sol1.rhs.subs('C1', 0).subs('C2', 0)
display(Math(r"y_p=" + sp.latex(Lefty_yp)))
print("\nFinal Solution:")
display(sol1)
Output :
Q1 part 2)
Code :
from sympy.interactive import printing
printing.init_printing(use_latex=True)
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
from sympy import *
import sympy as sp
from IPython.display import display, Math
x = sp.Symbol('x')
from sympy.interactive import printing
printing.init_printing(use_latex=True)
from sympy import *
import sympy as sp
from IPython.display import display, Math
x = sp.Symbol('x')
y1 = sp.Symbol('y_1')
y2 = sp.Symbol('y_2')
m = sp.Symbol('m')
W = sp.Symbol('W')
g = sp.Symbol('g')
F = sp.Symbol('F')
E = sp.Symbol('E')
D = sp.Symbol('D')
C = sp.Symbol('C')
B = sp.Symbol('B')
A = sp.Symbol('A')
y = sp.Symbol('y')
yp = sp.Symbol('y_p')
yc = sp.Symbol('y_c')
f = sp.Function('f')(x)
diffeq1 = Eq(f.diff(x, x) + f.diff(x) - 2 * f, x ** 2 + 2 * sin(x) - exp(3 *
x))
display(diffeq1)
print("\nYc:")
y_c1 = Eq(m ** 2 + m - 2, 0)
display(y_c1)
print("\nFinding roots using sympy: ")
roots = solve(y_c1, m)
display(roots)
print("\nHence Yc is:")
sol2 = dsolve(diffeq1, f)
homogeneous_eq = Eq(f.diff(x, x) + f.diff(x) - 2 * f, 0)
complementary_solution = sp.dsolve(homogeneous_eq, f).rhs
display(Math(r"y_c=" + sp.latex(complementary_solution)))
y_p = A * x ** 2 + B * x + C + D * cos(x) + E * sin(x) + F * exp(3 * x)
display(Math(r"y_p=" + sp.latex(y_p)))
y_p_prime = diff(y_p, x)
display(Math(r"y_p'=" + sp.latex(y_p_prime)))
y_p_prime_prime = diff(diff(y_p, x), x)
display(Math(r"y_p''=" + sp.latex(y_p_prime_prime)))
some_eq = y_p_prime_prime + y_p_prime - 2 * y_p
some_eq = simplify(some_eq)
Lefty_yp = Eq(some_eq, exp(2 * x) * (x ** 3 - 5 * x ** 2))
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
display(Lefty_yp)
display(Math(
r"A = -\frac{1}{2} \quad B = -\frac{1}{2} \quad C = -\frac{3}{4} \quad D = \frac{1}{5} \quad E =
-\frac{3}{5} "
r"\quad F = -\frac{1}{10}"))
print()
Lefty_yp = sol2.rhs.subs('C1', 0).subs('C2', 0)
display(Math(r"y_p=" + sp.latex(Lefty_yp)))
print("\nFinal Solution:")
display(sol2)
Output :
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
Q1 part 3 )
Code :
from sympy.interactive import printing
printing.init_printing(use_latex=True)
from sympy import *
import sympy as sp
from IPython.display import display, Math
x = sp.Symbol('x')
y1 = sp.Symbol('y_1')
y2 = sp.Symbol('y_2')
m = sp.Symbol('m')
W = sp.Symbol('W')
g = sp.Symbol('g')
F = sp.Symbol('F')
E = sp.Symbol('E')
D = sp.Symbol('D')
C = sp.Symbol('C')
B = sp.Symbol('B')
A = sp.Symbol('A')
y = sp.Symbol('y')
yp = sp.Symbol('y_p')
yc = sp.Symbol('y_c')
f = sp.Function('f')(x)
diffeq2 = Eq(f.diff(x, x) - 2 * f.diff(x) + 2 * f, exp(2 * x) * (cos(x) - 3 *
sin(x)))
display(diffeq2)
print("\nYc: ")
y_c2 = Eq(m ** 2 - 2 * m + 2, 0)
display(y_c2)
roots = solve(y_c2, m)
display(roots)
sol2 = dsolve(diffeq2, f)
homogeneous_eq = Eq(f.diff(x, x) - 2 * f.diff(x) + 2 * f, 0)
complementary_solution = dsolve(homogeneous_eq, f).rhs
display(Math(r"y_c=" + latex(complementary_solution)))
print("\nYp:")
y_p = exp(2 * x) * (B * sin(x) + A * cos(x))
display(Math(r"y_p=" + latex(y_p)))
y_p_prime = diff(y_p, x)
display(Math(r"y_p'=" + latex(y_p_prime)))
y_p_prime_prime = diff(diff(y_p, x), x)
display(Math(r"y_p''=" + latex(y_p_prime_prime)))
some_eq = y_p_prime_prime - 2 * y_p_prime + 2 * y_p
some_eq = simplify(some_eq)
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
Lefty_yp = Eq(some_eq, exp(2 * x) * (cos(x) - 3 * sin(x)))
display(Lefty_yp)
display(Math(r"A = \frac{7}{5} \quad B = -\frac{1}{5}"))
Lefty_yp = sol2.rhs.subs('C1', 0).subs('C2', 0)
simplify(Lefty_yp)
display(Math(r"y_p=" + latex(Lefty_yp)))
Y = complementary_solution + Lefty_yp
display(Math("Y = " + latex(Y)))
display(sol2)
Output :
Question 2 part 1)
Code :
# Import necessary libraries
from IPython.display import display
from sympy import *
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
import sympy as sp
init_printing()
# Define symbols
x, C1, C2 = symbols('x C1 C2')
# Define the differential equation
y = Function('y')(x)
diff_eqn = Eq(y.diff(x, 2) + y, csc(x))
# Find the complementary function
cf = dsolve(diff_eqn.subs(csc(x), 0))
display(Eq(y, cf.rhs))
# Find the linearly independent solutions to the homogeneous equation
y1 = sin(x)
y2 = cos(x)
# Define the derivatives of the linearly independent solutions
y1p =sp.diff(y1,x)
y2p = sp.diff(y2,x)
# Define the matrix and its determinant
W = Matrix([[y1, y2],
[y1p, y2p]
])
wronskian = W.det()
display("Wronskian: ",wronskian)
# Find the particular solution
#u1, u2, u3 = symbols('u1 u2 u3')
f1= -(y2*csc(x))/wronskian
u1= sp.integrate(f1,x)
display("U1: ",u1)
f2= (y1*csc(x))/wronskian
u2= sp.integrate(f2,x)
display("U2: ",u2)
yp = simplify((u1*y1 + u2*y2 ))
# Find the general solution
sol = simplify(cf.rhs + yp)
display(Eq(y, sol))
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
Output :
Question 2 part 2)
Code:
# Import necessary libraries
from IPython.display import display
from sympy import *
import sympy as sp
init_printing()
# Define symbols
x, C1, C2, C3 = symbols('x C1 C2 C3')
# Define the differential equation
y = Function('y')(x)
diff_eqn = Eq(y.diff(x, 3) + y.diff(x), tan(x))
# Find the complementary function
cf = dsolve(diff_eqn.subs(tan(x), 0))
display(Eq(y, cf.rhs))
# Find the linearly independent solutions to the homogeneous equation
y1 = 1
y2 = sin(x)
y3 = cos(x)
# Define the derivatives of the linearly independent solutions
y1p = 0 # sp.diff(y1,x)
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
y2p = y2.diff(x)
y3p = y3.diff(x)
y1pp = 0
#sp.diff(y1,x, 2)
y2pp = y2.diff(x, 2)
y3pp = y3.diff(x, 2)
# Define the matrix and its determinant
W = Matrix([[y1, y2, y3],
[y1p, y2p, y3p],
[y1pp, y2pp, y3pp]])
wronskian = W.det()
display("Wronskian: ",wronskian)
# Find the particular solution
#u1, u2, u3 = symbols('u1 u2 u3')
w1= Matrix ([[0,y2,y3],
[0,y2p,y3p],
[tan(x),y2pp,y3pp]])
f1= w1.det()/wronskian
u1= sp.integrate(f1,x)
display("U1: ",u1)
w2= Matrix ([[y1,0,y3],
[y1p,0,y3p],
[y1pp,tan(x),y3pp]])
f2= w2.det()/wronskian
u2= sp.integrate(f2,x)
display("U2: ",u2)
w3= Matrix ([[y1,y2,0],
[y1p,y2p,0],
[y1pp,y2pp,tan(x)]])
f3= w3.det()/wronskian
u3= sp.integrate(f3,x)
display("U3: ",u3)
yp = simplify((u1*y1 + u2*y2 + u3*y3))
# Find the general solution
sol = simplify(cf.rhs + yp)
display(Eq(y, sol))
Output :
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
Question 2 part 3 )
Code:
# Import
necessary libraries
from IPython.display import display
from sympy import *
import sympy as sp
init_printing()
# Define symbols
x, C1, C2, C3 = symbols('x C1 C2 C3')
# Define the differential equation
y = Function('y')(x)
diff_eqn = Eq(y.diff(x, 3) + (4*y.diff(x)), sec(2*x))
# Find the complementary function
cf = dsolve(diff_eqn.subs(sec(2*x), 0))
display(Eq(y, cf.rhs))
# Find the linearly independent solutions to the homogeneous equation
y1 = 1
y2 = sin(2*x)
y3 = cos(2*x)
# Define the derivatives of the linearly independent solutions
y1p = sp.diff(y1,x)
y2p = y2.diff(x)
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
y3p = y3.diff(x)
y1pp = sp.diff(y1,x, 2)
y2pp = y2.diff(x, 2)
y3pp = y3.diff(x, 2)
# Define the matrix and its determinant
W = Matrix([[y1, y2, y3],
[y1p, y2p, y3p],
[y1pp, y2pp, y3pp]])
wronskian = W.det()
display("Wronskian: ",wronskian)
# Find the particular solution
#u1, u2, u3 = symbols('u1 u2 u3')
w1= Matrix ([[0,y2,y3],
[0,y2p,y3p],
[sec(2*x),y2pp,y3pp]])
f1= w1.det()/wronskian
u1= sp.integrate(f1,x)
display("U1: ",u1)
w2= Matrix ([[y1,0,y3],
[y1p,0,y3p],
[y1pp,sec(2*x),y3pp]])
f2= w2.det()/wronskian
u2= sp.integrate(f2,x)
display("U2: ",u2)
w3= Matrix ([[y1,y2,0],
[y1p,y2p,0],
[y1pp,y2pp,sec(2*x)]])
f3= w3.det()/wronskian
u3= sp.integrate(f3,x)
display("U3: ",u3)
yp = simplify((u1*y1 + u2*y2 + u3*y3))
# Find the general solution
sol = simplify(cf.rhs + yp)
display(Eq(y, sol))
Output :
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
Question 2 part 4)
Code :
# Import necessary libraries
from IPython.display import display
from sympy import *
import sympy as sp
init_printing()
# Define symbols
x, C1, C2, C3 = symbols('x C1 C2 C3')
# Define the differential equation
y = Function('y')(x)
diff_eqn = Eq(y.diff(x, 3) -(2*y.diff(x,2)) - y.diff(x) + 2*y, exp(4*x))
# Find the complementary function
cf = dsolve(diff_eqn.subs(exp(4*x), 0))
display(Eq(y, cf.rhs))
# Find the linearly independent solutions to the homogeneous equation
y1 = exp(-x)
y2 = exp(x)
y3 = exp(2*x)
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
# Define the derivatives of the linearly independent solutions
y1p = y1.diff(x)
y2p = y2.diff(x)
y3p = y3.diff(x)
y1pp = y1.diff(x, 2)
y2pp = y2.diff(x, 2)
y3pp = y3.diff(x, 2)
# Define the matrix and its determinant
W = Matrix([[y1, y2, y3],
[y1p, y2p, y3p],
[y1pp, y2pp, y3pp]])
wronskian = W.det()
display("Wronskian: ",wronskian)
# Find the particular solution
#u1, u2, u3 = symbols('u1 u2 u3')
w1= Matrix ([[0,y2,y3],
[0,y2p,y3p],
[exp(4*x),y2pp,y3pp]])
f1= w1.det()/wronskian
u1= sp.integrate(f1,x)
display("U1: ",u1)
w2= Matrix ([[y1,0,y3],
[y1p,0,y3p],
[y1pp,exp(4*x),y3pp]])
f2= w2.det()/wronskian
u2= sp.integrate(f2,x)
display("U2: ",u2)
w3= Matrix ([[y1,y2,0],
[y1p,y2p,0],
[y1pp,y2pp,exp(4*x)]])
f3= w3.det()/wronskian
u3= sp.integrate(f3,x)
display("U3: ",u3)
yp = simplify((u1*y1 + u2*y2 + u3*y3))
# Find the general solution
sol = simplify(cf.rhs + yp)
display(Eq(y, sol))
Output :
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
Question 2 part 5)
Code :
# Import
necessary libraries
from IPython.display import display
from sympy import *
import sympy as sp
init_printing()
# Define symbols
x, C1, C2, C3 = symbols('x C1 C2 C3')
# Define the differential equation
y = Function('y')(x)
diff_eqn = Eq(y.diff(x, 3) -(3*y.diff(x,2)) +(2*y.diff(x)),
(exp(2*x)/(1+exp(x))))
# Find the complementary function
cf = dsolve(diff_eqn.subs(exp(2*x)/(1+exp(x)), 0))
display(Eq(y, cf.rhs))
# Find the linearly independent solutions to the homogeneous equation
y1 = 1
y2 = exp(x)
y3 = exp(2*x)
# Define the derivatives of the linearly independent solutions
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
y1p = sp.diff(y1,x)
y2p = y2.diff(x)
y3p = y3.diff(x)
y1pp = sp.diff(y1,x, 2)
y2pp = y2.diff(x, 2)
y3pp = y3.diff(x, 2)
# Define the matrix and its determinant
W = Matrix([[y1, y2, y3],
[y1p, y2p, y3p],
[y1pp, y2pp, y3pp]])
wronskian = W.det()
display("Wronskian: ",wronskian)
# Find the particular solution
#u1, u2, u3 = symbols('u1 u2 u3')
w1= Matrix ([[0,y2,y3],
[0,y2p,y3p],
[(exp(2*x)/(1+exp(x))),y2pp,y3pp]])
f1= w1.det()/wronskian
u1= sp.integrate(f1,x) + 1/2
display("U1: ",u1)
w2= Matrix ([[y1,0,y3],
[y1p,0,y3p],
[y1pp,(exp(2*x)/(1+exp(x))),y3pp]])
f2= w2.det()/wronskian
u2= sp.integrate(f2,x)
display("U2: ",u2)
w3= Matrix ([[y1,y2,0],
[y1p,y2p,0],
[y1pp,y2pp,(exp(2*x)/(1+exp(x)))]])
f3= w3.det()/wronskian
u3= sp.integrate(f3,x)
display("U3: ",u3)
yp = simplify((u1*y1 + u2*y2 + u3*y3))
# Find the general solution
sol = simplify(cf.rhs + yp)
display(Eq(y, sol))
Output :
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
Question 3 part 1 )
Code :
from sympy.interactive import printing
import re
from IPython.display import display
printing.init_printing(use_latex=True)
from sympy import *
import sympy as sp
# Define the independent variable and the dependent function
x = sp.Symbol('x')
y = sp.Function('f')(x)
# Define the differential equation
diff_eqn = Eq(x**2 * y.diff(x, x) - 2*x*y.diff(x) + 2*y,x**4*sp.exp(x))
display("Equation:",diff_eqn)
m= sp.Symbol('m')
y=x**m
d1=m*x**(m-1)
d2=m*(m-1)*(x**(m-2))
display("SUBSTITUTIONS")
display("Y:",y)
display("y`:",d1)
display(" y``:",d2)
# doing the substitutions
sub_diff_eq= Eq(((d2*x**2 )- (2*x*d1)+(2*y) )/y,0)
sub_diff_eq=simplify(sub_diff_eq)
display("Simplified eqution after the substitutions:",sub_diff_eq)
roots=sp.solve(sub_diff_eq)
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
c1=sp.Symbol('c1')
c2=sp.Symbol('c2')
yc=c1*x**roots[0]+c2*x**roots[1]
display("YC:")
display(yc)
y1 = x
y2 = x**2
display("now finding u1 and u2 for yp by variations of parameters")
rhs = diff_eqn.rhs/y2
# Divide the right-hand side by y1 to match the variation of parameters method
# modify the differential equation by dividing it by x^2
diff_eqn = Eq(y.diff(x, x) - 2*y.diff(x)/x + 2*y/(x**2), x**2*sp.exp(x))
display("Right-hand Side:", rhs)
w = (y1 * y2.diff(x)) - (y2 * y1.diff(x))
display("Wronskian:", w)
# Find u1 and u2
u1 = -(rhs * y2) / w
u1 = u1.integrate(x)
u1 = sp.expand(u1)
display("u1:", u1)
u2 = (rhs * y1) / w
u2 = u2.integrate(x)
u2 = sp.expand(u2)
display("u2:", u2)
# display the general solution
display("General Solution:",yc)
# Find the particular solution
yp = (u1 * y1) + (u2 * y2)
display("Particular Solution:", yp)
# Find the full solutionsol = yc + yp
display("Full Solution:",sol)
Output :
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
Question 3 part 2)
Code :
from sympy.interactive import printing
import re
from IPython.display import display
printing.init_printing(use_latex=True)
from sympy import *
import sympy as sp
# Define the independent variable and the dependent function
x = sp.Symbol('x')
y = sp.Function('f')(x)
# Define the differential equation
diff_eqn = Eq(x**2 * y.diff(x, x) + x*y.diff(x) -y,1/(1+x))
display("Equation:",diff_eqn)
m= sp.Symbol('m')
y=x**m
d1=m*x**(m-1)
d2=m*(m-1)*(x**(m-2))
display("SUBSTITUTIONS")
display("Y:",y)
display("y`:",d1)
display(" y``:",d2)
# doing the substitutions
sub_diff_eq=Eq(((d2*x**2)+(d1*x)-y)/y,0)
sub_diff_eq=simplify(sub_diff_eq)
display("Simplified eqution after the substitutions:",sub_diff_eq)
roots=sp.solve(sub_diff_eq)
c1=sp.Symbol('c1')
c2=sp.Symbol('c2')
yc=c1*x**roots[0]+c2*x**roots[1]
display("YC:")
display(yc)
y1 = 1/x
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
y2 = x
display("now finding u1 and u2 for yp by variations of parameters")
rhs = diff_eqn.rhs/x**2
# Divide the right-hand side by x**2 to match the variation of parameters
method
# modify the differential equation by dividing it by x^2
diff_eqn = Eq(y.diff(x, x) + y.diff(x)/x -y/(x**2), 1/((1+x)*(x**2)))
display("Right-hand Side:", rhs)
w = (y1 * y2.diff(x)) - (y2 * y1.diff(x))
display("Wronskian:", w)
# Find u1 and u2
u1 = -(rhs * y2) / w
u1 = u1.integrate(x)
u1 = sp.expand(u1)
display("u1:", u1)
u2 = (rhs * y1) / w
u2 = u2.integrate(x)
u2 = sp.expand(u2)
display("u2:", u2)
# display the general solution
display("General Solution:",yc)
# Find the particular solution
yp = (u1 * y1) + (u2 * y2)
display("Particular Solution:", yp)
# Find the full solution
sol = yc + yp
display("Full Solution:",sol)
Output :
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
22K4227 + 22K-4566
Ali Bilal + Aliza Ashfaq
Download