Simple Convolution Plot Errors

2 views (last 30 days)
I am trying to plot a convolution example from my text book. Somewhere in the setting up of the plot dimensions/colors etc i made an error and i cannot find it. any help would be greatly appreciated.
The current compile error is on
axis ([tau (1) tau(end) -2.0 2.5]);
thank you.
% MS2p4.m : MATLAB Session 2, Program 4
% Script M-file graphically demonstrates the convolution process.
clc
clear
figure (1) % Create figure window and make visible on screen
x = inline('1.5*sin(pi*t).*(t>=0 & t<1)');
h = inline('1.5*(t>=0&t<1.5) - (t>=2&t<2.5)');
dtau = 0.005; tau = -1:dtau:4;
ti = 0; tvec = -.25:.1:3.75;
y = NaN*zeros(1, length (tvec)); % Pre-allocate memory
for t = tvec,
ti = ti+1; % Time index
xh = x(t-tau).*h(tau); lxh = length (xh);
y(ti) = sum(xh.*dtau); % Trapezoidal approximation of interal
subplot (2,1,1), plot(tau, h(tau), 'k-', tau, x(t-tau), 'k--', t, 0, 'ok');
axis ([tau (1) tau(end) -2.0 2.5]);
patch([tau(1:end-1); tau(1:end-1); tau (2:end); tau (2:end)],...
[zeros(1,lxh-1);xh(1:end-1);xh(2:end);zeros(1,lxh-1)],...
[.8 .8 .8], 'edgecolor', 'none');
c = get(gca, 'children'); set (gca, 'children', [c(2); c(3); c(4); c(1)]);
subplot (2, 1, 2), plot (tvec, y, 'k', tvec (ti), y(ti), 'ok');
xlabel ('t'); ylabel ('y(t) = \int h(\tau)x(t-\tau) d\tau');
axis ([tau(1) tau(end) -1.0 2.0]); grid;
drawnow;
axis ([tau(1) tau(end) -1.0 2.0]); grid;
drawnow;
end

Accepted Answer

Image Analyst
Image Analyst on 26 Feb 2016
Edited: Image Analyst on 26 Feb 2016
You have a space between tau and (1) and (2:end), so it thinks there are two separate things, and then you end up having more than 4 elements in between the brackets. Get rid of that space. Fixed code:
axis ([tau(1) tau(end) -2.0 2.5]);
patch([tau(1:end-1); tau(1:end-1); tau(2:end); tau(2:end)],...
[zeros(1,lxh-1);xh(1:end-1);xh(2:end);zeros(1,lxh-1)],...
[.8 .8 .8], 'edgecolor', 'none');

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!