100% found this document useful (1 vote)
45 views40 pages

Aarush Arya Practical Compiled

The document discusses solving families of solutions to first order differential equations. It provides examples of using ode2() and desolve() functions in wxMaxima to plot the solutions for three differential equations. The examples vary the initial condition parameter k to generate multiple curves showing the effect of the initial condition on the solution.

Uploaded by

aarush arya
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
100% found this document useful (1 vote)
45 views40 pages

Aarush Arya Practical Compiled

The document discusses solving families of solutions to first order differential equations. It provides examples of using ode2() and desolve() functions in wxMaxima to plot the solutions for three differential equations. The examples vary the initial condition parameter k to generate multiple curves showing the effect of the initial condition on the solution.

Uploaded by

aarush arya
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/ 40

wxMaxima document 1 / 40

Aarush Arya

DSC-VI : Practical-01
Family of Solutions: First
Order Differential Equations
We'll plot the family of solutions of the following first order
differential equations:

1 y' = x² where y(0)=k


1.1 Using the pre-defined function 'ode2()'
(works for an O.D.E. of order upto 2)
ratprint: false$ /* suppresses error messages */
kill(all)$ /* clear all user-defined variables */
de: 'diff(y,x) = x^2; /* the eqn. is y' = x^2 */
sol: ode2(de, y, x); /* `sol` is assigned the General soln. of `de`*/
sol1: ic1(sol, x=0, y=k); /* `sol1` is a particular solution, w/ def. constt. %c being replac
v1: ev(sol1, k=−2); /* random values are given to `k` */
v2: ev(sol1, k=−1);
v3: ev(sol1, k=1);
v4: ev(sol1, k=2);

/* To plot the graphs */


wxplot2d([rhs(v1), rhs(v2), rhs(v3), rhs(v4)],
[x, −3, 3],
[style, [lines, 1], [lines, 2], [lines, 3], [lines, 4]])$
3 3
d 2 x x +3 k
y=x y= + %c y= y=
dx 3 3
3 3 3 3
x −6 x −3 x +3 x +6
y= y= y=
3 3 3 3
wxMaxima document 2 / 40

1.2 Using the pre-defined function 'desolve()'


(works for an O.D.E. of any order)
ratprint: false$
kill(all)$ /* clear all user-defined variables */
de: diff(y(x),x) = x^2; /* y is explicitly written as a function of x */
sol: desolve(de, y(x)); /* doesn't give constt.s explicitly but their values */
sol1: ev(sol, y(0)=k);
v1: ev(sol1, k=−2);
v2: ev(sol1, k=−1);
v3: ev(sol1, k=1);
v4: ev(sol1, k=2);

/* To plot the graphs */


wxplot2d([rhs(v1), rhs(v2), rhs(v3), rhs(v4)],
[x, −3, 3],
[style, [lines, 1], [lines, 2], [lines, 3], [lines, 4]])$
3 3
d 2 x x
y ( x )= x y ( x )= +y(0) y ( x )= +k
dx 3 3
wxMaxima document 3 / 40

3 3 3
x x x
y ( x )= −2 y ( x )= −1 y ( x )= +1
3 3 3
3
x
y ( x )= +2
3

2 y' = 9.8 - 0.196y


2.1 Using 'ode2()'
wxMaxima document 4 / 40

ratprint: false$
kill(all)$
de: 'diff(y,x) = 9.8 − 0.196·y;
gsol: ode2(de, y, x);
psol: ic1(gsol, x=0 ,y=k);
v0: ev(psol, k=0);
v1: ev(psol, k=1);
v2: ev(psol, k=2);
v3: ev(psol, k=−1);
v4: ev(psol, k=−2);
wxplot2d([rhs(v0), rhs(v1), rhs(v2), rhs(v3), rhs(v4)],
[x, −1, 1],
[style, [lines, 1], [lines, 2], [lines, 3], [lines, 4], [lines, 5]])$
49 x

250 49 x
d
y = 9.8 − 0.196 y y = %e 250
dx 50 %e + %c
49 x 49 x
− −
250 49 x 250
y = %e 250 y = %e
50 %e + k − 50
49 x

49 x 250 49 x
250 y = %e 250 y=
50 %e − 50 50 %e − 49
49 x 49 x
− −
250 49 x 250 49 x
%e 250 y = %e 250
50 %e − 48 50 %e − 51
49 x

250 49 x
y = %e 250
50 %e − 52
wxMaxima document 5 / 40

2.2 Using 'desolve()'

ratprint: false$
kill(all)$
de: diff(y(x),x) = 9.8 − 0.196·y(x);
gsol: desolve(de, y(x));
psol: ev(gsol, y(0)=k);
v0: ev(psol, k=0);
v1: ev(psol, k=−4);
v2: ev(psol, k=−2);
v3: ev(psol, k=2);
v4: ev(psol, k=4);
wxplot2d([rhs(v0), rhs(v1), rhs(v2), rhs(v3), rhs(v4)],
[x, −3, 3],
[style, [lines, 1], [lines, 2], [lines, 3], [lines, 4], [lines, 5]])$

