Difference between revisions of "ECE 280/PlotDemo"

From PrattWiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
The following is a demonstration of how to perform similar tasks in [[Maple]] and [[MATLAB]].  Specifically, this example shows how to define the unit step and ramp functions, use them to define accumulated signals, create functions to calculate the integrals of those signals, and plot both the signals and their integrals.  The comments are meant to show the different sections of each piece of code.  The resulting figures are connected with each particular program.
+
The following is a demonstration of how to perform similar tasks in [[Python]], [[Maple]], and [[MATLAB]].  Specifically, this example shows how to define the unit step and ramp functions, use them to define accumulated signals, create functions to calculate the integrals of those signals, and plot both the signals and their integrals.  The comments are meant to show the different sections of each piece of code.  The resulting figures are connected with each particular program.
 +
 
 +
== Python ==
 +
<html>
 +
<iframe src="https://trinket.io/embed/python3/f6df9d4af3" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 +
</html>
  
 
== MATLAB ==
 
== MATLAB ==
=== Code ===
 
 
<source lang="matlab">
 
<source lang="matlab">
 
% MATLAB and Maple Demo
 
% MATLAB and Maple Demo
 
% M. R. Gustafson II
 
% M. R. Gustafson II
% Fall 2013
 
  
 
%% Initialize
 
%% Initialize
Line 12: Line 15:
  
 
%% Define step and ramp functions
 
%% Define step and ramp functions
U = @(t) (t>0);
+
ustep = @(in) (1.0).*(in>=0);
R = @(t) t .* U(t);
+
uramp = @(in)  (in).*(in>=0);
  
 
%% Define x and y using accumulation
 
%% Define x and y using accumulation
X = @(t) (-1)*R(t+1)+U(t)+(2)*R(t)+(-1)*R(t-1)+(-1)*R(t-2)+(1)*R(t-3);
+
X = @(t) (-1)*uramp(t+1)+ustep(t)+(2)*uramp(t)+(-1)*uramp(t-1)+(-1)*uramp(t-2)+(1)*uramp(t-3);
Y = @(t) U(t+2)+(-2)*U(t+1)+R(t)+(-1)*R(t-1)+U(t-1)-U(t-2);
+
Y = @(t) ustep(t+2)+(-2)*ustep(t+1)+uramp(t)+(-1)*uramp(t-1)+ustep(t-1)-ustep(t-2);
  
 
%% Plot signals
 
%% Plot signals
Line 25: Line 28:
 
     t, Y(t), 'b:', ...
 
     t, Y(t), 'b:', ...
 
     t, X(t).*Y(t), 'g--')
 
     t, X(t).*Y(t), 'g--')
legend('x(t)', 'y(t)', 'x(t) y(t)', 0)
+
legend('x(t)', 'y(t)', 'x(t) y(t)', 'location','best')
 
xlabel('t')
 
xlabel('t')
 
gzoom
 
gzoom
Line 39: Line 42:
 
     t, IntY(t), 'b:', ...
 
     t, IntY(t), 'b:', ...
 
     t, IntXY(t), 'g--')
 
     t, IntXY(t), 'g--')
legend('\int x(t)', '\int y(t)', '\int x(t) y(t)', 0)
+
legend('\int x(t)', '\int y(t)', '\int x(t) y(t)', 'location','best')
 
xlabel('t')
 
xlabel('t')
 
gzoom
 
gzoom
  
%% Save plots (not in Maple)
+
%% Save plots
 
figure(1); print -dpng SignalPlot
 
figure(1); print -dpng SignalPlot
 
figure(2); print -dpng IntegralPlot</source>
 
figure(2); print -dpng IntegralPlot</source>
=== xMaple ===
+
 
 +
 
 +
[[File:SignalPlot.png|300px|MATLAB Signals]][[File:IntegralPlot.png|300px|MATLAB Integrals]]
 +
== xMaple ==
 
Note - for the xMaple code, if you copy and paste it, all the code will go in one execution group (thus the ; at the end of each line).  I haven't figured out the smart way to put things in multiple lines...
 
Note - for the xMaple code, if you copy and paste it, all the code will go in one execution group (thus the ; at the end of each line).  I haven't figured out the smart way to put things in multiple lines...
 
<source lang="text">
 
<source lang="text">
 
