Maple/Plotting

From PrattWiki
Revision as of 22:56, 15 January 2018 by DukeEgr93 (talk | contribs)
Jump to navigation Jump to search

Maple has several built in commands to make plots. This page will show you how to use the plot command.

Basics

The command generally takes at least two arguments - an array of items to plot and a statement about the independent variable and its values. For example, to plot cos and sin over one period, you could type:

plot([cos(t), sin(t)], t = 0 .. 2*Pi)

If you have a variable that has several unknowns, you can use the subs command to make substitutions for all but the independent variable of the plot. For example, given some function \(f\):

\( \begin{align} f&=e^{-t}\cos(\omega t-k x) \end{align} \)

which can be defined in Maple using the code:

f:=exp(-t)*cos(omega*t-k*x)

and assuming \(\omega\) is known to be 5 rad/s and \(k\) is 3 rad/m, you could make a plot of \(f\) at \(t=10\) s over the course of a 2 m section:

FVals:= omega=5, k=3;
plot(subs(FVals, t=10, f), x=0..2)

With the same equation, then, you could make a plot of \(f\) at the 6 m mark for times between 0 and 4 just by switching the variables around a bit:

FVals:= omega=5, k=3;
plot(subs(FVals, x=6, f), t=0..4)


Plot Options

Example with labels, title, and legend

There are several plot options, which are described in the help file for plot as well as a help file for plot options. The main ones to use here will be the labels, labeldirections, title, legend, and linestyle. Options go after the independent argument and are separated by commas. For example, a more complete version of the trig plot above might be:

plot([cos(t), sin(t)], t = 0 .. 2*Pi, 
labels = ["Time (t)", "Voltage (V)"], 
labeldirections = [horizontal, vertical], 
title = "Cosine and Sin (mrg)", 
legend = ["cos(t)", "sin(t)"],
linestyle=[4,2])

Note - using Shift-Enter will go to a new line.

Exceptional Cases

Plotting Functions with Vestigial Imaginary Parts

Oftentimes, solving differential equations or inverse Laplace transforms will yield small roundoff errors that cause a function which should be purely real to have a vestigial imaginary component. To eliminate this - and thus make it possible to see the plot - use Maple's map command to apply the Re command to your function. For example, if MySoln contains a set of solutions for variables a(t), b(t), and c(t), and those variables have leftover imaginary parts, you could use:

plot(map(Re, subs(MySoln[], [a(t), b(t), c(t)])), t=0..1)

to plot just the real parts. If a plot line seems to "disappear" over the domain of the plot, the usual cause is the extra imaginary part that needs to be eliminated.

Plotting Piecewise Functions

If an expression has several Heaviside functions within it, Maple can have a hard time actually making the plot. There may either be sections of the graph that do not plot at all or there may be roundoff errors that creep in. As an example, given the following code:

u:=t->Heaviside(t);
x := exp(t)*u(t)-u(t)-exp(t)*u(t-2)+exp(2)*u(t-2)-(exp(2)-1)*u(t-10);
plot(x, t = 0 .. 50);

you will get the following graph:

PiecewiseArtifacts1.gif

Note the problem once t is about 36 sec - there is some noise, then the signal value jumps up to 1. That should not happen! In order to eliminate the possibility of that kind of roundoff error, you can first tell Maple to convert functions with several Heaviside functions to a piecewise function. That is:

xp := convert(x, piecewise)

This will create a function that looks like:

\( \begin{align} xp:=\left\{ \begin{array}{cc} 0&t<0\\ \mbox{undefined}&t=0\\ \mbox{e}^{t}-1 & t\leq 2\\ \mbox{e}^{2}-1&t<10\\ -1+\mbox{e}^{2}+ \left( -\mbox{e}^{2}+1 \right) \mbox{undefined} & t=10\\ 0&10<t \end{array} \right. \end{align} \)

where the undefined parts are at the exact location the Heaviside function switched from on to off. Making a plot of the converted function yields:

PiecewiseArtifacts2.gif

which has no artifacts.