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

Assign. No 1

The document provides the solutions to three systems of ordinary differential equations. For each system, it gives the initial conditions, defines the function to calculate derivatives, runs the integration, plots the results, and displays the output. The solutions involve numerical integration of the systems over a time range.

Uploaded by

getachewbonga09
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)
31 views7 pages

Assign. No 1

The document provides the solutions to three systems of ordinary differential equations. For each system, it gives the initial conditions, defines the function to calculate derivatives, runs the integration, plots the results, and displays the output. The solutions involve numerical integration of the systems over a time range.

Uploaded by

getachewbonga09
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

Assign.

no 1

February 17, 2024

1.Find the general solution of the system of ordinary differential equations


𝑑𝑥
= 𝐴𝑥 + 𝑏(𝑡)
𝑑𝑡
, where:
3 −2 𝑒𝑡
(𝑎) 𝐴=[ ] 𝑏 = [ 𝑡]
4, −1 𝑒
−2 −5 − cos(𝑡)
(𝑏) 𝐴=[ ] 𝑏=[ ]
1, −2 sin(𝑡)
1 1 1 2𝑡
(𝑐) 𝐴=⎡
⎢0 2 1 ⎥
⎤ 𝑏=⎡
⎢𝑡 + 2⎤

⎣0 0 3 ⎦ ⎣ 3𝑡 ⎦

[122]: import numpy as np


from scipy.integrate import odeint
import matplotlib.pyplot as plt

[125]: # Initialization
t = np.arange(-1, 1, 0.1)
x0 = [1,1]

[126]: # Function that returns dx/dt


def mydiff(x, t):
dx1dt = 3*x[0]-2*x[1]+np.exp(t)
dx2dt = 4*x[0]-x[1]+np.exp(t)
dxdt = [dx1dt,dx2dt]
return dxdt

[127]: x = odeint(mydiff, x0, t)


print(x)

[[ 1. 1. ]
[ 1.12352754 1.34714329]
[ 1.21247492 1.70584637]
[ 1.25428314 2.05983867]
[ 1.23621097 2.38960601]
[ 1.14599686 2.67275842]

1
[ 0.97264108 2.88463929]
[ 0.70729128 2.99919516]
[ 0.34420598 2.99011668]
[-0.11824024 2.83224768]
[-0.67655584 2.50324429]
[-1.32119189 1.98544815]
[-2.03572726 1.26791816]
[-2.79625223 0.34854396]
[-3.57102841 -0.76385701]
[-4.32050496 -2.04757771]
[-4.99776886 -3.46633293]
[-5.54950084 -4.96799714]
[-5.91749599 -6.48393233]
[-6.04079036 -7.92907321]]

[128]: x1 = x[:,0]
x2 = x[:,1]

[129]: # Plot the Results


plt.plot(t,x[:,0])
plt.plot(t,x[:,1])
plt.title('Simulation with 2 variables')
plt.xlabel('t')
plt.ylabel('x(t)')
plt.grid()
plt.axis([-1, 1, -1.5, 4])
plt.legend(["x1", "x2"])
plt.show()

2
(b). solution

[130]: # Initialization
x0 = [1,1]
t = np.arange(-1, 2, 0.1)

[131]: # Function that returns dx/dt


def mydiff(x, t):
dx1dt = 2*x[0]-5*x[1]-np.cos(t)
dx2dt = x[0]-2*x[1]-2*np.sin(t)
dxdt = [dx1dt,dx2dt]
return dxdt

[132]: x = odeint(mydiff, x0, t)


print(x)

[[ 1. 1. ]
[ 0.59067516 1.03823168]
[ 0.07701463 1.0161916 ]
[-0.53010405 0.93448524]

3
[-1.21794337 0.79480899]
[-1.97209981 0.59992438]
[-2.77672305 0.35361682]
[-3.61475739 0.06063946]
[-4.46820223 -0.27335726]
[-5.31838816 -0.64190923]
[-6.14626483 -1.03783454]
[-6.93269673 -1.45334018]
[-7.65876297 -1.88013823]
[-8.30605683 -2.30957004]
[-8.85698111 -2.73273661]
[-9.29503532 -3.14063363]
[-9.60509009 -3.52428902]
[-9.77364598 -3.87490164]
[-9.78907228 -4.18397877]
[-9.6418226 -4.44347091]
[-9.32462408 -4.64590171]
[-8.83263744 -4.78449149]
[-8.16358531 -4.85327253]
[-7.3178469 -4.84719443]
[-6.29851722 -4.76221824]
[-5.11142962 -4.59539774]
[-3.76514103 -4.3449468 ]
[-2.27087947 -4.01029173]
[-0.64245397 -3.59210772]
[ 1.10387197 -3.09233883]]

