Professional Documents
Culture Documents
Mathematica Sample Questions
Mathematica Sample Questions
Michael Devasia
Solution of Cauchy problem for first order PDE and plot the integral surfaces.
i) 3*u_x + 2*u_y = 0; u(x,0) = sin(x)
In[28]:= ClearAll ;
pde1 = 3 * D[u[x, y], x] + 2 * D[u[x, y], y]
sol1 = DSolve [{pde1 == 0, u[x, 0] == Sin [x]}, u[x, y], {x, y}]
1
Out[30]= u[x, y] → Sin (2 x - 3 y)
2
Out[31]=
In[36]:= ClearAll ;
pde1 = x * D[u[x, y], x] + y * D[u[x, y], y] - 2 * x * y
sol1 = DSolve [{pde1 == 0, u[x, x ^ 2] == 2}, u[x, y], {x, y}]
0,1 1,0
Out[37]= -2 x y + y u [x, y] + x u [x, y]
2 x3 + x4 y - y3
Out[38]= u[x, y] →
x3
Out[39]=
In[40]:= ClearAll ;
pde1 = D[u[x, y], x] + x * D[u[x, y], y] - y
sol1 = DSolve [{pde1 == 0, u[0, y] == y ^ 2}, u[x, y], {x, y}]
0,1 1,0
Out[41]= -y + x u [x, y] + u [x, y]
1
u[x, y] → - 4 x + 3 x + 12 x y - 12 x y + 12 y
3 4 2 2
Out[42]=
12
3
Out[43]=
In[44]:= ClearAll ;
pde1 = D[u[x, y], x] + x * D[u[x, y], y] - y
sol1 = DSolve [{pde1 == 0, u[1, y] == 2 * y}, u[x, y], {x, y}]
0,1 1,0
Out[45]= -y + x u [x, y] + u [x, y]
1
u[x, y] → 5 - 3 x - 2 x + 6 y + 6 x y
2 3
Out[46]=
6
Out[47]=
4
In[11]:= Manipulate [Plot3D [a * x, {x, - 5, 5}, {y, - 5, 5}], {{a, 0.5 }, - 1, 1}]
Manipulate [Plot3D [b * y, {x, - 5, 5}, {y, - 5, 5}], {{b, 0.5 }, - 1, 1}]
Out[11]=
5
Out[12]=
ii) (u-y)*u_x+y*u_y=x+y
Characteristic Equation is dx/(u-y)=dy/y=du/(x+y)
Characteristic Curves are u(x,y)=ay-x and u(x,y)=±sqrt(x^2+2xy+y^2+b), where a and b are constants.
In[13]:= Manipulate [Plot3D [a * y - x, {x, - 5, 5}, {y, - 5, 5}], {{a, 0.5 }, - 1, 1}]
Manipulate [Plot3D [{Sqrt [x ^ 2 + 2 * x * y + y ^ 2 + 2 * b], - Sqrt [x ^ 2 + 2 * x * y + y ^ 2 + 2 * b]},
{ x, - 5, 5} , { y, - 5, 5}], {{ b, 0.5 } , - 1, 1}]
6
Out[13]=
7
Out[14]=
iii) 3*u_x+4*u_y=x
Characteristic Equation is dx/3=dy/4=du/x
Characterisctic Curve is 4x-3y=12a and u(x,y)=(x^2-2b)/6, where a and b are constants.
In[15]:= Manipulate [Plot3D [4 * x - 3 * y = 12 * a, {x, - 5, 5}, {y, - 5, 5}], {{a, 0.5 }, - 1, 1}]
Manipulate [Plot3D [(x ^ 2 + 2 * b) / 6, {x, - 5, 5}, {y, - 5, 5}], {{b, 0.5 }, - 1, 1}]
8
Out[15]=
9
Out[16]=
iv) 4*u_y-x*u_x=1
Characteristic Equation is dx/(-x)=dy/4=du/1
Characteristic Curve is u(x,y)=a-ln(x) and u(x,y)=y/4+b, where a and b are constants.
In[17]:= Manipulate [Plot3D [a - Log [x], {x, 0.1 , 5}, {y, - 5, 5}], {{a, 0.5 }, - 1, 1}]
Manipulate [Plot3D [y / 4 + b, {x, - 5, 5}, {y, - 5, 5}], {{b, 0.5 }, - 1, 1}]
10
Out[17]=
11
Out[18]=
In[4]:= ClearAll ;
f[x_ ] := Sin [x]; g[x_ ] := Cos [x];
5) Solution of one dimensional heat equation, u_t = k.u_xx, for a homogeneous rod of length l, i.e, solve
the IBVP u_t = k.u_xx
u(x,0) = f(x), u(0,t) = 0, u(l,t) = 0; 0<x<l, t>0
In[6]:= Plot3D [Sol1 , {x, 0.1 , 1}, {t, 1, 2}, AxesLabel → {"t", "x", ""}]
Out[6]=
In[10]:= Plot3D [Sol2 , {x, 0.1 , 1}, {t, 1, 2}, AxesLabel → {"t", "x", ""}]
Out[10]=
In[20]:= Plot3D [Sol3 , {x, 0.1 , 1}, {t, 1, 2}, AxesLabel → {"t", "x", ""}]
Out[20]=
In[21]:= k = 1; l = 2; f[x_ ] = x;
eqns4 = {D[u[x, t], t] - k * D[u[x, t], {x, 2}] == 0, u[x, 0] == f[x], u[0, t] == 0, u[l, t] == 1};
Sol4 = u[x, t] / . NDSolve [eqns4 , u[x, t], {x, 0, 1}, {t, 0, 4}]
In[24]:= Plot3D [Sol4 , {x, 0.1 , 1}, {t, 1, 2}, AxesLabel → {"t", "x", ""}]
Out[24]=
In[2]:= sol1 = DSolve [{y '[x] == 3 * y[x], z '[x] == 2 * y[x] + 3 * z[x]}, {y[x], z[x]}, x]
y[x] → ⅇ 1 , z[ x] → 2 ⅇ x 1 + ⅇ 3 x 2
3x 3x
Out[2]=
In[3]:= Plot [{y[x] / . {sol1 / . C[1] → 1}, z[x] / . sol1 / . {C[1] → 1, C[2] → 4}}, {x, - 5, 5}]
140 000
120 000
100 000
80 000
Out[3]=
60 000
40 000
20 000
-4 -2 2 4
ii)
x''[t]=-5x+2y
y''[t]=-2y+2x
16
In[69]:= sol2 =
DSolve [{x '' [t] == 5 * x[t] + 2 * y[t], y '' [t] == 2 * y[t] + 2 * x[t]}, {x[t], y[t]}, t] // FullSimplify
Out[69]= x[t] →
1
3 ( 1 - 2 3 ) Cosh [t] + 6 (2 1 + 3 ) Cosh 6 t + 3 ( 2 - 2 4 ) Sinh [t] + 6 (2 2 + 4 ) Sinh 6 t,
15
1
y[ t] → 6 (2 1 + 3 ) Cosh 6 t - 12 (( 1 - 2 3 ) Cosh [t] + ( 2 - 2 4 ) Sinh [t]) +
30
6 (2 2 + 4 ) Sinh 6 t
30 000
25 000
20 000
Out[70]=
15 000
10 000
5000
-4 -2 2 4
iii)
x'[t]=-2x+7y
y'[t]=3x+2y x[0]=0,y[0]=1
In[4]:= sol3 = DSolve [{x '[t] == - 2 x[t] + 7 y[t], y '[t] == 3 x[t] + 2 y[t], x[0] == 0, y[0] == 1}, {x[t], y[t]}, t]
7 1
x[t] → ⅇ - 1 + ⅇ , y[ t] → ⅇ 3 + 7 ⅇ
-5 t 10 t -5 t 10 t
Out[4]=
10 10
17
2 × 10 8
1 × 10 8
Out[5]=
-4 -2 2 4
-1 × 10 8
7) Approximate solution to initial value problems using the following approximate methods:
Comparison between exact and approximate results for any representative differential equation.
i) The Euler Method
ii) Modified Euler Method
ii) The Runge-Kutta Method
Euler Method
In[5]:= Euler1 [a0_ , b0_ , n0_ , f _, α_] := Module [{a = a0, b = b0, n = n0, h, ti }, h = (b - a) / n;
ti = Table [a + (j - 1) h, {j, 1, n + 1}];
wi = Table [0, n + 1];
wi [[1]] = α ;
OutputDetails = {{ 0, ti [[1]], α}} ;
For [i = 1, i ≤ n, i ++ , wi [[i + 1]] = wi [[i]] + h * f[ti [[i]], wi [[i]]];
OutputDetails = Append [OutputDetails , { i, N[ti [[i + 1]]], N[wi [[i + 1]]]}];];
Print [
NumberForm [TableForm [OutputDetails , TableHeadings → { None , { "i", "ti", "wi"}}], 6]];
];
18
In[6]:= f[t_ , x_ ] = t / x;
a = 0; b = 5; α = 1; n = 10;
Euler1 [a, b, n, f, α]
i ti wi
0 0 1
1 0.5 1.
2 1. 1.25
3 1.5 1.65
4 2. 2.10455
5 2.5 2.57971
6 3. 3.06426
7 3.5 3.55377
8 4. 4.04621
9 4.5 4.5405
10 5. 5.03604
In[9]:= f[t_ , x_ ] = 1 + x / t;
a = 1; b = 10; α = 2; n = 10;
Euler1 [a, b, n, f, α]
i ti wi
0 1 2
1 1.9 4.7
2 2.8 7.82632
3 3.7 11.2419
4 4.6 14.8764
5 5.5 18.687
6 6.4 22.6449
7 7.3 26.7294
8 8.2 30.9248
9 9.1 35.219
10 10. 39.6021
19
In[12]:= f[t_ , x_ ] = - 2 * x ^ 2 * t;
a = 0; b = 1; α = 1; n = 20;
Euler1 [a, b, n, f, α]
i ti wi
0 0 1
1 0.05 1.
2 0.1 0.995
3 0.15 0.9851
4 0.2 0.970543
5 0.25 0.951704
6 0.3 0.929061
7 0.35 0.903166
8 0.4 0.874616
9 0.45 0.844018
10 0.5 0.811962
11 0.55 0.778998
12 0.6 0.745622
13 0.65 0.712264
14 0.7 0.679289
15 0.75 0.646988
16 0.8 0.615594
17 0.85 0.585277
18 0.9 0.556161
19 0.95 0.528322
20 1. 0.501805
Modified Euler
In[1]:= Euler2 [a0_ , b0_ , n0_ , f _, α_] := Module [{a = a0, b = b0, n = n0, h, ti }, h = (b - a) / n;
ti = Table [a + (j - 1) h, {j, 1, n + 1}];
wi = Table [0, n + 1];
wi [[1]] = α ;
OutputDetails = {{ 0, ti [[1]], α}} ;
For [i = 1, i ≤ n, i ++ ,
wi [[i + 1]] = wi [[i]] + (h / 2) * (f[ti [[i]], wi [[i]]] + f[ti [[i + 1]], wi [[i]] + h * f[ti [[i]], wi [[i]]]]);
OutputDetails = Append [OutputDetails , { i, N[ti [[i + 1]]], N[wi [[i + 1]]]}];];
Print [
NumberForm [TableForm [OutputDetails , TableHeadings → { None , { "i", "ti", "wi"}}], 6]];
];
20
In[6]:= f[t_ , x_ ] = t / x;
a = 0; b = 5; α = 1; n = 10;
Euler2 [a, b, n, f, α]
i ti wi
0 0 1
1 0.5 1.125
2 1. 1.42168
3 1.5 1.80899
4 2. 2.24115
5 2.5 2.69682
6 3. 3.16589
7 3.5 3.6432
8 4. 4.12588
9 4.5 4.61225
10 5. 5.10126
In[121]:= f[t_ , x_ ] = 1 + x / t;
a = 1; b = 10; α = 2; n = 10;
Euler2 [a, b, n, f, α]
i ti wi
0 1 2
1 1.9 4.91316
2 2.8 8.28509
3 3.7 11.9576
4 4.6 15.8543
5 5.5 19.9298
6 6.4 24.1543
7 7.3 28.5065
8 8.2 32.9704
9 9.1 37.5336
10 10. 42.1862
1.0
0.8
0.6
0.4
Out[37]= Approx Solutions
0.2
1 2 3 4 5
-0.2
-0.4
1.0
0.8
0.6
0.4
Out[39]= Exact Solutions
0.2
1 2 3 4 5
-0.2
-0.4
1.0
0.8
0.6
Exact Solutions
0.4
Out[40]=
1 2 3 4 5
-0.2
-0.4
23
In[93]:= f[x_ , y _] := x / y;
rk [0, 5, 1, 10 ]
plot1 = ListPlot [pts , PlotLegends → {"Approx Solutions "}, PlotStyle → Red ]
sol = NDSolve [{y '[x] == x / y[x], y[0] == 1}, y[x], {x, 0, 5}]
plot2 = Plot [y[x] / . sol , {x, 0, 5}, PlotLegends → {"Exact Solutions "}]
Show [plot2 , plot1 ]
1 2 3 4 5
1 2 3 4 5
24
Exact Solutions
Out[98]= 3
Approx Solutions
1 2 3 4 5
In[99]:= f[x_ , y _] := 1 + y / x;
rk [1, 10, 2, 10, f]
plot1 = ListPlot [pts , PlotLegends → {"Approx Solutions "}, PlotStyle → Red ]
sol = NDSolve [{q '[x] == 1 + q[x] / x, q[1] == 2}, q[x], {x, 0, 10 }]
plot2 = Plot [q[x] / . sol , {x, 0, 10 }, PlotLegends → {"Exact Solutions "}]
Show [plot2 , plot1 ]
40
30
Out[101]=
20
Approx Solutions
10
2 4 6 8 10
1
Power : Infinite expression encountered .
0.
NDSolve : The function value {ComplexInfinity } is not a list of numbers with dimensions {1} at {x, q[x]} =
0. , -3.47893 .
-10
× 10
40
30
Out[103]=
20
Exact Solutions
10
2 4 6 8 10
40
30
Exact Solutions
Out[104]=
20
Approx Solutions
10
2 4 6 8 10
In[111]:= f[x_ , y _] := - 2 y ^ 2 x;
rk [0, 1, 1, 20, f]
plot1 = ListPlot [pts , PlotLegends → {"Approx Solutions "}, PlotStyle → Red ]
sol = NDSolve [{q '[x] == - 2 q[x] ^ 2 x, q[0] == 1}, q[x], {x, 0, 1}]
plot2 = Plot [q[x] / . sol , {x, 0, 1}, PlotLegends → {"Exact Solutions "}]
Show [plot2 , plot1 ]
1.0
0.9
0.8
0.6
0.5
1.0
0.9
0.8
0.6
1.0
0.9
0.6
8) Draw the following sequences of functions on the given interval and discuss pointwise and uniform
convergence
i) x^n
27
x
1.0
x2
x3
0.5
x4
x5
Out[8]=
x6
x8
x9
-0.5
x 10
1.0
0.5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+1
10
Out[51]=
-10 -5 5 10
-5
-10
viii) x^n/1+x^n
34
x2
x 2 +1
1.2
x3
3
x +1
1.1 x4
4
x +1
1.0 x5
x 5 +1
Out[60]=
x6
0.9 6
x +1
x7
0.8 x 7 +1
x8
x 8 +1
-10 -5 5 10
x9
9
x +1
x 10
x 10 +1
10
Out[61]=
-10 -5 5 10
-5
-10
In[3]:= Manipulate [
Plot [Table [x ^ n, {n, m, 40 }], {x, - 0.9 , 0.9 }, PlotRange → {- 2, 2}, PlotStyle → Red , Epilog →
{ Opacity [0.1 ], LightOrange , EdgeForm [GrayLevel [0.1 ]], Rectangle [{- a, l - ϵ } , { a, l + ϵ }]}],
{ m, 2, 40, 1, Appearance → "Labeled "} , {ϵ , 0.01 , 0.5 , 0.001 , Appearance → "Labeled "} ,
{ a, 0, 2, 0.01 , Appearance → "Labeled "} , { l, 0, 2, 0.01 , Appearance → "Labeled "}]
m 2
ϵ 0.417
a 0.5
l 0
Out[3]=
1
-0.5 0.5
-1
-2
We can see that, x^n converges uniformly to f(x)=0 in any interval [-a,a] aϵ(0,1)
ii) (x^2+nx)/n
36
In[4]:= Manipulate [Plot [Table [(x ^ 2 + n * x) / n, {n, m, 40 }], {x, - 0.9 , 0.9 },
PlotRange → {- 2, 2}, PlotStyle → Red , Epilog → {Opacity [0.1 ], LightOrange ,
EdgeForm [GrayLevel [0.1 ]], Rotate [Rectangle [{- a, l - ϵ }, {a, l + ϵ }], 45 Degree ]}],
{ m, 2, 40, 1, Appearance → "Labeled "} , {ϵ , 0.01 , 0.5 , 0.001 , Appearance → "Labeled "} ,
{ a, 0, 2, 0.01 , Appearance → "Labeled "} , { l, 0, 2, 0.01 , Appearance → "Labeled "}]
m 2
ϵ 0.286
a 1.04
l 0
Out[4]=
1
-0.5 0.5
-1
-2
In[5]:= Manipulate [
Plot [Table [x / n, {n, m, 40 }], {x, - 0.9 , 0.9 }, PlotRange → {- 2, 2}, PlotStyle → Red , Epilog →
{ Opacity [0.1 ], LightOrange , EdgeForm [GrayLevel [0.1 ]], Rectangle [{- a, l - ϵ } , { a, l + ϵ }]}],
{ m, 2, 40, 1, Appearance → "Labeled "} , {ϵ , 0.01 , 0.5 , 0.001 , Appearance → "Labeled "} ,
{ a, 0, 2, 0.01 , Appearance → "Labeled "} , { l, 0, 2, 0.01 , Appearance → "Labeled "}]
m 2
ϵ 0.273
a 0.56
l 0
Out[5]=
1
-0.5 0.5
-1
-2
In[1]:= Manipulate [
Plot [Table [x / (x + n), {n, m, 40 }], {x, 0, 1}, PlotRange → {- 2, 2}, PlotStyle → Red , Epilog →
{ Opacity [0.1 ], LightOrange , EdgeForm [GrayLevel [0.1 ]], Rectangle [{- a, l - ϵ } , { a, l + ϵ }]}],
{ m, 2, 40, 1, Appearance → "Labeled "} , {ϵ , 0.01 , 0.5 , 0.001 , Appearance → "Labeled "} ,
{ a, 0, 2, 0.01 , Appearance → "Labeled "} , { l, 0, 2, 0.01 , Appearance → "Labeled "}]
m 2
ϵ 0.01
a 0
l 0
Out[1]=
1
-1
-2
m 63
ϵ 0.166
a 0.48
l 0
Out[10]=
1
-0.5 0.5
-1
-2
m 34
ϵ 0.195
a 0.41
l 1.
Out[11]=
1
-0.5 0.5
-1
-2
m 4
ϵ 0.122
a 1.04
l 0
Out[14]=
1
-0.5 0.5
-1
-2