Difference between revisions of "ECE 280/Fall 2023/MAPLE"

From PrattWiki
Jump to navigation Jump to search
(Created page with "This web page is meant to support HW 4 Part III for the Fall 2021 ECE 280 class. == Accessing Maple == See the Using Maple: Duke Container section...")
 
 
Line 1: Line 1:
This web page is meant to support HW 4 Part III for the Fall 2021 ECE 280 class.   
+
This web page is meant to support the Maple Intro for the Fall 2023 ECE 280 class.   
 
== Accessing Maple ==
 
== Accessing Maple ==
 
See the [[Maple#Duke_Container | Using Maple: Duke Container]] section of the [[Maple]] page for how to reserve a Maple container and then use it.  Containers are accessed via web browsers so you will not need any additional software to use Maple in this way.
 
See the [[Maple#Duke_Container | Using Maple: Duke Container]] section of the [[Maple]] page for how to reserve a Maple container and then use it.  Containers are accessed via web browsers so you will not need any additional software to use Maple in this way.
Line 7: Line 7:
 
* [[Maple/Initialization and Documentation]]
 
* [[Maple/Initialization and Documentation]]
 
* [[Maple/Plotting]]
 
* [[Maple/Plotting]]
Then go through the [https://services.math.duke.edu/education/ccp/materials/engin/maptutor/contents.html Connected Curriculum Project - Engineering Maple Tutor] modules.  Note that modules 12 and 14 may not come up much in this course but are interesting nonetheless!
+
Then go through the [https://services.math.duke.edu/education/ccp/materials/engin/maptutor/contents.html Connected Curriculum Project - Engineering Maple Tutor] modules 1-11.  Note that modules 12 and 14 may not come up much in this course but are interesting nonetheless! A few notes:
 +
* You do not need semicolons anymore...unless you are trying to have one line do multiple things, then those things need to be divided by semicolons
 +
* A colon at the end of a thing tells Maple to do that thing but suppress viewing the results
 +
* Part 4: F5 is no longer the shortcut to enter text; easiest way is to go to Insert>Paragraph>Before or After Cursor, depending on what you want to do
 +
* Part 7: Maple now re-writes a line such as <code>g(x):=10*sin(x)</code> to be <code>g(x):=x->10*sin(x)</code>
  
== Convolution and Correlation Examples ==
+
== Examples From Chapter 1 ==
In the Resources section on Sakai, and specifically in the <code>Demo Codes - F21</code> folder there is a folder of Convolution examples and a folder of Correlation examples.  The main things to know are:
+
The following will take you through steps to find answers to several different kinds of problem in the classThe parts in this section come from examples in Chapter 1 of "Signals and Systems: Theory and Applications" by Fawwaz Ulaby and Andrew Yagle and available for free at [[http://ss2.eecs.umich.edu ss2.eecs.umich.edu]]
* <code>restart</code>
 
: This should always be the first executable in your worksheet; you can put your name and other text above it but you should always restart at the top in case you need to...restart your worksheet from scratch.
 
* <code> NumericEventHandler(invalid_operation=`Heaviside/EventHandler`(value_at_zero=1)):</code>
 
: This simply tells Maple to define the unit step function at 0 to be equal to 1 rather than leaving it undefinedIt makes some functional representations much cleaner later.  If this is left out, generally things will still work but there might be some undefined values in your function.
 
* <code>u:=t->Heaviside(t)</code>
 
* <code>r:=t->t*u(t)</code>
 
* <code>q:=t->t^2/2*u(t)</code>
 
* <code>c:=t->t^3/6*u(t)</code>
 
: These just define the unit step and its first three integrals in case we want to use them later.  Note that when you type the code into Maple it will put you into superscript mode when you type the ^ symbol; to get out of it, press the right arrow.  Also, when you type the / key, Maple will put you in the denominator of a fraction; to get out of it, press the right arrow.
 
* <code>y:=t->int(x(t-tau)*h(tau), tau=-infinity..infinity)</code>
 
: Assuming <code>x(t)</code> and <code>h(t)</code> are defined, this will calculate the convolution of $$x$$ and $$h$$ for a given $$t$$.  If you want to see the analytical formula for the convolution, you can type <code>y(t)</code>
 
* <code> convert(y(t), piecewise</code>
 
: This will present the function as a table of the piecewise functions and the relational operators that are true when those pieces are valid.
 
* <code>combine()</code> ans/or <code>simplify()</code>
 
: This tries to simplify whatever is inside the argument.  It is not always successful...
 
* <code>subs(a=1, b=2, c=3, THING)</code>
 
: Substitutes in values without permanently assigning anything. Note that the substitutions need to be equations without any kind of brackets. Further note that the substitutions are only made to the last item, not all the subsequent items, so if you try <code>subs(a=1, b=a, c=b, a+b+c)</code> you would end up getting "1 + a + b" in return, whereas <code>subs(c=b, b=a, a=1, a+b+c)</code> gives you 3.
 
* <code>maximize(f(x))</code>
 
: Finds the maximum value of the function of $$x$$.
 
* <code>evalf(THING)</code>
 
: Evaluates THING as a floating point number; this is useful in cases where Pi is involved as well as cases where Maple reports back a rational fraction.
 
  
== Turning In Work ==
+
=== Initializing Worksheets ===
For this assignment you will be turning in a PDF of your work to Gradescope and you will be uploading your mw worksheet to SakaiTo make a PDF, go to File -> Export As and then in the "Files of Type" change the option to PDFTo upload to Sakai, go to Sakai go to the class, and then click "Drop Box" at left. Once there, click the "Action" drop down and make a new folder called "HW 4" - once that exists, click "HW 4"'s action button and upload the .mw file thereNote that since the files live in the container, you will want to do all this using the Firefox browser that is included in the container.  Alternately, you can use the Firefox browser to go to box.duke.edu, upload things from the container to your box drive, download things from your box drive to your own computer, and upload things from there.  That doesn't sound tedious ''at all!!!''
+
Your worksheets should always start with the following:
 +
<syntaxhighlight lang=maple>
 +
restart</syntaxhighlight>
 +
so that if you re-run the whole worksheet, Maple will start from scratch.
 +
 
 +
Later in this example, you will be using the <code>display</code> command which comes from a module named <code>plots</code>.  As a result, just after the <code>restart</code> command, you should have:
 +
<syntaxhighlight lang=maple>
 +
with(plots):</syntaxhighlight>
 +
 
 +
=== Singularity Functions ===
 +
* Maple has a version of the continuous step function already available - it is called <code>Heaviside</code> and is defined as 0 when the argument is <0, 1 when the argument is >0, and undefined when the argument is 0.  Maple can handle most graphics and math even with this undefined instant.  If you need to explicitly define the value of the Heaviside function at 0, you can do that with:
 +
** <code> NumericEventHandler(invalid_operation=`Heaviside/EventHandler`(value_at_zero=1)):</code>
 +
* Because you will probable not want to type out that entire word, you may want to start your Maple script with some basic definitions of singularity functionsNote the semi-colons are in the lines below so you ca n copy and paste the section into a single command line in Maple.
 +
<syntaxhighlight lang=maple>
 +
u:=t->Heaviside(t);
 +
r:=t->t*u(t);
 +
q:=t->t^2/2*u(t);
 +
c:=t->t^3/6*u(t);
 +
</syntaxhighlight>
 +
 
 +
=== Defining and Plotting Functions ===
 +
One of the example functions in the Ulaby book is in Figure 1-8; we will be using this to show some signal transformations.  The signal is made up of a ramp function of amplitude 5 that is on for 2 secondsOne way to define this is:
 +
<syntaxhighlight lang=maple>
 +
x := t -> 5*r(t)-5*r(t-2)-10*u(t-2)
 +
</syntaxhighlight>
 +
If you want to make a quick plot of it, you can use:
 +
<syntaxhighlight lang=maple>
 +
plot(x(t), t=-1..5)
 +
</syntaxhighlight>
 +
If you want to make something that more closely resembles Figure 1-8, you can do that by putting multiple functions in the plot command:
 +
<syntaxhighlight lang=maple>
 +
plot([x(2*t), x(t), x(t/2)], t=-1..5)
 +
</syntaxhighlight>
 +
 
 +
Another example function is used on Figures 1-7 and 1-9.  That one is a little more complicated, but can be written (approximately) as:
 +
<syntaxhighlight lang=maple>
 +
x2 := t -> 2.8*sqrt(t)*u(t) + ((-1)*2.8*sqrt(t) + 5.6*exp(-1/3*t + 4/3))*u(t - 4)
 +
</syntaxhighlight>
 +
If you want to get something that resembles Figure 1-7, you can do that with:
 +
<syntaxhighlight lang=maple>
 +
plot([x2(t + 10), x2(t), x2(t - 10)], t = -11 .. 30, legend = ["x(t+10)", "x(t)", "x(t-10)"])
 +
</syntaxhighlight>
 +
Note the addition of a legend.
 +
 
 +
To get Figure 1-9, you can use:
 +
<syntaxhighlight lang=maple>
 +
plot([x2(-t), x2(t)], t = -20 .. 20, color = ["green", "red"], legend = ["x(-t)", "x(t)"])
 +
</syntaxhighlight>
 +
Note the addition of color information; up until now, Maple's default color choices have matched the book. This time, however, they did not.
 +
 
 +
Figure 1-10 has a new function $$x(t)=r(t+3)-r(t+2)-u(t)+e^{-t}u(t)$$ We can get that with:
 +
<syntaxhighlight lang=maple>
 +
x3 := t -> r(t + 3) - r(t + 2) - u(t) + exp(-t)*u(t)
 +
</syntaxhighlight>and then create an array of stacked plots with:
 +
<syntaxhighlight lang=maple>
 +
A := Array(1 .. 3, 1 .. 1);
 +
A[1, 1] := plot(x(t), t = -5 .. 6, title = "x(t)");
 +
A[2, 1] := plot(x(-2*t), t = -5 .. 6, title = "z(t)=x(-2t)");
 +
A[3, 1] := plot(x(-2*(t - 3)), t = -5 .. 6, title = "y(t)");
 +
display(A, size = [300, 200]);
 +
</syntaxhighlight>
 +
 
 +
For Figure 1-11, first note that the equations in the book are only valid if $$x(t)=0$$ for $$t<=$$; if that is not the case, you ned to use $$x_e(t)=\frac{x(t)+x(-t)}{2}$$ and $$x_o(t)=\frac{x(t)-x(-t)}{2}$$.  Then you would want to write something like:
 +
<syntaxhighlight lang=maple>
 +
B := Array(1 .. 3, 1 .. 1);
 +
B[1, 1] := plot(x2(t), t = -12 .. 12, title = "x(t)");
 +
B[2, 1] := plot((x2(t) + x2(-t))/2, t = -12 .. 12, title = "Even Part");
 +
B[3, 1] := plot((x2(t) - x2(-t))/2, t = -12 .. 12, title = "Odd Part");
 +
display(B, size = [300, 200]);
 +
</syntaxhighlight>
 +
 
 +
To create and plot a periodic function, you basically need to define one period as a function and then define another function that passes an argument based on the remainder of time with the period of the function.  Note that Maple's <code>mod</code> command is meant for polynomials and not floating point evaluation.  I recommend creating a new command called <code>nmod()</code> to figure out the remainder part and then use it with periodic functions:
 +
<syntaxhighlight lang=maple>
 +
nmod := (t, T) -> t/T - floor(t/T);
 +
x4a := t -> 5*r(t) - 5*r(t - 2) - 10*u(t - 2)
 +
x4b := t -> x4a(nmod(t, 2))
 +
plot(x4b(t), t = -5 .. 7)
 +
</syntaxhighlight>
 +
 
 +
== Examples From Chapter 2 ==
 +
The parts in this section come from examples in Chapter 2 of "Signals and Systems: Theory and Applications" by Fawwaz Ulaby and Andrew Yagle and available for free at [[http://ss2.eecs.umich.edu ss2.eecs.umich.edu]]
 +
 
 +
=== Initializing Worksheets / Defining Singularity Functions ===
 +
Once again, your worksheet should start with the following to restart everything, load the plots module, and define singularity functions:
 +
<syntaxhighlight lang=maple>
 +
restart;
 +
with(plots):
 +
NumericEventHandler(invalid_operation=`Heaviside/EventHandler`(value_at_zero=1)):
 +
u:=t->Heaviside(t);
 +
r:=t->t*u(t);
 +
q:=t->t^2/2*u(t);
 +
c:=t->t^3/6*u(t);
 +
</syntaxhighlight>
 +
 
 +
 
 +
=== Defining Convolution ===
 +
* If you have two functions defined, you can perform convolution in Maple by using the convolution integral; for example, if you want to convolve $$x(t)=u(t)-u(t-3)$$ and $$h(t)=e^{-t}u(t)$$, you can write:<syntaxhighlight lang=maple>
 +
x := t -> u(t) - u(t - 3);
 +
h := t -> exp(-t)*u(t);
 +
int(x(tau)*h(t - tau), tau = -infinity .. infinity);</syntaxhighlight>
 +
* If you want to plot that, you could change the last time above to a function and then plot $$x(t)$$ and $$y(t)$$ on the same graph:<syntaxhighlight lang=maple>
 +
y := t -> int(x(tau)*h(t - tau), tau = -infinity .. infinity);
 +
plot([x(t), y(t)], t = -1 .. 8, color=["blue", "red"], size=[300, 300],legend = ["x(t)", "y(t)"]);</syntaxhighlight>where the colors are set simply to match Ulaby's color scheme while also having $$x(t)$$ show up first in the legend...
 +
* You can also create a function that will perform convolution by receiving two functions as arguments, changing the independent variable on those functions, and performing convolutions.  That is to say:<syntaxhighlight lang=maple>
 +
convolve := (a, b) -> int(subs(t = tau, a)*subs(t = t - tau, b), tau = -infinity .. infinity);
 +
y := t -> convolve(x(t), h(t));
 +
y(t);</syntaxhighlight>
 +
* Typically you will get a piecewise result; if you want to see a version built with Heaviside functions you can use:<syntaxhighlight lang=maple>
 +
convert(y(t), Heaviside)</syntaxhighlight>whereas if you get a version with Heaviside functions and you want to see the piecewise version you can use:<syntaxhighlight lang=maple>
 +
convert(y(t), piecewise)</syntaxhighlight>
 +
=== Convolving and Plotting ===
 +
* The next example in the demo is from Figure 2-9 with $$x_1(t)=r(t)-2r(t-1)+r(t)$$ and $$h_1(t)=e^{-t}u(t)$$
 +
* The example after that is from Figure 2-11 with $$x_2(t)=u(t)-u(t-1)$$ and $$h_2(t)=2e^{-2t}u(t)$$
 +
* The last example is from Figure 2-23 with $$x_3(t)=r(t)-2r(t-1)+r(t-2)$$ and $$h_3(t)=u(t)-2u(t-1)+u(t-2)$$

Latest revision as of 02:40, 2 October 2023

This web page is meant to support the Maple Intro for the Fall 2023 ECE 280 class.

Accessing Maple

See the Using Maple: Duke Container section of the Maple page for how to reserve a Maple container and then use it. Containers are accessed via web browsers so you will not need any additional software to use Maple in this way.

Maple Basics

Go through the Other Pundit Pages on the Maple page, specifically:

Then go through the Connected Curriculum Project - Engineering Maple Tutor modules 1-11. Note that modules 12 and 14 may not come up much in this course but are interesting nonetheless! A few notes:

  • You do not need semicolons anymore...unless you are trying to have one line do multiple things, then those things need to be divided by semicolons
  • A colon at the end of a thing tells Maple to do that thing but suppress viewing the results
  • Part 4: F5 is no longer the shortcut to enter text; easiest way is to go to Insert>Paragraph>Before or After Cursor, depending on what you want to do
  • Part 7: Maple now re-writes a line such as g(x):=10*sin(x) to be g(x):=x->10*sin(x)

Examples From Chapter 1

The following will take you through steps to find answers to several different kinds of problem in the class. The parts in this section come from examples in Chapter 1 of "Signals and Systems: Theory and Applications" by Fawwaz Ulaby and Andrew Yagle and available for free at [ss2.eecs.umich.edu]

Initializing Worksheets

Your worksheets should always start with the following:

restart

so that if you re-run the whole worksheet, Maple will start from scratch.

Later in this example, you will be using the display command which comes from a module named plots. As a result, just after the restart command, you should have:

with(plots):

Singularity Functions

  • Maple has a version of the continuous step function already available - it is called Heaviside and is defined as 0 when the argument is <0, 1 when the argument is >0, and undefined when the argument is 0. Maple can handle most graphics and math even with this undefined instant. If you need to explicitly define the value of the Heaviside function at 0, you can do that with:
    • NumericEventHandler(invalid_operation=`Heaviside/EventHandler`(value_at_zero=1)):
  • Because you will probable not want to type out that entire word, you may want to start your Maple script with some basic definitions of singularity functions. Note the semi-colons are in the lines below so you ca n copy and paste the section into a single command line in Maple.
u:=t->Heaviside(t);
r:=t->t*u(t);
q:=t->t^2/2*u(t);
c:=t->t^3/6*u(t);

Defining and Plotting Functions

One of the example functions in the Ulaby book is in Figure 1-8; we will be using this to show some signal transformations. The signal is made up of a ramp function of amplitude 5 that is on for 2 seconds. One way to define this is:

x := t -> 5*r(t)-5*r(t-2)-10*u(t-2)

If you want to make a quick plot of it, you can use:

plot(x(t), t=-1..5)

If you want to make something that more closely resembles Figure 1-8, you can do that by putting multiple functions in the plot command:

plot([x(2*t), x(t), x(t/2)], t=-1..5)

Another example function is used on Figures 1-7 and 1-9. That one is a little more complicated, but can be written (approximately) as:

x2 := t -> 2.8*sqrt(t)*u(t) + ((-1)*2.8*sqrt(t) + 5.6*exp(-1/3*t + 4/3))*u(t - 4)

If you want to get something that resembles Figure 1-7, you can do that with:

plot([x2(t + 10), x2(t), x2(t - 10)], t = -11 .. 30, legend = ["x(t+10)", "x(t)", "x(t-10)"])

Note the addition of a legend.

To get Figure 1-9, you can use:

plot([x2(-t), x2(t)], t = -20 .. 20, color = ["green", "red"], legend = ["x(-t)", "x(t)"])

Note the addition of color information; up until now, Maple's default color choices have matched the book. This time, however, they did not.

Figure 1-10 has a new function $$x(t)=r(t+3)-r(t+2)-u(t)+e^{-t}u(t)$$ We can get that with:

x3 := t -> r(t + 3) - r(t + 2) - u(t) + exp(-t)*u(t)

and then create an array of stacked plots with:

A := Array(1 .. 3, 1 .. 1);
A[1, 1] := plot(x(t), t = -5 .. 6, title = "x(t)");
A[2, 1] := plot(x(-2*t), t = -5 .. 6, title = "z(t)=x(-2t)");
A[3, 1] := plot(x(-2*(t - 3)), t = -5 .. 6, title = "y(t)");
display(A, size = [300, 200]);

For Figure 1-11, first note that the equations in the book are only valid if $$x(t)=0$$ for $$t<=$$; if that is not the case, you ned to use $$x_e(t)=\frac{x(t)+x(-t)}{2}$$ and $$x_o(t)=\frac{x(t)-x(-t)}{2}$$. Then you would want to write something like:

B := Array(1 .. 3, 1 .. 1);
B[1, 1] := plot(x2(t), t = -12 .. 12, title = "x(t)");
B[2, 1] := plot((x2(t) + x2(-t))/2, t = -12 .. 12, title = "Even Part");
B[3, 1] := plot((x2(t) - x2(-t))/2, t = -12 .. 12, title = "Odd Part");
display(B, size = [300, 200]);

To create and plot a periodic function, you basically need to define one period as a function and then define another function that passes an argument based on the remainder of time with the period of the function. Note that Maple's mod command is meant for polynomials and not floating point evaluation. I recommend creating a new command called nmod() to figure out the remainder part and then use it with periodic functions:

nmod := (t, T) -> t/T - floor(t/T);
x4a := t -> 5*r(t) - 5*r(t - 2) - 10*u(t - 2)
x4b := t -> x4a(nmod(t, 2))
plot(x4b(t), t = -5 .. 7)

Examples From Chapter 2

The parts in this section come from examples in Chapter 2 of "Signals and Systems: Theory and Applications" by Fawwaz Ulaby and Andrew Yagle and available for free at [ss2.eecs.umich.edu]

Initializing Worksheets / Defining Singularity Functions

Once again, your worksheet should start with the following to restart everything, load the plots module, and define singularity functions:

restart;
with(plots):
NumericEventHandler(invalid_operation=`Heaviside/EventHandler`(value_at_zero=1)):
u:=t->Heaviside(t);
r:=t->t*u(t);
q:=t->t^2/2*u(t);
c:=t->t^3/6*u(t);


Defining Convolution

  • If you have two functions defined, you can perform convolution in Maple by using the convolution integral; for example, if you want to convolve $$x(t)=u(t)-u(t-3)$$ and $$h(t)=e^{-t}u(t)$$, you can write:
    x := t -> u(t) - u(t - 3);
    h := t -> exp(-t)*u(t);
    int(x(tau)*h(t - tau), tau = -infinity .. infinity);
  • If you want to plot that, you could change the last time above to a function and then plot $$x(t)$$ and $$y(t)$$ on the same graph:
    y := t -> int(x(tau)*h(t - tau), tau = -infinity .. infinity);
    plot([x(t), y(t)], t = -1 .. 8, color=["blue", "red"], size=[300, 300],legend = ["x(t)", "y(t)"]);
    where the colors are set simply to match Ulaby's color scheme while also having $$x(t)$$ show up first in the legend...
  • You can also create a function that will perform convolution by receiving two functions as arguments, changing the independent variable on those functions, and performing convolutions. That is to say:
    convolve := (a, b) -> int(subs(t = tau, a)*subs(t = t - tau, b), tau = -infinity .. infinity);
    y := t -> convolve(x(t), h(t));
    y(t);
  • Typically you will get a piecewise result; if you want to see a version built with Heaviside functions you can use:
    convert(y(t), Heaviside)
    whereas if you get a version with Heaviside functions and you want to see the piecewise version you can use:
    convert(y(t), piecewise)

Convolving and Plotting

  • The next example in the demo is from Figure 2-9 with $$x_1(t)=r(t)-2r(t-1)+r(t)$$ and $$h_1(t)=e^{-t}u(t)$$
  • The example after that is from Figure 2-11 with $$x_2(t)=u(t)-u(t-1)$$ and $$h_2(t)=2e^{-2t}u(t)$$
  • The last example is from Figure 2-23 with $$x_3(t)=r(t)-2r(t-1)+r(t-2)$$ and $$h_3(t)=u(t)-2u(t-1)+u(t-2)$$