[133]: x1 = x[:,0]
x2 = x[:,1]

[134]: # Plot the Results


plt.plot(t,x1)
plt.plot(t,x2)
plt.title('Simulation with 2 variables')
plt.xlabel('t')
plt.ylabel('x(t)')
plt.grid()
plt.axis([-1, 1, -1.5, 2])
plt.legend(["x1", "x2"])
plt.show()

4
[135]: # Initialization
x0 = [0, 3, 1]
t = np.arange(-1, 2, 0.1)

[136]: # Function that returns dx/dt


def mydiff(x, t):
dx1dt = x[0]+x[1]+x[2]+2*t
dx2dt = 2*x[1]+x[2]+t+2
dx3dt = x[2]+3*t
dxdt = [dx1dt,dx2dt,dx3dt]
return dxdt

[137]: x = odeint(mydiff, x0, t)


print(x)

[[ 0.00000000e+00 3.00000000e+00 1.00000000e+00]


[ 2.54585556e-01 3.88044016e+00 8.05170920e-01]
[ 6.38882288e-01 4.94589609e+00 6.21402761e-01]
[ 1.18932244e+00 6.23861649e+00 4.49858810e-01]
[ 1.95121564e+00 7.81033914e+00 2.91824701e-01]
[ 2.98079983e+00 9.72440620e+00 1.48721274e-01]

5
[ 4.34775510e+00 1.20583491e+01 2.21188044e-02]
[ 6.13828386e+00 1.49070474e+01 -8.62472882e-02]
[ 8.45888443e+00 1.83865891e+01 -1.74459067e-01]
[ 1.14409716e+01 2.26389872e+01 -2.40396884e-01]
[ 1.52465339e+01 2.78379431e+01 -2.81718166e-01]
[ 2.00750585e+01 3.41958886e+01 -2.95833970e-01]
[ 2.61720048e+01 4.19725894e+01 -2.79883070e-01]
[ 3.38391731e+01 5.14856565e+01 -2.30703324e-01]
[ 4.34473884e+01 6.31233883e+01 -1.44800024e-01]
[ 5.54520147e+01 7.73604601e+01 -1.83109199e-02]
[ 7.04119281e+01 9.47770902e+01 1.53032435e-01]
[ 8.90127181e+01 1.16082455e+02 3.73947404e-01]
[ 1.12095056e+02 1.42143293e+02 6.49647478e-01]
[ 1.40689375e+02 1.74018847e+02 9.85894457e-01]
[ 1.76058268e+02 2.13003548e+02 1.38905612e+00]
[ 2.19748310e+02 2.60679160e+02 1.86616993e+00]
[ 2.73653400e+02 3.18978468e+02 2.42501352e+00]
[ 3.40092176e+02 3.90263088e+02 3.07418248e+00]
[ 4.21902622e+02 4.77418504e+02 3.82317640e+00]
[ 5.22557685e+02 5.83970155e+02 4.68249399e+00]
[ 6.46306555e+02 7.14225245e+02 5.66373806e+00]
[ 7.98347294e+02 8.73445953e+02 6.77973176e+00]
[ 9.85037773e+02 1.06806101e+03 8.04464681e+00]
[ 1.21415340e+03 1.30592412e+03 9.47414541e+00]]

[138]: x1 = x[:,0]
x2 = x[:,1]
x3 = x[:,2]

[139]: # Plot the Results


plt.plot(t,x1)
plt.plot(t,x2)
plt.plot(t,x3)
plt.title('Simulation with 2 variables')
plt.xlabel('t')
plt.ylabel('x(t)')
plt.grid()
plt.axis([-1, 4, 1, 8])
plt.legend(["x1", "x2", "x3"])
plt.show()

6
7

You might also like