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
This form of graph can be used for plotting data. Suppose one did an experiment in the lab to test Hooke's Law. Forces of 8 different magnitudes were applied to a spring and the amount of stretching measured. The data are typed into two data lists X and Y. The command scatterplot will then plot the 8 points representing the 8 trials. The default options for scatterplot suck. Use the options s ymbol=circle, color=red for a graph that is easy to see. The labels option determines what is printed on the x and y axes.
> `Force on the spring(Newtons)`;X := [9.8, 19.6, 29.4, 49, 58.8, 68.6, 98.0, 117.6];
>
> `Amount of stretching (cm)`;Y := [58.3, 68.7, 95.3, 120.6, 151.8, 170.3, 234.9, 306.8];
> scatterplot(X,Y,symbol=circle,color=red,labels=[`Force (n)`,`Stretching (cm)`]);
> graph1:=%:
This command calculates the line that best fits the data for the Hooke's Law problem. This command has a hard to remember form. I have tried below to break it down into easier to remember parts.
leastsquare[
[independant variable,dependent variable],
form of equation to be fit,
{parameters of the eq}
]
([data list for independent variable,data list for dependent variable])
> leastsquare[[force,stretching],stretching=m*force+b,{m,b}]([X,Y]);
> implicitplot(stretching=2.25*force+24.19,force=0..130,stretching=0..400, color=blue);
> graph2:=%:
> display(graph1,graph2);
10. Sequence
The form of a sequence can be seen below in p and q . The 3rd number in the sequence q is denoted q[3] and the 2nd number of sequence p by p[2] .
> p:=seq(n^2,n=1..5);
> q:=seq(n^2+n-1,n=1..5);
> q[3];p[2];
A sequence may be generated from a formula as are p and q or recursively. The sequence below gives the first 20 Fibbonacci numbers, which are generated recursively. Note also the form of the loop in Maple:
for <index> from <first index value> to <last index value> by <index step> do BLOCK od;
The index must be of type INTEGER which is a subtype of type RATIONAL . Using colons instead of semicolons prevents the loop from printing any output. One usually does not want a loop to output several thousand numbers. In this case there are only 20 outputs from the loop and they are stored in the sequence F . The line after the loop prints the sequence.
> F[1]:=1: F[2]:=1: for i from 3 to 20 do F[i] := F[i-1] + F[i-2]; od:
> Fib:=seq(F[i],i=1..20);
11. Data list
Because most statistical commands use this type, I call it a data list . Actually I think it is just called type list . Although Maple can read and write to files, that is not the way it is usually done. Data are usually generated as a sequence and then copied into a data list. Stored "onboard" so to speak. A data list can be created just by typing the data into it as in L . Data may also be copied into a list from a sequence as in P and Q . Note that Maple is case sensitive, and so p, q, P and Q are all different. Though lists and vectors appear to be the same, they are distinct data types. Note use of is for querying data type.
> L:=[3.14, 8.99,10.65,12.75,-82.00];
> P:=[seq(p[i],i=1..5)];
> Q:=[seq(q[i],i=1..5)];
> is(Q,vector);is(Q,array);is(Q,list);
> scatterplot(P,Q,color=red);
There is no royal road to Maple. Any very comprehensive software like Maple is going to take some time to learn. But finally we get to the point of seeing Maple do something we have done in class.
We are going to solve the differential equation y' = .05*y, y(0) = 8 using Euler's method with DELt = .001 . The ouputs of the loop are stored in two sequences t and y . Assume the unit of time is seconds. Then the sequence t progresses by milliseconds since DELt = .001 . Thus t[1000] is time 1 and y[1000] is the value of y at time 1. This loop will store 100001 values in t and another 100001 values in y . Put a colon after od or else it will print all of these 200002 numbers.
> DELt:=.001;t[0]:=0;y[0]:=8.;
> for i from 1 to 100000 do t[i]:=t[i-1]+DELt; y[i]:=y[i-1]+.05*y[i-1]*DELt; od:
The next loop prints out a table for t, y at times 0, 1, 2, 3, etc. The print command (which is the default command) can be formatted to make a nicer looking table, but let us not get into that.
> for i from 0 to 10 do t[1000*i],y[1000*i]; od;
> `The value of y at time 3.654 is y[3654]`; time=t[3654],`value of y`=y[3654];
In my semi-humble opinion, the best way to plot computed data is with scatterplot , even though that is part of the stats package. Above you can see that scatterplot requires two data lists . It will not plot the sequences t and y, but if we copy them into data lists, it will plot the data lists. The following does not copy all of t and y, but only every 100 milliseconds of them. Using the options style=point and symbol=point , scatterplot will plot 1000 points, which are enough that the points appear to merge into a curve.
> T:=[seq(t[100*i],i=0..1000)]:
> Y:=[seq(y[100*i],i=0..1000)]:
> scatterplot(T,Y,color=black,style=point,symbol=point,labels=[`t`,`y(t)`]);
> Euler:=%:
It is easy to show that the solution to the de is y = 8*exp(.05*t) . The following is intended to compare the graphs of the exact solution and Euler's solution. Because the graphs are so close to coincidence one can only see the one on top (the first one listed in display ). Thus I made the exact graph (magenta) very wide so that one can see the Euler graph going up through its middle.
>
> Exact:=plot(8*exp(.05*t),t=0..100, color=magenta, thickness = 10):
> display(Euler,Exact);
It is important to know which commands operate on which data type. In the case of graphs:
plot
operates on type
expression
implicitplot
operates on type
equation
scatterplot
operates on type
list,list
All three forms of graph can be displayed together using display .