Convolution matrix dimension error, must agree

1 view (last 30 days)
so im doing this program but i dont see why its not working, driving me nuts... i kepp getting this message: "Error using .* Matrix dimensions must agree.
Error in Convo1 (line 12) z= median(diff(t).*conv(x1,h,'same'));"
if true
clc;
t= linspace(-4,4,16000);
x1= (t.^2/6).*heaviside(t);
x2= (t.^3+3.*t).*heaviside(t);
x3= log(3.*t-1).*heaviside(3.*t-1);
h= heaviside(t);
z= median(diff(t).*conv(x1,h,'same')); error here-----
z2= median(diff(t).*conv(x2,h,'same'));
z3= median(diff(t).*conv(x3,h,'same'));
%plot
figure;
subplot(4,1,1);
plot(t,x1,'g',t,x2,'r',t,x3,'b');
subplot(4,1,2);
plot(t,z);
subplot(4,1,3);
plot(t,z1);
subplot(4,1,4);
plot(t,z3);
end
Can you guys tell me what im doing wrong and if im doing the convolution ok??

Answers (1)

Walter Roberson
Walter Roberson on 22 Oct 2015
x1 is the same size as t, and the conv() with 'same' is going to be the same size as that. But diff(t) is going to be one element shorter: diff(t) for a vector is (t(2:end)-t(1:end-1)). You cannot element-by-element multiply two vectors of different lengths.
  2 Comments
alan gonzales
alan gonzales on 22 Oct 2015
i totally understand that i cant multiply two vectors of different lengths. i did not know that diff(t) will be shorter TY how do i fix this=?
Walter Roberson
Walter Roberson on 22 Oct 2015
Considering that you are implicitly operating with respect to t, what were you thinking diff(t) would mean? If you were to switch to symbolic notation for a moment instead of particular numeric t, then diff(t) with symbolic t would mean diff(t,t) which would be 1.
If you were thinking in terms of dx1/dt then you would need to be using diff on symbolic x1(t), diff(x1(t),t) rather than diff(t). It would at least be possible to compute that symbolically and then evaluate it at particular t. For example diff(x2(t),t) comes out as 3*Heaviside(t)*(t^2+1)
Your x3 has difficulty at t = 1/3 exactly, as it depends upon how you define Heaviside(0) . Some people define it as 0 but others define it as 1 but the most common is to define it as 1/2 . If you use 1/2 then diff(x3(t),t) at t = 1/3 is -infinity whereas at t=1/3 + epsilon is +1/epsilon for non-zero epsilon.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!