Chap0_Tut_0.3.mw

MAPLE TEXT COMMANDS FOR CHAPTER 0 

TUTORIAL 0.3: SIMPLE MAPLE PROGRAMS 

Procedures: The norm of a vector in three-dimensional space. 

>

norm3d:=proc(a,b,c) sqrt(a^2+b^2+c^2) end;
norm3d(3,4,0);

 

5

(1)

 

For..do..end do loop: Sum the natural numbers from 1 to 100. 

>

i:=`i`:total:=0:
for i from 0 to 100 do
total:=i+total:
end do:
total;

 

5050

(2)

 

If..then..elif..else: Determine whether p is less than or not less than 2. 

>

p:=4:
if p<2 then printf("p is less than 2");
elif p>=2 then printf("p is not less than 2");
end if;

 

p is not less than 2

 

Arrays and sequences: Evaluate the first 25 terms of the Fibonacci sequence. 

>

F:=array(1..10000):
F[1]:=1:F[2]:=1:N:=15:
for i from 3 to N do
F[i]:=F[i-1]+F[i-2]:
end do:
seq(F[i],i=1..N);

 

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610

(3)

 

Iteration: List the final 10 iterates for the logistic map. 

>

mu:=3.2:x[0]:=0.2:
for n from 0 to 99 do
x[n+1]:=mu*x[n]*(1-x[n]):
end do:
for n from 90 to 99 do
nprintf("x[%-d]=%g",n+1,x[n+1]);
end do;

 

`x[91]=0.513045` 

 

`x[92]=0.799455` 

 

`x[93]=0.513045` 

 

`x[94]=0.799455` 

 

`x[95]=0.513045` 

 

`x[96]=0.799455` 

 

`x[97]=0.513045` 

 

`x[98]=0.799455` 

 

`x[99]=0.513045` 

 

`x[100]=0.799455`

(4)

 

Multiple plots: Using the display command. 

>

with(plots):
p1:=plot(x^2,x=-4..4,color=blue):p2:=plot(4-x^2,x=-4..4,color=red):
t1:=textplot([2.6,6,`y=x^2`],align=RIGHT):
t2:=textplot([-3,-6,`y=4-x^2`],align=RIGHT):
display({p1,p2,t1,t2},font=[TIMES,ROMAN,15],tickmarks=[3,3]);

 

Plot_2d

 

Multiple plot with text: Solution curves to two initial value problems.  

>

restart:with(DEtools):with(plots):
deqn1:=diff(x(t),t$2)=-2*diff(x(t),t)-25*x(t):
p3:=DEplot(deqn1,x(t),t=0..10,[[x(0)=1,D(x)(0)=0]],stepsize=0.1,linecolor=blue,thickness=1):
deqn2:=diff(x(t),t$2)=-25*x(t):
p4:=DEplot(deqn2,x(t),t=0..10,[[x(0)=1,D(x)(0)=0]],stepsize=0.1,linecolor=red,thickness=1):
t3:=textplot([6,1,`Harmonic motion`],align=RIGHT,color=red):
t4:=textplot([1.8,0.2,`Damped motion`],align=RIGHT,color=blue):
display({p3,p4,t3,t4},labels=[`t`,`x`]);

 

Plot_2d

 

Interactive Exploration: An interactive parameter Maplet pops up with a parameter slider. 

>

restart:with(DEtools):with(plots):
interactiveparams(plot,[{1-a*x,(x-1)/a},x=0..2],a=0..1);

 

Plot_2d

 

Interactive Exploration: Consider the system of differential equations  

http://www.doc.mmu.ac.uk/STAFF/S.Lynch/images/Chap0_Tut_0.3_18.gifused to model the chemical system 

X→Y→Z. Show how the solution curves vary as the rate constants, aand b, vary.  

>

sys1:=diff(x(t),t)=-a*x(t),diff(y(t),t)=a*x(t)-b*y(t),diff(z(t),t)=b*y(t);

 

diff(x(t), t) = `+`(`-`(`*`(a, `*`(x(t))))), diff(y(t), t) = `+`(`*`(a, `*`(x(t))), `-`(`*`(b, `*`(y(t))))), diff(z(t), t) = `*`(b, `*`(y(t)))

(5)

 

>

dsolve([sys1,x(0)=10,y(0)=0,z(0)=0]);

 

{x(t) = `+`(`*`(10, `*`(exp(`+`(`-`(`*`(a, `*`(t)))))))), y(t) = `+`(`/`(`*`(10, `*`(a, `*`(exp(`+`(`-`(`*`(b, `*`(t)))))))), `*`(`+`(a, `-`(b)))), `-`(`/`(`*`(10, `*`(a, `*`(exp(`+`(`-`(`*`(a, `*`(t)...

(6)

 

>

interactiveparams(plot,[{10*exp(-a*t),10*a*exp(-b*t)/(a-b)-10*a*exp(-a*t)/(a-b),(10*exp(-a*t)*b-10*exp(-b*t)*a^2/(a-b)+10*exp(-b*t)*a*b/(a-b)+10*a-10*b)/(a-b)},t=0..30],a=0.1..0.9,b=0.1..0.8);

 

Plot_2d

 

Figure: A plot when a = .5and b = .45.  

End of Tutorial 0.3