MAPLE: A QUICKIE COURSE (Continued)
> restart:with(plots):with(student):with(linalg):with(DEtools):with(stats):with(statplots):with(fit):
Warning, new definition for norm
Warning, new definition for trace
Warning, new definition for adjoint
12. Differential equation.
We shall solve the differential equation y' = 2 - .04y, y(0) = 0. The notation used in this problem cannot be used in Maple. Instead use y(t), diff(y(t),t), diff(y(t),t,t) for y, y' y". Or use differential operators y(t), D(y)(t),(D@@2)(y)(t) . Notice carefully where the parentheses go; this is not intuitive. We have named the differential equation de and solve it with the dsolve command. I am not sure what data type y(t) is. It appears to be of no type, but only a dummy which tells the program what the independent variable t is and what the dependent variable y is. In any case I have redefined y after solving the de to make it type function . This allows me to plot its graph with plot . The plot command also plots the constant function 50 since y = 50 is the horizontal asymptote of this function.
>
> de:=diff(y(t),t)=2.-.04*y(t);
> dsolve({de,y(0)=0},y(t));
> y:=t->50.-50.*exp(-.04*t);
> plot({y(t),50},t=0..100);
> y:='y':t:='t':
The differential equation de2 is linear order 2 constant coefficients non-homogeneous, a nightmare to do with a pencil. Note the particular part first followed by the complementary part.
> de2:=diff(y(t),t,t)-4.89*diff(y(t),t)+98.34*y(t)=t^2;
> dsolve({de2},y(t));
> y:='y':t:='t':
The differential equation de3 is the same as de2 except written with differential operators and initial conditions.
> de3:=(D@@2)(y)(t)-4.89*D(y)(t)+98.34*y(t)=t^2;
> dsolve({de3,y(0)=2.,D(y)(0)=-5.},y(t));
> x:='x':y:='y':t:='t':
13. System of differential equations
This is the system for the two tanks
x' = 5 - 6(x/100) + 1(y/200), y' = 6(x/100) - 6(y/200), x(0) = 0, y(0) = 50.
Note that dsolve works pretty much the same way on systems as it does on single ODE's.
> dsolve({D(x)(t)=5-6/100*x(t)+1/200*y(t),D(y)(t)=6/100*x(t)-6/200*y(t),x(0)=0,y(0)=50},{x(t),y(t)});
As usual Maple tries to do this with rationals and surds. The next command changes it to floating point with 4 significant digits.
>
> evalf(%,4);
We next reassign x and y to have type function .
> x:=t->100.0-33.65*exp(-.02209*t)-66.42*exp(-.06790*t);
> y:=t->200.0+105.1*exp(-.06790*t)-255.1*exp(-.02209*t);
Now we plot x and y together with 2 constant functions (the assymptotes). Note how the color and thickness options assign colors and thickness to the 4 functions being plotted.
> plot({100,200,x(t),y(t)},t=0..150,color=[black,black,red,blue],thickness=[1,1,3,3],title=`Tank1 = red Tank2 = blue` );
14. Matrices
The next command defines a 3 by 3 matrix whose entries are listed in the brackets by rows.
> A:=matrix(3,3,[1,4,5,-9,7,6,9,0,0]);
> B:=matrix(3,3,[-8,-5,6,1,0,0,3,2,2]);
The 2,3 entry of A is denoted A[2,3]. Since a (lowercase) is not defined, all Maple can do is write a sub 2,3.
> a[2,3]=A[2,3];
> b[1,1]=B[1,1];
> B[2,1];
> b[2,1];
The operations on matrices are +, -, * (scalar multiplication), &* (matrix multiplication). These operations appear not to be evaluated without the command evalm (evaluation matrix).
> `A + B`=evalm(A+B);
> `3A`=evalm(3*A);
> `AB`=evalm(A&*B);
> `Inverse of A`=inverse(A);
> evalf(%,6);
> `Determinant of A`=det(A);
> A&*B-3*inverse(B)&*B;
> evalm(%);
A matrix can be made to look like a table. This is a programming trick which should be used with caution. The entry name is not a string, but a variable which has no assigned value. If name had earlier been assigned the value 6, then 6 would appear where name is.
> A:='A':B:='B':GradeBook:=matrix(4,3,[Name,Average,Grade,Fred,65,C,Jane,92,A,Paul,75,B]);
15. Vector
The next command makes U a 3 dimensional vector.
> U:=vector(3,[5,-7,8]);
This command makes V a 3 dimensional curve with parameter t . V can be differentiated by map ping the diff operator onto V . This means it applies diff to each of the 3 coordinates. Antidifferentiation ( int )can also be mapped onto the vector V .
> V:=vector(3,[t^2,cos(Pi*t),exp(4*t)]);
> `V '`=map(diff,V,t);
> `V "`=map(diff,V,t,t);
> `V '"`=map(diff,V,t,t,t);
> Int(V,t)=map(int,V,t);
The usual operations on vectors can be performed though awkwardly.
>
> Vec1:=vector(3,[3,5,9]);Vec2:=vector(3,[7,5,3]);
> `Addition`; `Vec1 + Vec2 =`;evalm(Vec1+Vec2);
> `Subtraction`; `Vec1 - Vec2 =`;evalm(Vec1-Vec2);
> `Scalar multiplication `; `3*Vec2 =`;evalm(3*Vec2);
> `Dot or inner product`; `Vec1.Vec2 =`;innerprod(Vec1,Vec2);
> `Cross product`; `Vec1 x Vec2 =`;crossprod(Vec1,Vec2);
>