Difference between revisions of "Maple/Plotting"
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | [[Maple]] has several built in commands to make plots. This page will show you how to use the | + | [[Maple]] has several built-in commands to make plots. This page will show you how to use the <code>plot</code> command. |
== Basics == | == Basics == | ||
Line 8: | Line 8: | ||
<source lang=text> | <source lang=text> | ||
plot([cos(t), sin(t)], t = 0 .. 2*Pi) | plot([cos(t), sin(t)], t = 0 .. 2*Pi) | ||
− | </source> | + | </source> |
If you have a variable that has several unknowns, you can use the | If you have a variable that has several unknowns, you can use the | ||
Line 29: | Line 29: | ||
</source> | </source> | ||
− | With the same equation, then, you could make a plot of <math>f</math> at the 6 m | + | With the same equation, then, you could make a plot of <math>f</math> at the 6 m mark for times between 0 and 4 just by switching the variables around a bit: |
− | mark for times between 0 and 4 just by switching the variables around | ||
− | a bit: | ||
<source lang=text> | <source lang=text> | ||
FVals:= omega=5, k=3; | FVals:= omega=5, k=3; | ||
Line 44: | Line 42: | ||
</gallery></center> | </gallery></center> | ||
− | + | == Plot Options == | |
[[File:MPplot3.jpg|thumb|right|Example with labels, title, and legend]] | [[File:MPplot3.jpg|thumb|right|Example with labels, title, and legend]] | ||
There are several plot options, which are described in the help file | 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 | for plot as well as a help file for plot options. The main ones to | ||
use here will be the <code>labels</code>, <code>labeldirections</code>, <code>title</code>, | use here will be the <code>labels</code>, <code>labeldirections</code>, <code>title</code>, | ||
− | + | <code>legend</code>, <code>linestyle</code>, <code>color</code>, and <code>size</code>. Options | |
go after the independent argument and are separated by commas. For | go after the independent argument and are separated by commas. For | ||
example, a more complete version of the trig plot above might be: | example, a more complete version of the trig plot above might be: | ||
Line 56: | Line 54: | ||
labels = ["Time (t)", "Voltage (V)"], | labels = ["Time (t)", "Voltage (V)"], | ||
labeldirections = [horizontal, vertical], | labeldirections = [horizontal, vertical], | ||
− | title = "Cosine and Sin | + | title = "Cosine and Sin", |
legend = ["cos(t)", "sin(t)"], | legend = ["cos(t)", "sin(t)"], | ||
− | linestyle=[4,2]) | + | linestyle=[4,2], |
+ | color=["red", "green"], | ||
+ | size=[300, 300]) | ||
</source> | </source> | ||
− | Note - using Shift-Enter will go to a new line. | + | Note - using Shift-Enter will go to a new line. Here are Maple's help pages on [https://www.maplesoft.com/support/help/maple/view.aspx?path=plot%2foptions Plot Options] along with specific help on [https://www.maplesoft.com/support/help/maple/view.aspx?path=plot%2fcolornames color names]. Here's the blurb from Maple about linestyle (annotated to include the numerical value of each style): "The linestyle value $$t$$ can be one of the following names: (1) solid, (2) dot, (3) dash, (4) dashdot, (5) longdash, (7) spacedash, or (7) spacedot. The default value of $$t$$ is solid. The value $$t$$ can also be an integer from 1 to 7, where each integer represents a line style, as given in the order above. " |
+ | == Exceptional Cases == | ||
=== Plotting Functions with Vestigial Imaginary Parts === | === 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 <code>map</code> command to apply the <code>Re</code> command to your function. For example, if <code>MySoln</code> contains a set of solutions for variables <code>a(t)</code>, <code>b(t)</code>, and <code>c(t)</code>, and those variables have leftover imaginary parts, you could use: | 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 <code>map</code> command to apply the <code>Re</code> command to your function. For example, if <code>MySoln</code> contains a set of solutions for variables <code>a(t)</code>, <code>b(t)</code>, and <code>c(t)</code>, and those variables have leftover imaginary parts, you could use: | ||
Line 68: | Line 69: | ||
</source> | </source> | ||
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. | 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: | ||
+ | <source lang="text"> | ||
+ | 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); | ||
+ | </source> | ||
+ | you will get the following graph: | ||
+ | <center> | ||
+ | [[File:PiecewiseArtifacts1.gif]] | ||
+ | </center> | ||
+ | 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: | ||
+ | <source lang="text"> | ||
+ | xp := convert(x, piecewise) | ||
+ | </source> | ||
+ | This will create a function that looks like: | ||
+ | <center><math> | ||
+ | \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} | ||
+ | </math></center> | ||
+ | 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: | ||
+ | <center> | ||
+ | [[File:PiecewiseArtifacts2.gif]] | ||
+ | </center> | ||
+ | which has no artifacts. |
Latest revision as of 23:00, 27 September 2023
Maple has several built-in commands to make plots. This page will show you how to use the plot
command.
Contents
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\):
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
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
, linestyle
, color
, and size
. 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",
legend = ["cos(t)", "sin(t)"],
linestyle=[4,2],
color=["red", "green"],
size=[300, 300])
Note - using Shift-Enter will go to a new line. Here are Maple's help pages on Plot Options along with specific help on color names. Here's the blurb from Maple about linestyle (annotated to include the numerical value of each style): "The linestyle value $$t$$ can be one of the following names: (1) solid, (2) dot, (3) dash, (4) dashdot, (5) longdash, (7) spacedash, or (7) spacedot. The default value of $$t$$ is solid. The value $$t$$ can also be an integer from 1 to 7, where each integer represents a line style, as given in the order above. "
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:
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:
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:
which has no artifacts.