Vectors must be the same length.

Hi,
I am getting this error here:
Error using plot
Vectors must be the same length.
Error in test (line 32)
plot(t,u);ylabel('Input'); axis([-202 max(t) [-201 202]*max(u)*1.1])
The code I am using is the below:
dt = 0.01 % sample rate
sim('speed') % run the simulation it will save the data as y and u -
dat =iddata(y,u,dt); % IDDATA(output,input,sample_time)
order = [2 1 1] % ["number of a" "number of b" "pure delay"]
mod = arx(dat,order) % Mod = ARX(DATA,ORDER)
a = mod.a; b=mod.b; % a is the denominatar, b the numerator polynomial in z-1
yhat = filter(b,a,u); % calculate the filter output (i.e. y = b/a * u)
e = y-yhat; % calculate the error
close all; % close all old figure windows
scrsz = get(groot,'ScreenSize'); % How big is the screen (so we can come up with a figure window size)
fig=figure('Position',[50 100 scrsz(3)*2/5 scrsz(4)*3/4]);
% Plot the input
subplot('position',[0.1 0.8 0.85 0.15]);
plot(t,u);ylabel('Input'); axis([-202 max(t) [-201 202]*max(u)*1.1])
name=['System ID Results for Model Order = ' num2str(order)];
title(name);
% Plot the output and the model
subplot('position',[0.1 0.35 0.85 0.40]);
plot(t,y,t,yhat);
ylabel('Output');legend('Actual - y','Model - yhat','Location','NW');
% Plot the error
subplot('position',[0.1 0.1 0.85 0.20]);
plot(t,e);ylabel('Model Error');xlabel('time [s]');
% some methods of assessing the model:
Sum_of_Squared_Errors = e'*e % the model with smallest number is "best" fit
Coefficient_of_determination = 1-(e'*e)/((y-mean(y))'*(y-mean(y))) % (range 0 to 1) will tend to 1 as the model fit improves
AIC = aic(mod) % look up in help - trades off fit to the data against number of parameters
figure(2);histfit(e,21,'normal') % plot the error distribution
title('Distribution of Model Error');
xlabel('Error')
Thanks in advance!

7 Comments

Check
size(t)
size(u)
before the command
plot(t,u)
If they are not equal, an error will pop up.
Ah ok, I see. You are correct the size is not equal:
ans = 201 1
33 size(t)
ans = 501 1
How can use the same size to resolve this isue?
You need to get to the root of the problem. WHY are they not the same size even though you expect them to be?
thanks for your help, I think it is expected to not be the same size as the data keep changing.
I have used the numel function to resolve this matter and it seems that it works
Is there any alternative solution apart from numel?
@I Bos numel (or length) is the typical way.
In your case, the time vector spans 5 seconds but your data is less than 5 seconds.
So either you defined time vector arbitrarily or data is missing?
BUT if you are confident that the sample rate is 1/dt, then you can just redifine a time vector.
u=rand(10,1);
t0=0; % initial time
dt=0.01;
fs=1/dt;
t2 = t0+(0:numel(u)-1)/fs;
An alternative is to pad data with 0's if you know data is missing at the front or at the end: Pad array - MATLAB padarray (mathworks.com)
@AndresVar thanks very much for your help.
How the Padding works in my case, as I would need to add 300 '0's at the end of the u.
Many thanks again.
You would use a "direction" of 'post' to add at the end of something.

Sign in to comment.

Answers (0)

Categories

Asked:

on 26 Feb 2022

Commented:

on 27 Feb 2022

Community Treasure Hunt

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

Start Hunting!