-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelID.m
59 lines (56 loc) · 1.32 KB
/
modelID.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
close all
clearvars
clc
%Performs model estimations imposing a linear time-delayed model,
%the estimation of the model parameters uses a simple
%generic genetic algorithm
%Guel-Cortez 2022
%
clearvars
close all
file=sprintf('pwm2.csv'); %we upload the data
[t,u,y] = csvimport(file, 'columns', {'Time (s)','PWM','Pressure'});
%
%
%% Initial experimental data
%Model estimations
%% Model 1
tx=t((t>=80)& (t<=131));
yx=y((t>=80)& (t<=131))-2.85;
ux=u((t>=80)& (t<=131));
figure
yyaxis left
plot(tx,ux)
yyaxis right
plot(tx,yx)
%
options = optimoptions('ga','PlotFcn',"gaplotbestf",'UseParallel', true,'MaxStallGenerations',500,'MaxGenerations',1000);
[x,fval] = ga(@(K) Pestimation(tx,ux,yx,K),4,[],[],[],[],[0;0;0;0],[100;100;100;5],[],options);
%
[ye,sol]=Pestimation(tx,ux,yx,x);
figure
yyaxis left
plot(tx,ux,'r')
yyaxis right
plot(tx,yx,'b')
hold on
plot(tx,ye,'k')
xlim([tx(1),tx(end)])
num = sol(1);
den = [1 sol(2) sol(3)];
tau1=sol(4);
sys1 = tf(num,den,'InputDelay',sol(4)); %respuesta
%
function varargout= Pestimation(t,u,y,K)
num = K(1);
den = [1 K(2) K(3)];
sys = tf(num,den,'InputDelay',K(4));
ye = lsim(sys,u,t);
%cost = norm(y - ye,2)^2+0.5*norm(K,2)^2;
cost = norm(y - ye,2)^2;%+0.5*norm(K,2)^2;
if abs(nargout)==1
varargout={cost};
else
varargout={ye,K};
end
end