d
y ( x ) = 9.8 − 0.196 y ( x ) y ( x )=
dx
49 x

250
( 250 y ( 0 ) − 12500 ) %e
+ 50 y ( x )=
250
49 x
− 49 x
250 −
( 250 k − 12500 ) %e 250
+ 50 y ( x ) = 50 − 50 %e
250
49 x 49 x
− −
250 250
y ( x ) = 50 − 54 %e y ( x ) = 50 − 52 %e
49 x 49 x
− −
250 250
y ( x ) = 50 − 48 %e y ( x ) = 50 − 46 %e
wxMaxima document 6 / 40

3 y'cos(x) + ysin(x) = 2cos³(x)sin(x)-1


ratprint: false$
kill(all)$
de: 'diff(y,x)·cos(x) + y·sin(x) = 2·(cos(x))^3·sin(x) − 1;
gsol: ode2(de, y, x);
psol: ic1(gsol, x=0, y=k);
v0: ev(psol, k=0);
v1: ev(psol, k=−1);
v2: ev(psol, k=−2);
v3: ev(psol, k=1);
v4: ev(psol, k=2);
wxplot2d([rhs(v0), rhs(v1), rhs(v2), rhs(v3), rhs(v4)],
[x,−1,1],
[style, [lines, 1], [lines, 2], [lines, 3], [lines, 4], [lines, 5]])$

d 3
cos ( x ) y + sin ( x ) y = 2 cos ( x ) sin ( x ) − 1 y = cos ( x )
dx

1
− 2 − tan ( x ) + %c y=−(
tan ( x ) + 1
wxMaxima document 7 / 40

3 2
cos ( x ) tan ( x ) + ( − k − 1 ) cos ( x ) tan ( x ) + cos ( x ) tan ( x ) − k cos ( x )
) y
2
tan ( x ) + 1
3 2
cos ( x ) tan ( x ) − cos ( x ) tan ( x ) + cos ( x ) tan ( x )
=− y=−
2
tan ( x ) + 1
3
cos ( x ) tan ( x ) + cos ( x ) tan ( x ) + cos ( x )
y=−
2
tan ( x ) + 1
3 2
cos ( x ) tan ( x ) + cos ( x ) tan ( x ) + cos ( x ) tan ( x ) + 2 cos ( x )
y=−
2
tan ( x ) + 1
3 2
cos ( x ) tan ( x ) − 2 cos ( x ) tan ( x ) + cos ( x ) tan ( x ) − cos ( x )
y=−
2
tan ( x ) + 1
3 2
cos ( x ) tan ( x ) − 3 cos ( x ) tan ( x ) + cos ( x ) tan ( x ) − 2 cos ( x )
2
tan ( x ) + 1
wxMaxima document 8 / 40

DSC-VI : Practical-02

Family of Solutions: Second

Order Differential Equations

We'll now plot the family of solutions of the following second


order differential equations:

1 y'' + 3y' + 2y = 0 where y'(0)=b, y(0)=1,


varying between -3 and 3
/* We'll use 'ode2()' */
ratprint: false$

de: 'diff(y,x,2) + 3·'diff(y,x) + 2·y = 0;


