PDE PRACTICALS Mathematica
PDE PRACTICALS Mathematica
SHIVANI
7347
22055563007
BSC.(H) MATHEMATICS 5TH SEMESTER
PRACTICALS : PARTIAL DIFFERENTIAL EQUATIONS
SYLLABUS:
4. Solution of vibrating string problem using D’Alembert formula with initial conditions.
7. Solution of traffic flow problem with given initial conditions , and plotting of the
characteristic base curves and the traffic density function .
PRACTICAL - 1
General solution of first and second order PDE.
In[27]:= eqn1 = x D[u[x, y], x] + y D[u[x, y], y] 0;
DSolve[eqn1, u[x, y], {x, y}]
Out[28]=
y
u[x, y] 1
x
In[35]:= eqn5 = D[u[x, y], {x, 2}] - D[u[x, y], {y, 2}] 0;
DSolve[eqn5, u[x, y], {x, y}]
Out[36]=
{{u[x, y] 1 [-x + y] + 2 [x + y]}}
In[37]:= eqn7 = d[u[x, y], {x, 2}] + D[u[x, y], {y, 2}] 6 * x + 2;
DSolve[eqn7, u[x, y], {x, y}]
Out[38]=
u[x,y] 1
Solve K[2]2 (y + 2 [x])2 ,
1 K[2]
1 [x] + 2 ∫1 (2 + 6 x - d[K[1], {x, 2}]) K[1]
{u[x, y]}
In[39]:= ClearAll[x, y, u]
In[40]:= eqn4 = D[u[x, y], {x, 2}] - 4 D[u[x, y], x, y] + 3 D[u[x, y], {y, 2}] 0;
DSolve[eqn4, u[x, y], {x, y}]
Out[41]=
{{u[x, y] 1 [3 x + y] + 2 [x + y]}}
In[42]:= eqn8 = D[u[x, t], {t, 2}] 4 D[u[x, t], {x, 2}];
DSolve[eqn8, u[x, t], {x, t}]
Out[43]=
x x
u[x, t] 1 t - + 2 t +
2 2
PRACTICAL - 2
To obtain the solution of the Cauchy problem for 1st order PDEs
and to plot them
4
x2 + y
In[55]:= Plot3D , {x, -4, 4}, {y, -4, 4}
y
Out[55]=
In[56]:= ClearAll[x, y, u]
2
In[61]:= Plot3D- , {x, -1, 1}, {y, -1, 1}
-2 + x + y
Out[61]=
In[62]:= ClearAll[x, y, u]
1
In[67]:= Plot3D , {x, -1, 1}, {y, -1, 1}
-y + Coth[x - y]
Out[67]=
In[68]:= ClearAll[x, y, u]
In[74]:= ClearAll[x, y, u]
1
u[x, y] - 2 x y + 1 Log-x2 + 2 x y + y2 ,
2
1
u[x, y] 2 x y + 1 Log-x2 + 2 x y + y2
2
2 2
u[x, y] - 2 x2 + 3 x y - 2 y2 , u[x, y] 2 x2 + 3 x y - 2 y2 ,
7 7
2 2
u[x, y] - 2 x2 + 3 x y - 2 y2 , u[x, y] 2 x2 + 3 x y - 2 y2
7 7
O u t [ ] =
2 2
u[x, y] - 2 x2 + 3 x y - 2 y2 , u[x, y] 2 x2 + 3 x y - 2 y2 ,
7 7
2 2
u[x, y] - 2 x2 + 3 x y - 2 y2 , u[x, y] 2 x2 + 3 x y - 2 y2
7 7
6 7347_prac_2[1].nb
2 2
In[79]:= Plot3D- 2 x2 + 3 x y - 2 y 2 , 2 x2 + 3 x y - 2 y2 , {x, -3, 3}, {y, -10, 10}
7 7
Out[79]=
In[80]:= ClearAll[x, y, u]
Q1. Ux - Uy = 1
Characteristic eqn : dx/1 = dy/-1 = du/1
In[107]:=
DSolve[u '[x] 1, u[x], x]
Out[107]=
{{u[x] x + 1 }}
In[108]:=
DSolve[u '[y] -1, u[y], y]
Out[108]=
{{u[y] -y + 1 }}
In[109]:=
g1 = Plot3D[{x, x - 1, x + 1}, {x, -3, 3}, {y, -3, 3}]
g2 = Plot3D[{-y, -y + 1, -y - 1}, {x, -3, 3}, {y, -3, 3}]
Out[109]=
Out[110]=
2 Shivani_7347_prac-_3[1].nb
In[111]:=
Show[g1, g2]
Out[111]=
In[112]:=
ClearAll[x, y, u]
In[114]:=
DSolve[u[x] * u '[x] 1, u[x], x]
Out[114]=
u[x] - 2 x + 1 , u[x] 2 x + 1
Shivani_7347_prac-_3[1].nb 3
In[115]:=
g1 = Plot3D[{y, y + 1, y - 1}, {x, -3, 3}, {y, -3, 3}]
g2 = Plot3D- 2 x, - 2 x+1, - 2 x - 1 , {x, -3, 3}, {y, -3, 3}
g3 = Plot3D 2 x, 2 x+1, 2 x - 1 , {x, -3, 3}, {y, -3, 3}
Out[115]=
Out[116]=
Out[117]=
4 Shivani_7347_prac-_3[1].nb
In[118]:=
Show[g1, g2, g3]
Out[118]=
Q3. Ux + Uy = 0
Characteristic eqn : dx/1 = dy/1 = du/0
In[119]:=
ClearAll[x, y, u]
In[120]:=
DSolve[u '[x] 0, u[x], x]
Out[120]=
{{u[x] 1 }}
In[121]:=
DSolve[y '[x] 1, y[x], x]
Out[121]=
{{y[x] x + 1 }}
Shivani_7347_prac-_3[1].nb 5
In[122]:=
g1 = Plot3D[{-1, 0, 1}, {x, -3, 3}, {y, -3, 3}]
g2 = Plot3D[{x, x + 1, x - 1}, {x, -3, 3}, {y, -3,
3}]
Out[122]=
Out[123]=
6 Shivani_7347_prac-_3[1].nb
In[124]:=
Show[g1, g2]
Out[124]=
In[126]:=
DSolve[u '[x] (x + y) / (u[x] - y), u[x], x]
Out[126]=
u[x] y - x2 + 2 x y + y2 - 2 1 , u[x] y + x2 + 2 x y + y2 - 2 1
In[127]:=
p1 = Plot3Dy - x2 + 2 x y + y 2 - 2 , y - x2 + 2 x y + y 2 - 2 , y - x2 + 2 x y + y2 + 2 ,
In[129]:=
p2 = Plot3Dy + x2 + 2 x y + y 2 - 2 , y + x2 + 2 x y + y 2 - 2 , y + x2 + 2 x y + y2 + 2 ,
In[130]:=
DSolve[y * t '[y] t[y], t[y], y]
Out[130]=
{{t[y] y 1 }}
In[131]:=
p3 = Plot3D[{-x, y - x, -y - x}, {x, -3, 3}, {y, -3, 3}]
Out[131]=
8 Shivani_7347_prac-_3[1].nb
In[132]:=
Show[p1, p2, p3]
Out[132]=
Que5. y Ux + x Uy = u
Characteristic eqn : dx/y = dy/x = du/u
In[133]:=
ClearAll[x, y, u]
In[134]:=
DSolve[y '[x] x / y[x], y[x], x]
Out[134]=
y[x] - x2 + 2 1 , y[x] x2 + 2 1
In[135]:=
DSolve[u '[v] u[v] / v, u[v], v]
Out[135]=
{{u[v] v 1 }}
Shivani_7347_prac-_3[1].nb 9
In[136]:=
In[137]:=
In[138]:=
a3 = Plot3D[{0, x + y, -x - y}, {x, -3, 3}, {y, -3, 3}]
Out[138]=
In[139]:=
Show[a1, a2, a3]
Out[139]=
PRACTICAL - 4
Solution of vibrating string problem using D’Alembert formula with initial
condition
Print[u[x, t]]
1
(-Sin[t - x] + Sin[t + x])
2
In[209]:=
h0 = Plot[Evaluate[u[x, 0]], {x, -6, 6}]
Out[209]=
1.0
0.5
-6 -4 -2 2 4 6
-0.5
-1.0
In[210]:=
h1 = Plot[Evaluate[u[x, 1]], {x, -6, 6}]
Out[210]=
0.6
0.4
0.2
-6 -4 -2 2 4 6
-0.2
-0.4
-0.6
2 SHIVANI_7347_PRAC_-_4[1].nb
In[211]:=
h2 = Plot[Evaluate[u[x, 2]], {x, -6, 6}]
Out[211]=
0.4
0.2
-6 -4 -2 2 4 6
-0.2
-0.4
In[212]:=
Show[h0, h1, h2, PlotStyle {Red}]
Out[212]=
1.0
0.5
-6 -4 -2 2 4 6
-0.5
-1.0
In[213]:=
Plot3D[u[x, t], {x, -3, 3}, {t, 0, 3}]
Out[213]=
Print[u[x, t]]
1 2 2
--(-2 t+x) Sin[2 t - x] + -(2 t+x) Sin[2 t + x]
2
In[219]:=
h0 = Plot[Evaluate[u[x, 0]], {x, -6, 6}]
Out[219]=
0.04
0.02
-6 -4 -2 2 4 6
-0.02
-0.04
In[220]:=
h1 = Plot[Evaluate[u[x, 1]], {x, -6, 6}]
Out[220]=
0.2
0.1
-6 -4 -2 2 4 6
-0.1
-0.2
4 SHIVANI_7347_PRAC_-_4[1].nb
In[221]:=
h3 = Plot[Evaluate[u[x, 2]], {x, -6, 6}]
Out[221]=
0.2
0.1
-6 -4 -2 2 4 6
-0.1
-0.2
In[222]:=
Show[h0, h1, h2, PlotStyle {Red}]
Out[222]=
0.04
0.02
-6 -4 -2 2 4 6
-0.02
-0.04
In[223]:=
Plot3D[u[x, t], {x, -3, 3}, {t, 0, 3}]
Out[223]=
= 1, -1≤x≤1
= 0 , x>1
ut(x,0) = Sinx
In[224]:=
√
c = 2;
f[x_] := Piecewise[{{0, x < -1}, {1, -1 ≤ x ≤ 1}, {0, x > 1}}]
g[x_] := Sin[x]
1 1 x+c t
u[x_, t_] := * (f[x + c t] + f[x - c t]) + * g[s] s
2 2c x-c t
Print[u[x, t]]
1.0
0.8
0.6
Wave at t=0
0.4
0.2
-6 -4 -2 2 4 6
In[230]:=
h1 = Plot[Evaluate[u[x, 1]], {x, -6, 6}, PlotLegends "Wave at t=1", PlotStyle Red]
Out[230]=
1.0
0.5
Wave at t=1
-6 -4 -2 2 4 6
-0.5
6 SHIVANI_7347_PRAC_-_4[1].nb
In[231]:=
h2 = Plot[Evaluate[u[x, 2]], {x, -6, 6}, PlotLegends "Wave at t=2", PlotStyle Green]
Out[231]=
0.6
0.4
Wave at t=2
0.2
-6 -4 -2 2 4 6
-0.2
In[232]:=
Show[h0, h1, h2]
Out[232]=
1.0
0.8
0.6
Wave at t=0
Wave at t=1
0.4 Wave at t=2
0.2
-6 -4 -2 2 4 6
Q4.Utt = 2Uxx
u(x,0) = Sinx
Ut(x,0) = Cosx
In[233]:=
√
c = 2;
f[x_] := Sin[x]
g[x_] := Cos[x]
In[236]:=
1 1 x+c t
u[x_, t_] := * (f[x + c t] + f[x - c t]) + * g[s] s
2 2c x-c t
Print[u[x, t]]
Cos[x] Sin 2 t 1
+ -Sin 2 t - x + Sin 2 t + x
2 2
Cos[x] Sin 2 t 1
+ -Sin 2 t - x + Sin 2 t + x
2 2
SHIVANI_7347_PRAC_-_4[1].nb 7
In[238]:=
h0 = Plot[Evaluate[u[x, 0]], {x, -6, 6}, PlotLegends "Wave at t=0"]
Out[238]=
1.0
0.5
-6 -4 -2 2 4 6
Wave at t=0
-0.5
-1.0
In[239]:=
h1 = Plot[Evaluate[u[x, 1]], {x, -6, 6}, PlotLegends "Wave at t=1", PlotStyle Red]
Out[239]=
0.6
0.4
0.2
-6 -4 -2 2 4 6
Wave at t=1
-0.2
-0.4
-0.6
In[240]:=
h2 = Plot[Evaluate[u[x, 2]], {x, -6, 6}, PlotLegends "Wave at t=2", PlotStyle Green]
Out[240]=
1.0
0.5
-6 -4 -2 2 4 6
Wave at t=2
-0.5
-1.0
8 SHIVANI_7347_PRAC_-_4[1].nb
In[241]:=
Show[h0, h1, h2]
Out[241]=
1.0
0.5
Wave at t=0
Wave at t=1
-6 -4 -2 2 4 6
Wave at t=2
-0.5
-1.0
Print[u[x, t]]
1
Erf π t - x + Erf π t + x
4
In[247]:=
h0 = Plot[Evaluate[u[x, 0]], {x, -6, 6}, PlotLegends "Wave at t=0", PlotStyle Blue]
Out[247]=
1.0
0.5
-6 -4 -2 2 4 6
Wave at t=0
-0.5
-1.0
SHIVANI_7347_PRAC_-_4[1].nb 9
In[248]:=
h1 = Plot[Evaluate[u[x, 1]], {x, -6, 6}, PlotLegends "Wave at t=1", PlotStyle Red]
Out[248]=
0.5
0.4
0.3
Wave at t=1
0.2
0.1
-6 -4 -2 2 4 6
In[249]:=
h2 = Plot[Evaluate[u[x, 2]], {x, -6, 6}, PlotLegends "Wave at t=2", PlotStyle Green]
Out[249]=
0.5
0.4
0.3
Wave at t=2
0.2
0.1
-6 -4 -2 2 4 6
In[250]:=
Show[h0, h1, h2]
Out[250]=
1.0
0.5
Wave at t=0
Wave at t=1
-6 -4 -2 2 4 6
Wave at t=2
-0.5
-1.0
PRACTICAL - 5
Solution of the heat equation Ut = KUxx with the given
initial condition.
Que1. Ut - kUxx = 0 , 0 < x < 5 , t > 0 , u (x, 0) = 0, u (0, t) = sin t , u (5, t) = 0 , k = 1
I n [ ] : = K = 1;
pde = D[u[x, t], t] - k D[u[x, t], {x, 2}] 0;
DSolve[{pde, u[x, 0] 0, u[0, t] Sin[t], u[5, t] 0}, u[x, t], {x, t}]
O u t [ ] =
1
u[x, t] - (-5 + x) Sin[t] +
5
1
- π2 t 1[1]2 1
∞ 50 25 π2 1[1]2 - 50 π2 1[1]2 Cos[t] - 1250 Sin[t] Sin π x 1[1]
5
1[1]=1 625 π 1[1] + π5 1[1]5
I n [ ] : = k = 1;
0 < x < 5;
pde = D[u[x, t], t] k D[u[x, t], {x, 2}];
s=
NDSolve[{pde, u[x, 0] 0, u[0, t] Sin[t], u[5, t] 0}, u[x, t], {x, 0, 5}, {t, 0, 5}]
Plot3D[Evaluate[u[x, t] /. s], {x, 0, 5}, {t, 0, 5}]
O u t [ ] =
O u t [ ] =
I n [ ] : = k = 1;
pde2 = D[u[x, t], t] k * D[u[x, t], {x, 2}];
sol2 = NDSolve[
{pde2, u[x, 0] Sin[x], u[0, t] 0, u[π, t] 0}, u[x, t], {x, 0, π}, {t, 0, 5}]
Plot3D[Evaluate[u[x, t] /. sol], {x, -5, 5}, {t, 0, 5}]
O u t [ ] =
NDSolve: Warning: an insufficient number of boundary conditions have been specified for the direction of independent
variable x. Artificial boundary effects may be present in the solution.
O u t [ ] =
I n [ ] : = ClearAll[x, y, u, t]
2 Shivani_7347_prac_-_6[1].nb
In[141]:=
c = 2;
pde2 = D[u[x, t], {t, 2}] - c2 * D[u[x, t], {x, 2}] 0 ;
sol2 = DSolvepde2, u[x, 0] 10 * x4 , Derivative[0, 1][u][x, 0] 0, u[x, t], {x, t}
Out[143]=
1
10 (-2 t + x)4 + 10 (2 t + x)4 t ≥ 0
u[x, t] 2
Indeterminate True
I n [ ] : = ClearAll[x, y, u, t]
I n [ ] : = c=3
pde3 = D[u[x, t], {t, 2}] - c2 * D[u[x, t], {x, 2}] 0 ;
sol3 = DSolvepde2, u[x, 0] 0, Derivative[0, 1][u][x, 0] x 3 , u[x, t], {x, t}
O u t [ ] =
3
O u t [ ] =
1 1 1
- (-2 t + x)4 + (2 t + x)4 t ≥ 0
u[x, t] 4 4 4
Indeterminate True
I n [ ] : = ClearAll[x, y, u, t]
I n [ ] : = c=1
pde4 = D[u[x, t], {t, 2}] - c2 * D[u[x, t], {x, 2}] 0 ;
sol4 = DSolvepde4, u[0, t] 0, u[1, t] 0,
u[x, 0] 10 x2 * 1 - x2 , Derivative[0, 1][u][x, 0] 0, u[x, t], {x, t}
O u t [ ] =
1
O u t [ ] =
u[x, t]
∞ 480 -1 + (-1)1[1] - 40 1 + 5 (-1)1[1] π2 1[1]2 Cos[π t 1[1]] Sin[π x 1[1]]
1[1]=1 π5 1[1]5
12
10
1 2 3 4
In[93]:= ClearAll;
10
1 2 3 4
-2
-4
In[96]:= f[x_] := Piecewise[{{0, x < 0}, {4 x, 0 < x < 1}, {0, x > 1}}];
shivani 7347 prac - 7[1].nb 3
In[99]:= Plot3D[u[x, t] /. sol2, {x, -1, 3}, {t, 0, 1}, AxesLabel Automatic]
Out[99]=
In[100]:=
ClearAll
Out[100]=
ClearAll
In[101]:=
sol3 = DSolve[t '[x] 1 / 2, t[x], x]
Out[101]=
x
t[x] + 1
2
In[102]:=
x x x
Plot , + 1, + 2, {x, -3, 3}
2 2 2
Out[102]=
-3 -2 -1 1 2 3
-1
1 x<0
with the initial condition u(x,0) = 1 - x 0 < x < 1,
0 x≤1
plotting initial condition
In[103]:=
sol1 = DSolve[u '[t] 0, u[t], t]
Out[103]=
{{u[t] 1 }}
In[104]:=
1
sol2 = DSolvet '[x] , t[x], x
2c
Out[104]=
x
t[x] + 1
2c
In[105]:=
Plot[{x / 2, x / 2 + 1, x / 2 - 1}, {x, -6, 6}]
Out[105]=
-6 -4 -2 2 4 6
-2
-4
In[106]:=
f[x_] := Piecewise[{{1, x < 0}, {1 - x, 0 < x < 1}, {0, x > 1}}];
In[107]:=
Plot[{Piecewise[{{1, x < 0}, {1 - x, 0 < x < 1}, {0, x > 1}}]}, {x, -5, 5}]
Out[107]=
1.0
0.8
0.6
0.4
0.2
-4 -2 2 4
In[108]:=
eqn3 = D[u[x, t], t] -2 D[u[x, t], x];
shivani 7347 prac - 7[1].nb 5
In[109]:=
sol3 = DSolve[{eqn3, u[x, 0] f[x]}, u[x, t], {x, t}]
Out[109]=
x
1 -2 t - < 0
2
u[x, t] x
1 + 2 t - x 0 < -2 t - < 1
2
0 True
In[110]:=
Plot3D[u[x, t] /. sol3, {x, -1, 3}, {t, 0, 1}, AxesLabel Automatic]
Out[110]=
ASSIGNMENT
Q1. Find the general solution of the following partial differential equations
a) Uxx - 4Uxy +3Uyy = 0
In[16]:= DSolve[D[u[x, y], {x, 2}] - 4 D[u[x, y], x, y] + 3 D[u[x, y], {y, 2}] 0, u[x, y], {x, y}]
Out[16]=
{{u[x, y] 1 [3 x + y] + 2 [x + y]}}
b) xUx + yUy = 2u
In[17]:= DSolve[x D[u[x, y], x] + y D[u[x, y], y] 2 u[x, y], u[x, y], {x, y}]
Out[17]=
y
u[x, y] x2 1
x
In[18]:= DSolve[{y D[u[x, y], x] - 2 x y D[u[x, y], y] 2 x u[x, y], u[0, y] y ^ 3}, u[x, y], {x, y}]
Out[18]=
4
x2 + y
u[x, y]
y
4
x2 + y
In[19]:= Plot3D , {x, -5, 5}, {y, -5, 5}
y
Out[19]=
In[25]:= ClearAll[x, y, u, t]
4 SHIVANI 7347 ASSIGNMENT[1].nb
In[26]:= k = 1;
0 < x < 2;
pde1 = D[u[x, t], t] k D[u[x, t], {x, 2}];
s = NDSolve[{pde1, u[x, 0] x, u[0, t] 0, Derivative[1, 0][u][2, t] 1},
u[x, t], {x, 0, 2}, {t, 0, 3}]
Plot3D[Evaluate[u[x, t] /. s], {x, 0, 2}, {t, 0, 3}]
Out[29]=
Out[30]=
In[31]:=