ECE 280/Fall 2021/HW 02
Appearance
This page is meant as a support page for HW 02. The codes and graphs are for zyBook Challenge Activity 1.2.3
Using MATLAB and Python
Using MATLAB
There are a few options when it comes to using MATLAB:
- You can install it for free on your own computer - see MATLAB#Installation
- You can use MATLAB Online once you have a MathWorks account associated with Duke. You will need to go through the first part of the MATLAB at Duke page to get the account set up. Once that is done, go to MATLAB Online and log in with your MathWorks credentials. Note that all your files will be on the MATLAB cloud; to access them directly from your computer, install the MATLAB Drive Connector
- You could use MATLAB in a Duke Container but this is currently MATLAB R2014a and will not work with Simulink so...it's a no go for now.
Using Python
I generally recommend using Anaconda for Python, and specifically using the Spyder IDE. You can download Anaconda at the Individual Edition Download link. To start Python:
- Windows:
- Go to the Start menu -> Anaconda3 -> Spyder, or
- Go to the Start menu -> Anadonda3 -> Anaconda Navigator and the click Spyder, or if those don't work,
- Go to the Start menu -> Anaconda3 -> Anaconda prompt and then when it comes up type
spyderand hit return
- macOS:
- Search for Anaconda, start Anaconda Navigator and the click Spyder, or if that doesn't work,
- Search for
terminal, start a terminal, and then when it comes up typespyderand hit return
Turning The Assignment In
For this assignment, you will be attaching 8 files:
- The MATLAB code (text files ending in .m) for 1.2.11 and 1.4.11
- The MATLAB graphs (in .png format) for 1.2.11 and 1.4.11
- The Python code (text files ending in .py) for 1.2.11 and 1.4.11
- The Python graphs (in .png format) for 1.2.11 and 1.4.11
Make sure the names of the Python graphs are different from the names of the MATLAB graphs! Yuo will upload all eight things to the HW 2 Part 3 assignment in gradescope.
MATLAB

%% Initialize workspace
clear
%% Define singularity functions
u = @(t) (t>=0)*1;
r = @(t) t.*u(t);
%% Define x(t)
x = @(t) r(t+1) - r(t-1) - 2*u(t-2);
%% Create time base
t = linspace(-4, 3, 1000);
%% Initialize figure
figure(1)
clf
%% Make plots
plot(t, x(t), 'k-', 'linewidth', 4), hold on
plot(t, x(3*t), 'r--', 'linewidth', 3)
plot(t, x(-3*t), 'g-.', 'linewidth', 2)
plot(t, x(-t-1), 'b:', 'linewidth', 1), hold off
%% Add label and legend
xlabel('t')
legend('x(t)', 'x(3t)', 'x(-3t)', 'x(-t-1)', 'location', 'best')
%% Save figure
print -dpng CA010203matlab_plot
Python

# %% Import modules
import numpy as np
import matplotlib.pyplot as plt
# %% Define singularity functions
u = lambda t: (t>=0)*1
r = lambda t: t*u(t)
# %% Define x(t)
x = lambda t: r(t+1) - r(t-1) - 2*u(t-2)
# %% Create time base
t = np.linspace(-4, 3, 1000)
# %% Initialize figure and axes
fig = plt.figure(num=1, clear=True)
ax = fig.add_subplot(1, 1, 1)
# %% Make plots
ax.plot(t, x(t), 'k-', linewidth=4)
ax.plot(t, x(3*t), 'r--', linewidth=3)
ax.plot(t, x(-3*t), 'g-.', linewidth=2)
ax.plot(t, x(-t-1), 'b:', linewidth=1)
# %% Add label and legend
ax.set(xlabel='t')
ax.legend(['x(t)', 'x(3t)', 'x(-3t)', 'x(-t-1)'], loc='best')
# %% Save figure
fig.tight_layout()
fig.savefig("CA010203python_plot.png")