为什么我的代码返回的值与我给出的值相同?

时间:2019-06-27 15:16:26

标签: matlab

我正在尝试使用'nlgreyest'进行非线性灰箱估计以估计一组参数,但是代码无法运行多个迭代并返回与我给定的相同值。

我尝试更改数据集,参数值的初始猜测以及终止选项,但均未成功。

为ODE文件创建数据的功能

%6-12-19


function dx = DOF2damped(t, x)
u0 = .1; %amplitude ()
omega = 1; %hz
m1 = 0.85048569375; %kg
m2 = 0.7370876; %kg
k1 = 1; % N/m 35
k2 = 356.9085; % N/m
c1 = 0.2; %N/m*s
c2 = 0.2; %N/m*s
u = [u0*sin(omega*t); u0*omega*cos(omega*t)];

%y = [x(1);x(2);x(3);x(4)]; %x1, x1dot, x2, x2dot
dx = [x(2);(((-k1 - k2)/m1)*x(1) + ((-c1 - c2)/m1)*x(2) + (k2/m1)*x(3) + (c2/m1)*x(4) +(k1/m1)*u(1) + (c1/m1)*u(2))
    ;x(4); ((k2/m2)*x(1) + (c2/m2)*x(2) + (-k2/m2)*x(3) + (-c2/m2)*x(4))];
end

下面显示的ODE文件

% Generate Data for 2 DOF Mass Spring damper system
% Ben 
%6-12-19
%6-25-19 updated
%% Requirements
% DOF2damped.m
% variables_2DOF.m
%% clearing 
clear 
close all
clc
%% flag (set =/= 1 if plots are unnecessary)
flag_plot = 0;
%% code

variables_2DOF %calls variable function

tspan = linspace(0,5,5000); %time span from 0 to 5 with 5000 data points


x0 = [0;0;0;0]; %initial conditions
[t, x] = ode45(@(t,x)DOF2damped(t, x), tspan, x0); %ode function, solve for t, x
u = [(u0*sin(omega*t)), (u0*omega*cos(omega*t))]; %input [position, velocity]

%% visuals
if flag_plot ==1
    figure
    plot(t,x(:,1)) %x1 position
    hold on
    plot(t,x(:,3)) %x2 position
    figure
    plot(t,u(:,1)) %input position
    hold on
    plot(t,u(:,2)) %input velo
end
%% data saving
X = x(:,[1,3]); %positions of masses 1 and 2
U = u; 
datafilename = '2DOF_data_file.mat'
save(datafilename,'X','U')

如下所示在nlgrey模型中使用的功能

%6-20-19

function [dx, y] = DOF2greyfxn(t, x, u, m1, m2, k1, k2, c1, c2, varargin)


y = [x(1);x(3)];
dx = [x(2);(((-k1 - k2)/m1)*x(1) + ((-c1 - c2)/m1)*x(2) + (k2/m1)*x(3) + (c2/m1)*x(4) +(k1/m1)*u(1) + (c1/m1)*u(2))
    ;x(4); ((k2/m2)*x(1) + (c2/m2)*x(2) + (-k2/m2)*x(3) + (-c2/m2)*x(4))];
end

nlgrey模型函数

% Estimate parameters for 2 DOF Mass Spring damper system
% Ben 
%6-18-19
%6-25-19 updated
%% Requirements
% DOF2damped.m
% variables_2DOF.m
% ode45_2DOF.m
% DOF2greyfxn.m
%% clearing
clear
clc
close all
%% data
load('2DOF_data_file.mat')
x = X;
u = U;

filename = 'DOF2greyfxn'; %calls model file
Order = [2 2 4]; %number of outputs, inputs, states
    %check book to see if 4 inputs are necessary in state eqs

global u0 omega m1 m2 k1 k2 c1 c2

Parameters = {u0,omega,m1,m2,k1,k2,c1,c2};
InitialStates = [0; 0; 0; 0]; % x1 position, x1 velo, x2 pos, x2 velo
Ts = 0; %sample time = continuous time

%% code (parameter estimation)

nlgr = idnlgrey(filename, Order, Parameters, InitialStates, Ts, 'Name','DOF2grey'); %setup nlgrey object 

setpar(nlgr,'Fixed',{true true true true true true false false}); %tells matlab which parameters to estimate (c1, c2)
nlgr = setinit(nlgr, 'Fixed', {true true true true}); % tells matlab to estimate the initial states (all of them)

estdata = iddata(x, u, 0.001); %creates data object based on ode45_2DOF, input (u), output (x), sample time = data points/total time

%opt = nlgreyestOptions('Display', 'on'); %shows estimation progress
nlgr = nlgreyest(estdata, nlgr, 'Display', 'on');%, opt); %estimates unknown parameters in(nlgr) 
    %using estdata as input argument, estdata and nlgr must have same input and output dimensions
%nlgr = pem(estdata, nlgr);

%% visuals and info
present(nlgr)
size(estdata)

compare(estdata,nlgr)

我希望函数返回的参数值与估计的值不同,但是无论它们是否与实验值不同,它们都将返回相同的值。 例如,ODE函数可能使用的'c'值为1,但是如果我估计nlgrey文件中的'c'的值为0.2,则模型将估计值为0.2,而该模型将不适合数据。

0 个答案:

没有答案