2301520 Fundamentals of AMCS
Differential Equations

Vector Field Plots

Vector Field Plots

VectorPlot[{-1-x^2+y,1+x-y^2},{x,-3,3},{y,-3,3}]

StreamPlot[{-1-x^2+y,1+x-y^2},{x,-3,3},{y,-3,3}]

Differential Equations

1. First-Order Ordinary Differntial Equations (ODEs)

1.1. First-Order ODE General Solution (Without Initial Conditions)

Clear[x,y]

sol1=DSolve[y'[x]-y[x]-Exp[-x]==0, y,x]

y'[x]-y[x]-Exp[-x]==0 /. sol1[[1]] (* Verifying *)

f1[x_]:=y[x]/. sol1[[1]] /. C[1]-> -10

f1[z]

f1[x_,k_]:=y[x]/. sol1[[1]] /. C[1]-> k

f1[x,k]

tab=Table[f1[x,k], {k, -80, 80, 40}]

Plot[Evaluate[tab], {x, 0, 7}, PlotLegends->{"k=-80", "k=-40", "k=0", "k=40", "k=80"}]

plot1=Plot[f1[x,0.5], {x, 0, 3},PlotStyle-> Red]

splot=StreamPlot[{1,y+Exp[-x]},{x,0,7},{y,-10,10}]

Show[plot1,splot]

vplot=VectorPlot[{1,y+Exp[-x]},{x,0,7},{y,-10,10}, VectorScale->{Tiny,Scaled[0.4],None}]

Show[plot1,vplot]

1.2. First-Order ODE Particular Solution (With Initial Conditions)

sol2=DSolve[{y'[x]-y[x]-Exp[-x]==0,y[2]==-0.1}, y,x]

y'[x]-y[x]-Exp[-x]==0 /. sol2[[1]]

y'[x]-y[x]-Exp[-x] /. sol2[[1]]

Simplify[%]

f2[x_]:=y[x]/. sol2[[1]]

f2[z]

plot2=Plot[f2[x], {x, 0, 7}, PlotStyle->Red]

Show[plot2,splot]

Show[plot2,vplot]

DSolve[{y'[x]==y[x]Cos[x+y[x]],y[0]==1},y,x]

sol3=NDSolve[{y'[x]==y[x]Cos[x+y[x]],y[0]==1},y,{x,0,30}]

f3[x_]:=y[x]/. sol3[[1]]

plot3=Plot[f3[x],{x,0,30},PlotRange->All, PlotStyle-> Red]

splot3=StreamPlot[{1,y Cos[x+y]},{x,0,30},{y,0,1.5}]

Show[plot3,splot3]

vplot3=VectorPlot[{1,y Cos[x+y]},{x,0,30},{y,0,1.5}, VectorScale->{Tiny,Scaled[1],None}]

Show[plot3,vplot3]

2. Second Order ODEs

2.1. Second-Order ODE General Solution (Without Initial Conditions)

sol4=DSolve[y''[x]-4 y[x]==0, y,x]

y''[x]-4 y[x]==0/. sol4[[1]]

y''[x]-4 y[x]/. sol4[[1]]

Simplify[%]

f4[x_,k1_, k2_]:=y[x]/. sol4[[1]] /. {C[1]-> k1, C[2]-> k2}

f4[x,k1, k2]

Plot[{f4[x,k1=2, k2=3],f4[x, k1=1, k2=2]}, {x, 0, 7}, PlotLegends->{"(Subscript[k, 1])=2, (Subscript[k, 2])=3", " (Subscript[k, 1])=1, (Subscript[k, 2])=2"}]

2.2. Second-Order ODE Particular Solution (With Initial Conditions)

sol5=DSolve[{y''[x]-4 y[x]==0,   y[2]==1, y'[3]==0 }, y,x]

y''[x]-4 y[x]/. sol5[[1]]

y[2] /. sol5[[1]]

Simplify[%]

y'[3]/. sol5[[1]]

f5[x_]:=y[x]/. sol5[[1]]

f5[x]

Plot[f5[x], {x, -2, 7}]

sol6=DSolve[{y''[x]-4 y[x]==0,   y[1]==-1, y[3]==2 }, y,x]

y''[x]-4 y[x]/. sol6[[1]]

Simplify[%]

y[1]/. sol6[[1]]

Simplify[%]

y[3]/. sol6[[1]]

Simplify[%]

f6[x_]:=y[x]/. sol6[[1]]

f6[x]

Plot[f6[x], {x, 0, 4}]

3 Partial Differential Equations (PDEs)

sol7=DSolve[(∂_x) u[x,y]+x (∂_y) u[x,y] == Sin[x], u,{x,y}]

(∂_x) u[x,y]+x (∂_y) u[x,y] == Sin[x] /. sol7[[1]]

f7[x_,y_]:= u[x,y]/. sol7[[1]] /.C[1][a_]-> Sin[a]

f7[x,y]

Plot3D[f7[x,y], {x,-2,2}, {y,-7,7}, PlotPoints -> 30]

4 Linear System of ODEs

sol1=DSolve[{x1'[t]==x2[t],x2'[t]==-x1[t]/2+5 x2[t]/2}, {x1,x2},t]

f11[t_]:=Simplify[x1[t]/. sol1[[1]] ]

f11[t]

f21[t_]:=Simplify[x2[t]/. sol1[[1]] ]

f21[t]

A ={{0,1},{-1/2,5/2}}

MatrixForm[A]

X[t_]:={x1[t],x2[t]}

system = X'[t] == A.X[t]

sol2=DSolve[system, {x1,x2}, t]

f12[t_]:=Simplify[x1[t]/. sol2[[1]] ]

f12[t]

f22[t_]:=Simplify[x2[t]/. sol2[[1]] ]

f22[t]

solc=Solve[{f11[3]==6,f21[3]==-1},{C[1],C[2]}]

y[t_]:=Simplify[f11[t]/. solc[[1]]]

y[t] (* This is in fact the particular solution of the original equation *)

y[3]

dy[t_]:= Simplify[f21[t] /. solc[[1]]]

dy[t]

dy[3]

Simplify[D[y[t],t]]

ddy[t_]:=Simplify[Evaluate[D[y[t],{t,2}]]]

ddy[t]

Simplify[2 ddy[t]-5 dy[t]+y[t]]

5 Model with ODEs

popsol=DSolve[n'[t]==k n[t], n,t]

f1[t_]:=n[t]/. popsol[[1]]

f1[t]

solc1=Solve[f1[0]==42,C[1], Reals]

f2[t_]:=f1[t] /. solc1[[1]]

f2[t]

solk=Solve[f2[15]==56, k, Reals]

y[t_]:=f2[t] /. solk[[1]]

y[t]

N[y[45]]