# Maple and MATLAB Demo
 
# Maple and MATLAB Demo
 
# M. R. Gustafson II
 
# M. R. Gustafson II
# Fall 2013
 
 
# Initialize
 
# Initialize
 
restart;
 
restart;
Line 70: Line 75:
 
</source>
 
</source>
  
 +
[[File:SignalPlotMaple.png|300px|xMaple Signals]][[File:IntegralPlotMaple.png|300px|xMaple Integrals]]
  
=== Plots ===
+
[[Category:ECE 280]]
==== MATLAB ====
 
[[File:SignalPlot.png|300px|MATLAB Signals]][[File:IntegralPlot.png|300px|MATLAB Integrals]]
 
==== xMaple ====
 
[[File:SignalPlotMaple.png|300px|xMaple Signals]][[File:IntegralPlotMaple.png|300px|xMaple Integrals]]
 

Latest revision as of 05:22, 4 February 2023

The following is a demonstration of how to perform similar tasks in Python, Maple, and MATLAB. Specifically, this example shows how to define the unit step and ramp functions, use them to define accumulated signals, create functions to calculate the integrals of those signals, and plot both the signals and their integrals. The comments are meant to show the different sections of each piece of code. The resulting figures are connected with each particular program.

Python

MATLAB

% MATLAB and Maple Demo
% M. R. Gustafson II

%% Initialize
clear

%% Define step and ramp functions
ustep = @(in) (1.0).*(in>=0);
uramp = @(in)  (in).*(in>=0);

%% Define x and y using accumulation
X = @(t) (-1)*uramp(t+1)+ustep(t)+(2)*uramp(t)+(-1)*uramp(t-1)+(-1)*uramp(t-2)+(1)*uramp(t-3);
Y = @(t) ustep(t+2)+(-2)*ustep(t+1)+uramp(t)+(-1)*uramp(t-1)+ustep(t-1)-ustep(t-2);

%% Plot signals
t = linspace(-4, 4, 1e4);
figure(1); clf
plot(t, X(t), 'r-', ...
     t, Y(t), 'b:', ...
     t, X(t).*Y(t), 'g--')
legend('x(t)', 'y(t)', 'x(t) y(t)', 'location','best')
xlabel('t')
gzoom

%% Calculate integrals
IntX  = @(t) cumtrapz(t, X(t));
IntY  = @(t) cumtrapz(t, Y(t));
IntXY = @(t) cumtrapz(t, X(t).*Y(t));

%% Plot integrals
figure(2); clf
plot(t, IntX(t), 'r-', ...
     t, IntY(t), 'b:', ...
     t, IntXY(t), 'g--')
legend('\int x(t)', '\int y(t)', '\int x(t) y(t)', 'location','best')
xlabel('t')
gzoom

%% Save plots
figure(1); print -dpng SignalPlot
figure(2); print -dpng IntegralPlot


MATLAB SignalsMATLAB Integrals

xMaple

Note - for the xMaple code, if you copy and paste it, all the code will go in one execution group (thus the ; at the end of each line). I haven't figured out the smart way to put things in multiple lines...

# Maple and MATLAB Demo
# M. R. Gustafson II
# Initialize
restart;
# Define step and ramp functions
U := t-> Heaviside(t); 
R := t->  t*U(t); 
# Define x and y using accumulation
X := t->  -R(t+1)+U(t)+2*R(t)-R(t-1)-R(t-2)+R(t-3); 
Y := t->  U(t+2)-2*U(t+1)+R(t)-R(t-1)+U(t-1)-U(t-2); 
# Plot signals
plot([X(t), Y(t), X(t)*Y(t)], t = -4 .. 4, linestyle = [1, 2, 3], legend = ['x(t)', 'y(t)', 'x(t)*y(t)']);
# Calculate integrals
IntX := t->  int(X(tau), tau = -infinity .. t);
IntY := t->  int(Y(tau), tau = -infinity .. t);
IntXY := t->  int(X(tau)*Y(tau), tau = -infinity .. t);
# Plot integrals
plot([IntX(t), IntY(t), IntXY(t)], t = -4 .. 4, linestyle = [1, 2, 3], legend = ['int*x(t)', 'int*y(t)', 'int*x(t)*y(t)']);

xMaple SignalsxMaple Integrals