h4kr
h4kr

Reputation: 248

Vector size mismatch in MATLAB

I have been trying to write down a code for an LTI system where the response is calculated from an input x(t) and impulse h(t) response.

For the part of the code below:

y = conv(x,h).*steps;
ty = 0:steps:7;
plot(ty,y);

I'm getting the following error message:

Error using plot
Vectors must be the same length.

I'm using ty = 0:steps:7; as h(t) is defined as exp(-t/2).*((t>=2)-(t>=7)) (since it extends upto t=7).

What does actually decide ty?

Upvotes: 1

Views: 121

Answers (1)

MichaelTr7
MichaelTr7

Reputation: 4767

Convolution Using Anonymous Functions

One way to do this process is to use anonymous functions/functions handles which are indicated by the @() that holds the input parameters in this case time, t. To create truncated signals conditional statements on t can be implemented. To have a signal ranging from t=2 to t=7 seconds truncation can be done by element-wise multiplying by ((t>=2) & (t<=7)). The t vector used to plot the final result must have a time range that is the sum of the lengths of time of the signals used in the convolution process. I believe ty is the time vector to plot the output against. In this case you must ensure ty has the same length as the output y.

Length of Results = Length of System Response + Length of Input Signal


In the case below:
x(t) → length = 1s
h(t) → length = 5s (2s to 7s)
y(t) → length = 1s + 5s = 6s (2s to 8s)

Convolution Results

Step_Size = 0.1;
Start_Time = 0; End_Time = 7;
t = Start_Time: Step_Size: End_Time;

%Input into system x(t)%
x = @(t) 1.0.*(t >= 0 & t <= 1);
subplot(3,1,2); fplot(x);
title('x(t): Input into system');
xlabel('Time (s)'); ylabel('Amplitude');
xlim([0 10]);
ylim([0 1.1]);

%System impulse response h(t)%
h = @(t) exp(-t/2).*((t>=2) & (t<=7));
subplot(3,1,1); fplot(h);
title('h(t): System impulse response');
xlabel('Time (s)'); ylabel('Amplitude');
xlim([0 10]);

y = conv(x(t),h(t)).*Step_Size;
t = linspace(0,2*max(t),length(y));
subplot(3,1,3); plot(t,y);
title('y(t): Output/System Response');
xlabel('Time (s)'); ylabel('Amplitude');

Upvotes: 1

Related Questions