Vectors must be the same length.
Mostrar comentarios más antiguos
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 comentarios
Torsten
el 26 de Feb. de 2022
Check
size(t)
size(u)
before the command
plot(t,u)
If they are not equal, an error will pop up.
I Bos
el 26 de Feb. de 2022
Image Analyst
el 26 de Feb. de 2022
You need to get to the root of the problem. WHY are they not the same size even though you expect them to be?
I Bos
el 26 de Feb. de 2022
@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)
I Bos
el 27 de Feb. de 2022
Walter Roberson
el 27 de Feb. de 2022
You would use a "direction" of 'post' to add at the end of something.
Respuestas (0)
Categorías
Más información sobre Test and Measurement en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!