gsol: ode2(de, y, x); /* general soln. */
psol: ic2(gsol, x=0, y=1, 'diff(y,x)=b); /* particular soln. */

/* Fixing values for 'b' */


v0: ev(psol, b=0)$ /* suppress o/p */
v1: ev(psol, b=1)$
v2: ev(psol, b=2)$
v3: ev(psol, b=3)$
v4: ev(psol, b=−1)$
v5: ev(psol, b=−2)$
v6: ev(psol, b=−3)$

/* Using 'wxplot2d()' to plot the family of solutions */


wxplot2d([rhs(v0), rhs(v1), rhs(v2), rhs(v3), rhs(v4), rhs(v5), rhs(v6)],
[x, −1, 4],
[style, [lines, 1], [lines, 1.5], [lines, 2], [lines, 2.5], [lines, 3], [lines, 3.5], [lines, 4]],
[legend, "Curve-0", "Curve-1", "Curve-2", "Curve-3", "Curve-4", "Curve-5", "Curve-
2
d d −x
y+3 y +2 y=0 y = %k1 %e + %k2
2 dx
dx
−(2 x) −x −(2 x)
%e y = ( b + 2 ) %e + ( − b − 1 ) %e
wxMaxima document 9 / 40

2 y'' + 3y' + 2y = 0 where y(0)=a, y'(0)=1


wxMaxima document 10 / 40

/* We'll use 'ode2()' */


kill(all)$

de: 'diff(y,x,2) + 3·'diff(y,x) + 2·y = 0;


gsol: ode2(de, y, x); /* general soln. */
psol: ic2(gsol, x=0, y=a, 'diff(y,x)=1); /* particular soln. */

/* Fixing values for 'a' */


v0: ev(psol, a=0)$ /* suppress o/p */
v1: ev(psol, a=3)$
v2: ev(psol, a=6)$
v3: ev(psol, a=9)$
v4: ev(psol, a=−3)$
v5: ev(psol, a=−6)$
v6: ev(psol, a=−9)$

/* Using 'wxplot2d()' to plot the family of solutions */


wxplot2d([rhs(v0), rhs(v1), rhs(v2), rhs(v3), rhs(v4), rhs(v5), rhs(v6)],
[x, −1, 4],
[style, [lines, 1], [lines, 1.5], [lines, 2], [lines, 2.5], [lines, 3], [lines, 3.5], [lines, 4]],
[legend, "Curve-0", "Curve-1", "Curve-2", "Curve-3", "Curve-4", "Curve-5", "Curve-

2
d d −x
y+3 y +2 y=0 y = %k1 %e + %k2
2 dx
dx
−(2 x) −x −(2 x)
%e y = ( 2 a + 1 ) %e + ( − a − 1 ) %e
wxMaxima document 11 / 40

3 Solving (1) using desolve()

y'' + 3y' +2y = 0 where y'(0)=b, y(0)=1,

varying between -3 and 3

kill(all)$

de: diff(y(x),x,2) + 3·diff(y(x),x) + 2·y(x) = 0;


gsol: desolve(de, y(x)); /* general soln. */
psol: ev(gsol, y(0)=1, diff(y(x),x)=b); /* particular soln. */

/* Fixing values for 'b' */


v0: ev(psol, b=0)$
v1: ev(psol, b=1)$
v2: ev(psol, b=2)$
v3: ev(psol, b=−1)$
v4: ev(psol, b=−2)$

/* Using 'wxplot2d()' to plot the family of solutions */


wxplot2d([rhs(v0), rhs(v1), rhs(v2), rhs(v3), rhs(v4)],
[x, −1, 3],
[style, [lines, 0.5], [lines, 1], [lines, 1.5], [lines, 2], [lines, 2.5]],
[legend, "Soln-1", "Soln-2", "Soln-3", "Soln-4", "Soln-5"])$
2
d d −x
y ( x )+ 3 y ( x ) + 2 y ( x )= 0 y ( x ) = %e
2 dx
dx
d −(2 x) d
y(x) + 2 y ( 0 ) + %e − y(x) −y(0)
dx dx
x=0 x=0

−x −(2 x)
y ( x ) = ( b + 2 ) %e + ( − b − 1 ) %e
wxMaxima document 12 / 40

4 y'' + y' - 6y = 0 (No initial conditions


given)
wxMaxima document 13 / 40

/* We'll use 'ode2()' */


kill(all)$

de: 'diff(y,x,2) + 'diff(y,x) − 6·y = 0;


gsol: ode2(de, y, x); /* general soln. */
psol: ic2(gsol, x=0, y=c, 'diff(y,x)=k); /* particular soln. */

/* Fixing values for 'c' and 'k' */


v0: ev(psol, c=0, k=0)$
v1: ev(psol, c=1, k=−1)$
v2: ev(psol, c=2, k=−2)$
v3: ev(psol, c=3, k=−3)$
v4: ev(psol, c=−1, k=1)$
v5: ev(psol, c=−2, k=2)$
v6: ev(psol, c=−3, k=3)$
/* Using wxplot2d() to plot the family of solutions */
wxplot2d([rhs(v0), rhs(v1), rhs(v2), rhs(v3), rhs(v4), rhs(v5), rhs(v6)],
[x, −1, 2],
[style, [lines, 1], [lines, 1.5], [lines, 2], [lines, 2.5], [lines, 3], [lines, 3.5], [lines, 4]],
[legend, "Curve-0", "Curve-1", "Curve-2", "Curve-3", "Curve-4", "Curve-5", "Curve-
2
d d 2x −(3 x)
y+ y−6 y=0 y = %k1 %e + %k2 %e
2 dx
dx
2x −(3 x)
( k + 3 c ) %e ( 2 c − k ) %e
y= +
5 5
wxMaxima document 14 / 40

DSC-VI : Practical-03

Family of Solutions: Third

Order Differential Equations

We'll now plot the family of solutions of the following third order
differential equations:

1 y''' - 5y'' + 8y' - 4y = 0

kill(all)$
de: diff(y(x),x,3) − 5·diff(y(x),x,2) + 8·diff(y(x),x) − 4·y(x) = 0$
gsol: desolve(de, y(x))$
psol: ev(gsol, y(0)=c1, diff(y(x),x)=c2, diff(y(x),x,2)=c3)$
s1: ev(psol, c1=1, c2=2, c3=3)$
s2: ev(psol, c1=2, c2=1, c3=3)$
s3: ev(psol, c1=3, c2=1, c3=2)$
s4: ev(psol, c1=1, c2=3, c3=2)$
s5: ev(psol, c1=2, c2=3, c3=1)$
s6: ev(psol, c1=3, c2=2, c3=1)$

wxplot2d([rhs(s1), rhs(s2), rhs(s3), rhs(s4), rhs(s5), rhs(s6)],


[x, 2, 4],
[style, [lines, 1], [lines, 1.5], [lines, 2], [lines, 2.5], [lines, 3], [lines, 3.5]],
[legend, "1", "2", "3", "4", "5", "6"])$
wxMaxima document 15 / 40

2 y''' - 12y'' + 48y' - 64y = 12 - 32exp(-8x)


+ 2exp(4x)

kill(all)$
de: diff(y(x),x,3) − 12·diff(y(x),x,2) + 48·diff(y(x),x) − 64·y(x) = 12 − 32·exp(−8·x) + 2·ex
gsol: desolve(de, y(x))$
psol: ev(gsol, y(0)=k1, diff(y(x),x)=k2, diff(y(x),x,2)=k3)$
s1: ev(psol, k1=1, k2=2, k3=3)$
s2: ev(psol, k1=2, k2=1, k3=3)$
s3: ev(psol, k1=3, k2=2, k3=1)$
s4: ev(psol, k1=1, k2=3, k3=2)$
s5: ev(psol, k1=2, k2=3, k3=1)$
s6: ev(psol, k1=3, k2=1, k3=2)$

wxplot2d([rhs(s1), rhs(s2), rhs(s3), rhs(s4), rhs(s5), rhs(s6)],


[x, 3, 5],
[style, [lines, 1], [lines, 1.5], [lines, 2], [lines, 2.5], [lines, 3], [lines, 3.5]],
[legend, "1", "2", "3", "4", "5", "6"])$
wxMaxima document 16 / 40

DSC-VI : Practical-04
Exponential Growth/Decay
Model
x(t): population at time t
a: per capita death rate
b: per capita birth rate
initial condition: x(0) = x_0

1 Exponential Growth
wxMaxima document 17 / 40

kill(all)$
eqn: 'diff(x,t) = b·x − a·x;
sol: ode2(eqn, x, t);
fsol: ic1(sol, x=x_0, t=0);
fsol1: ev(fsol, a=0.010, b=0.027, x_0=5.3); /* b > a */
wxplot2d(rhs(fsol1),
[t, 0, 20],
[xlabel, "Base Year 1990"],
[ylabel, "Human Population (in billions)"])$

d (b−a) t b t−a t
x=b x−a x x = %c %e x = %e x
dt 0
0.017 t
x = 5.3 %e

2 Exponential Decay
wxMaxima document 18 / 40

kill(all)$
eqn: 'diff(x,t) = b·x − a·x ;
sol: ode2(eqn, x, t);
fsol: ic1(sol, x=x_0, t=0);
fsol1: ev(fsol, a=0.027, b=0.020, x_0=5.3); /* b > a */
wxplot2d(rhs(fsol1),
[t, 0, 20],
[xlabel, "Base Year 1990"],
[ylabel, "Human Population (in billions)"])$

d (b−a) t b t−a t
x=b x−a x x = %c %e x = %e x
dt 0
− ( 0.006999999999999999 t )
x = 5.3 %e

DSC-VI : Practical-05
Method of Variation of
Parameters
1 Solve y'' - y = x
wxMaxima document 19 / 40

Sol: The general solution comprises of two parts: y = y_c + y_p

We have:

de: 'diff(y,x,2) − y = x; /* our d.e. */


hp: lhs(de) = 0$ /* homogeneous part */
r: rhs(de)$ /* `r` i.e. non-homogeneous part */
2
d
y−y=x
2
dx

1.1 Calculating y_c


y_c: rhs(ode2(hp, y, x));
y_1: exp(x)$
y_2: exp(−x)$
x −x
%k1 %e + %k2 %e

1.2 Calculating y_p


A: matrix(
[y_1, y_2],
[diff(y_1, x), diff(y_2, x)]
);
x −x
%e %e

x −x
%e − %e

W: determinant(A);
−2
u_1: integrate(−y_2·r/W, x);
u_2: integrate(y_1·r/W, x);
−x x
( − x − 1 ) %e ( x − 1 ) %e

2 2
Now, our y_p is:
y_p: ratsimp(u_1·y_1 + u_2·y_2); /* this will return a simplified expression */
−x

1.3 General Solution


The general solution (y=y_c+y_p):
wxMaxima document 20 / 40

'y = y_c + y_p;


x −x
y = %k1 %e + %k2 %e −x

DSC-VI : Practical-06
Lake Pollution Model
1 Constant flow and constant pollution
concentration inflow
c(t): concentration of pollutant in the lake at time t.
F: constant flow rate.
V: constant volume of the lake.
cin: constant concentration of pollutant in the flow entering the
lake.
initial condition: c(0)=c0.(5 different initial conditions taken)
kill(all)$
eqn1: 'diff(c,t) = (F/V)·cin − (F/V)·c;
sol1: ode2(eqn1, c, t);
fsol1: ic1(sol1, c=c0, t=0);
v: ev(fsol1, cin=3, V=28, F=4·12);
v1: ev(v, c0=8)$
v2: ev(v, c0=6)$
v3: ev(v, c0=3)$
v4: ev(v, c0=2)$
v5: ev(v, c0=1)$
wxplot2d([rhs(v1), rhs(v2), rhs(v3), rhs(v4), rhs(v5)],
[t,0,2.5],
[legend,"1","2","3","4","5"],
[style, [lines, 1], [lines, 1.5], [lines, 2], [lines, 2.5], [lines, 3]],
[ylabel, "c(t)"])$
Ft

V Ft
d F cin Fc
c= − c = %e V
dt V V cin %e + %c
Ft 12 t
− −
V Ft 7
c = %e V c = %e
cin %e − cin + c0
wxMaxima document 21 / 40

12 t
7
3 %e + c0 − 3

2 Seasonal flow and constant pollution


concentration inflow
c(t): concentration of pollutant in the lake at time t.
F: seasonal flow rate.
V: constant volume of the lake.
cin: constant concentration of pollutant in the flow entering the
lake.
initial condition: c(0)=c0.
wxMaxima document 22 / 40

kill(all)$
eqn1: 'diff(c,t) = (F/V)·cin − (F/V)·c;
sol1: ode2(eqn1, c, t);
fsol1: ic1(sol1, c=c0, t=0);
v: ev(fsol1, cin=3, V=28, F=1+0.5·sin(2·π·t));
v1: ev(v, c0=1)$
wxplot2d(rhs(v1),
[t,0,8],
[legend,""],
[ylabel, "c(t)"])$
Ft

V Ft
d F cin Fc
c= − c = %e V
dt V V cin %e + %c
Ft

V Ft
c = %e V c=
cin %e − cin + c0
t ( 0.5 sin ( 2 π t ) + 1 )

28 t ( 0.5 sin ( 2 π t ) + 1 )
%e 28
3 %e + c0 − 3
wxMaxima document 23 / 40

3 Constant flow and seasonal pollution

concentration inflow

c(t): concentration of pollutant in the lake at time t.


F: constant flow rate.
V: constant volume of the lake.
cin: seasonal concentration of pollutant in the flow entering the
lake.
initial condition: c(0)=c0.
kill(all)$
eqn1: 'diff(c,t) = (F/V)·cin − (F/V)·c;
sol1: ode2(eqn1, c, t);
fsol1: ic1(sol1, c=c0, t=0);
v: ev(fsol1, cin=1+0.5·cos(2·π·t), V=28, F=4·12);
v1: ev(v, c0=1)$
wxplot2d(rhs(v1),
[t,0,8],
[legend,""],
[ylabel, "c(t)"])$
Ft

V Ft
d F cin Fc
c= − c = %e V
dt V V cin %e + %c
Ft 12 t
− −
V Ft 7
c = %e V c = %e
cin %e − cin + c0
12 t
7
− ( 0.5 cos ( 2 π t ) ) + %e ( 0.5 cos ( 2 π t ) + 1 ) + c0 − 1
wxMaxima document 24 / 40

4 Seasonal flow and seasonal pollution


concentration inflow
c(t): concentration of pollutant in the lake at time t.
F: seasonal flow rate.
V: constant volume of the lake.
cin: seasonal concentration of pollutant in the flow entering the
lake.
initial condition: c(0)=c0.
kill(all)$
eqn1: 'diff(c,t) = (F/V)·cin − (F/V)·c;
sol1: ode2(eqn1, c, t);
fsol1: ic1(sol1, c=c0, t=0);
v: ev(fsol1, cin=1+0.5·cos(2·π·t), V=28, F=1+0.5·sin(2·π·t));
v1: ev(v, c0=1)$
wxplot2d(rhs(v1),
[t,0,8],
[legend,""],
[ylabel, "c(t)"])$
wxMaxima document 25 / 40

Ft

V Ft
d F cin Fc
c= − c = %e V
dt V V cin %e + %c
Ft

V Ft
c = %e V c=
cin %e − cin + c0
t ( 0.5 sin ( 2 π t ) + 1 ) t ( 0.5 sin ( 2 π t ) + 1 )

28 28
%e ( − ( 0.5 cos ( 2 π t ) ) + %e
( 0.5 cos ( 2 π t ) + 1 ) + c0 − 1 )

DSC-VI : Practical-07
Logistic Growth Model
wxMaxima document 26 / 40

x(t): population at time t.


a: per capita death rate.
b: per capita birth rate.
r: b-a is the reproduction rate.
K: carrying capacity.
initial condition: x(0)=x0.

r: 1$ K: 1000$
eqn: 'diff(x,t)=r·x·(1−x/K);
gs: ode2(eqn,x,t);
gs1: logcontract(gs);
gs2: solve(gs1,x)[1];
ps: ic1(gs2, t=0, x=x0);
ps1: ev(ps, x0=10)$
ps2: ev(ps, x0=200)$
ps3: ev(ps, x0=500)$
ps4: ev(ps, x0=800)$
ps5: ev(ps, x0=1400)$
wxplot2d([rhs(ps1), rhs(ps2), rhs(ps3), rhs(ps4), rhs(ps5), K],
[t, 0, 10],
[legend, "x0=10", "x0=200", "x0=500", "x0=800", "x0=1400", "K (=1000)"],
[style, [lines, 1.5], [lines, 2], [lines, 2.5], [lines, 3], [lines, 3.5], [lines, 1]],
[xlabel, "t (in years)"], [ylabel, "Population"])$

d x
x= 1− x log ( x ) − log ( x − 1000 ) = t + %c
dt 1000
t + %c
x 1000 %e
log = t + %c x= x=
x − 1000 t + %c
%e −1
t
1000 %e x0
t
( %e − 1 ) x0 + 1000
wxMaxima document 27 / 40

DSC-VI : Practical-08
Demonstration of the
Runge-Kutta Method
1 Solve dy/dx = x, where at x=0, y=0
1.1 We solve the above differential equation
using ode2():
eqn: 'diff(y,x) = x;
gs: ode2(eqn, y, x);
ps: ic1(gs, y=0, x=0);
wxplot2d(rhs(ps), [x,0,2])$
2 2
d x x
y=x y= + %c y=
dx 2 2
wxMaxima document 28 / 40

1.2 Now, we use desolve() to solve the same


equation
kill(all)$
eqn: diff(y(x),x) = x;
gs: desolve(eqn, y(x));
ps: ev(gs, y(0)=0);
wxplot2d([rhs(ps)], [x,0,2])$
2 2
d x x
y ( x )= x y ( x )= +y(0) y ( x )=
dx 2 2
wxMaxima document 29 / 40

1.3 Finally, using the Runge-Kutta Method


Syntax:
rk([rhs(ODE_1),...,rhs(ODE_n)], [v1,...,vn], [init_1,...,init_n], domain)
kill(all)$
eqn: 'diff(y,x) = x;
fsol: rk(rhs(eqn), [y], [0], [x,0,2,0.1]);
wxplot2d([discrete, fsol])$
d
y=x [ [ 0.0 , 0.0 ] , [ 0.1 , 0.005000000000000001 ] ,
dx
[ 0.2 , 0.02 ] , [ 0.30000000000000004 , 0.045 ] , [ 0.4 , 0.07999999999999999 ] ,
[ 0.5 , 0.125 ] , [ 0.6000000000000001 , 0.18 ] , [ 0.7000000000000001 , 0.245 ] ,
[ 0.8 , 0.32 ] , [ 0.9 , 0.405 ] , [ 1.0 , 0.5 ] , [ 1.1 , 0.605 ] ,
[ 1.2000000000000002 , 0.72 ] , [ 1.3 , 0.845 ] , [ 1.4000000000000001 , 0.98 ] ,
[ 1.5 , 1.125 ] , [ 1.6 , 1.28 ] , [ 1.7000000000000002 , 1.445 ] , [ 1.8 , 1.62 ] ,
[ 1.9000000000000001 , 1.8050000000000002 ] , [ 2.0 , 2.0 ] ]
wxMaxima document 30 / 40

2 We now have a coupled system of


differential equations

dx/dt = 4 - x^2 - 4*y^2


dy/dt = y^2 - x^2 + 1

x(0) = -1.25, y(0) = 0.75


kill(all)$
eqn1: 'diff(x,t) = 4 − x^2 − 4·y^2;
eqn2: 'diff(y,t) = y^2 − x^2 + 1;
pts: rk([rhs(eqn1), rhs(eqn2)], [x,y], [−1.25,0.75], [t,0,2,0.01])$
d 2 2 d 2 2
x=−(4 y )−x +4 y=y −x +1
dt dt
wxMaxima document 31 / 40

curve_x: makelist([pts[i][1], pts[i][2]], i,1,length(pts))$


/*wxplot2d([discrete, curve_x])$*/
curve_y: makelist([pts[i][1], pts[i][3]], i,1,length(pts))$
wxplot2d([[discrete, curve_x], [discrete, curve_y]],
[legend, "x(t)", "y(t)"],
[style, [lines, 1], [lines, 3]],
[xlabel, "t"],
[ylabel, "x(t) and y(t)"])$

DSC-VI : Practical-09
Predator-Prey Model
1 Basic Lotka-Volterra Model
x(t): Number of prey per unit area.
y(t): Number of predators per unit area.
Initial condition: x(0)=200, y(0)=80.
The constant b1,c1,c2,a2 are all positive.
wxMaxima document 32 / 40

b1:1.0$ a2:0.5$ c1:0.01$ c2:0.005$


eqn1: 'diff(x,t) = b1·x − c1·x·y;
eqn2: 'diff(y,t) = c2·x·y − a2·y;
pts: rk([rhs(eqn1), rhs(eqn2)], [x,y], [200,80], [t,0,20,.1])$
[%[1], last(%), length(%)];
x_pts: makelist([pts[i][1], pts[i][2]], i,1,length(pts))$
[%[1], last(%), length(%)];
y_pts: makelist([pts[i][1], pts[i][3]], i,1,length(pts))$
[%[1], last(%), length(%)];
wxplot2d([[discrete, x_pts], [discrete, y_pts]],
[t,0,20], [y,0,250],
[style, [lines, 2], [lines, 4]],
[xlabel, "t (in years)"],
[ylabel, "Population density"],
[legend, "x(t): Preys", "y(t): Predators"])$
d d
x = 1.0 x − 0.01 x y y = 0.005 x y − 0.5 y [
dt dt
[ 0.0 , 200.0 , 80.0 ] , [ 20.0 , 154.59213309914438 , 157.6845208218405 ] , 201 ]
[ [ 0.0 , 200.0 ] , [ 20.0 , 154.59213309914438 ] , 201 ]
[ [ 0.0 , 80.0 ] , [ 20.0 , 157.6845208218405 ] , 201 ]
wxMaxima document 33 / 40

2 Density-Dependent Growth

x(t): Number of preys per unit area.


y(t): Number of predators per unit area.
Initial consitions: x(0)=120, y(0)=40.
The constants b1,c1,c2,a2,K are all positive.
kill(all)$
b1:1.0$ a2:0.5$ c1:0.01$ c2:0.005$ K: 1000$
eqn1: 'diff(x,t) = b1·x·(1−x/K) − c1·x·y;
eqn2: 'diff(y,t) = c2·x·y − a2·y;
pts: rk([rhs(eqn1), rhs(eqn2)], [x,y], [120,40], [t,0,40,.1])$
[%[1], last(%), length(%)];
x_pts: makelist([pts[i][1], pts[i][2]], i,1,length(pts))$
[%[1], last(%), length(%)];
y_pts: makelist([pts[i][1], pts[i][3]], i,1,length(pts))$
[%[1], last(%), length(%)];
wxplot2d([[discrete, x_pts], [discrete, y_pts]],
[t,0,40], [y,0,250],
[style, [lines, 2], [lines, 4]],
[xlabel, "t (in years)"],
[ylabel, "Population density"],
[legend, "x(t): Preys", "y(t): Predators"])$

d x d
x = 1.0 1 − x − 0.01 x y y = 0.005 x y −
dt 1000 dt
0.5 y [ [ 0.0 , 120.0 , 40.0 ] ,
[ 40.0 , 112.8076936975965 , 90.67006550194145 ] , 401 ]
[ [ 0.0 , 120.0 ] , [ 40.0 , 112.8076936975965 ] , 401 ]
[ [ 0.0 , 40.0 ] , [ 40.0 , 90.67006550194145 ] , 401 ]
wxMaxima document 34 / 40

3 Effect of DDT
x(t): Number of preys per unit area.
y(t): Number of predators per unit area.
Initial consitions: x(0)=200, y(0)=80.
The constants b1,c1,c2,a2,p1,p2 are all positive.
wxMaxima document 35 / 40

kill(all)$
b1:1.0$ a2:0.5$ c1:0.01$ c2:0.005$ p1: 0.1$ p2: 0.1$
eqn1: 'diff(x,t) = b1·x − c1·x·y − p1·x;
eqn2: 'diff(y,t) = c2·x·y − a2·y − p2·y;
pts: rk([rhs(eqn1), rhs(eqn2)], [x,y], [200,80], [t,0,20,.1])$
[%[1], last(%), length(%)];
x_pts: makelist([pts[i][1], pts[i][2]], i,1,length(pts))$
[%[1], last(%), length(%)];
y_pts: makelist([pts[i][1], pts[i][3]], i,1,length(pts))$
[%[1], last(%), length(%)];
wxplot2d([[discrete, x_pts], [discrete, y_pts]],
[t,0,20], [y,0,250],
[style, [lines, 2], [lines, 4]],
[xlabel, "t (in years)"],
[ylabel, "Population density"],
[legend, "x(t): Preys", "y(t): Predators"])$
d d
x = 0.9 x − 0.01 x y y = 0.005 x y − 0.6 y [
dt dt
[ 0.0 , 200.0 , 80.0 ] , [ 20.0 , 94.9961084048835 , 134.7757919917374 ] , 201 ]
[ [ 0.0 , 200.0 ] , [ 20.0 , 94.9961084048835 ] , 201 ]
[ [ 0.0 , 80.0 ] , [ 20.0 , 134.7757919917374 ] , 201 ]
wxMaxima document 36 / 40

4 Two Prey and One Predator

x1(t): Number of prey1 per unit area.


x2(t): Number of prey2 per unit area.
y(t): Number of predators per unit area.
Initial condition: x1(0)=150, x2(0)=130, y(0)=80.
The constant b1,b2,c1,c2,c3,c4,a are all positive.
kill(all)$
b1:1.32$ b2:1.3$ a:0.5$ c1:0.01$ c2:0.01$ c3: 0.003$ c4: 0.004$
eqn1: 'diff(x1,t) = b1·x1 − c1·x1·y;
eqn2: 'diff(x2,t) = b2·x2 − c2·x2·y;
eqn3: 'diff(y,t) = c3·x1·y + c4·x2·y − a·y;
pts: rk([rhs(eqn1), rhs(eqn2), rhs(eqn3)], [x1,x2,y], [150,130,80], [t,0,40,.1])$
[%[1], last(%), length(%)];
x1_pts: makelist([pts[i][1], pts[i][2]], i,1,length(pts))$
[%[1], last(%), length(%)];
x2_pts: makelist([pts[i][1], pts[i][3]], i,1,length(pts))$
[%[1], last(%), length(%)];
y_pts: makelist([pts[i][1], pts[i][4]], i,1,length(pts))$
[%[1], last(%), length(%)];
wxplot2d([[discrete, x1_pts], [discrete, x2_pts], [discrete, y_pts]],
[t,0,40], [y,0,300],
[style, [lines, 2], [lines, 3], [lines, 4]],
[xlabel, "t (in years)"],
[ylabel, "Population density"],
[legend, "x1(t): Prey1", "x2(t): Prey2", "y(t): Predator"])$
d d
x1 = 1.32 x1 − 0.01 x1 y x2 = 1.3 x2 − 0.01 x2 y
dt dt
d
y = 0.004 x2 y + 0.003 x1 y − 0.5 y [
dt
[ 0.0 , 150.0 , 130.0 , 80.0 ] , [ 40.0 , 94.32633644255458 , 36.73244132377353 ,
64.13219452304699 ] , 401 ]
[ [ 0.0 , 150.0 ] , [ 40.0 , 94.32633644255458 ] , 401 ]
[ [ 0.0 , 130.0 ] , [ 40.0 , 36.73244132377353 ] , 401 ]
[ [ 0.0 , 80.0 ] , [ 40.0 , 64.13219452304699 ] , 401 ]
wxMaxima document 37 / 40

DSC-VI : Practical-10
Epidemic Model for Influenza
1 Basic Epidemic Model
S(t): susceptibles at time t
I(t): infectives at time t
Initial condition: S(0)=762, I(0)=1.
The constants b,c are all positive
wxMaxima document 38 / 40

b:2.18·10^−3$ c:0.44$
eqn1: 'diff(S,t) = −b·S·I;
eqn2: 'diff(I,t) = b·S·I − c·I;
pts: rk([rhs(eqn1), rhs(eqn2)], [S,I], [762,1], [t,0,16,0.1])$
[%[1], last(%), length(%)];
susc: makelist([pts[i][1], pts[i][2]], i,1,length(pts))$
[%[1], last(%), length(%)];
infec: makelist([pts[i][1], pts[i][3]], i,1,length(pts))$
[%[1], last(%), length(%)];
wxplot2d([[discrete, susc], [discrete, infec]],
[t,0,16], [y,0,800],
[style, [lines,2], [lines,4]],
[xlabel, "t (in days)"],
[ylabel, "Susceptibles and Infectives Population"],
[legend, "S(t): Susceptibles", "I(t): Infectives"])$
d d
S = − ( 0.00218 I S ) I = 0.00218 I S − 0.44 I
dt dt
[ [ 0.0 , 762.0 , 1.0 ] , [ 16.0 , 20.37747256075321 , 11.67419228363819 ] , 161 ]
[ [ 0.0 , 762.0 ] , [ 16.0 , 20.37747256075321 ] , 161 ]
[ [ 0.0 , 1.0 ] , [ 16.0 , 11.67419228363819 ] , 161 ]
wxMaxima document 39 / 40

2 Contagious for Life

S(t): susceptibles at time t


I(t): infectives at time t
Initial condition: S(0)=762, I(t)=1.
The constant b is positive.
b:2.18·10^−3$
eqn1: 'diff(S,t) = −b·S·I;
eqn2: 'diff(I,t) = b·S·I;
pts: rk([rhs(eqn1), rhs(eqn2)], [S,I], [762,1], [t,0,16,0.1])$
[%[1], last(%), length(%)];
susc: makelist([pts[i][1], pts[i][2]], i,1,length(pts))$
[%[1], last(%), length(%)];
infec: makelist([pts[i][1], pts[i][3]], i,1,length(pts))$
[%[1], last(%), length(%)];
wxplot2d([[discrete, susc], [discrete, infec]],
[t,0,16], [y,0,800],
[style, [lines,2], [lines,4]],
[xlabel, "t (in days)"], [ylabel, "Susceptibles and Infectives Population"],
[legend, "S(t): Susceptibles", "I(t): Infectives"])$
d d
S = − ( 0.00218 I S ) I = 0.00218 I S [
dt dt
−6
[ 0.0 , 762.0 , 1.0 ] , 16.0 , 1.6087082536698787 10 , 762.9999983912916 ,
−6
161 ] [ 0.0 , 762.0 ] , 16.0 , 1.6087082536698787 10 , 161

[ [ 0.0 , 1.0 ] , [ 16.0 , 762.9999983912916 ] , 161 ]


wxMaxima document 40 / 40

You might also like