⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zuixiaoerchengfa.m

📁 这个程序主要是一个递推的最小二乘发,根据diphon方程来辨识一类系统的参数
💻 M
字号:
function varargout = lsmgui(varargin)
% LSMGUI M-file for lsmgui.fig
%      LSMGUI, by itself, creates a new LSMGUI or raises the existing
%      singleton*.
%
%      H = LSMGUI returns the handle to a new LSMGUI or the handle to
%      the existing singleton*.
%
%      LSMGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in LSMGUI.M with the given input arguments.
%
%      LSMGUI('Property','Value',...) creates a new LSMGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before lsmgui_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to lsmgui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help lsmgui

% Last Modified by GUIDE v2.5 30-Nov-2006 23:59:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @lsmgui_OpeningFcn, ...
                   'gui_OutputFcn',  @lsmgui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before lsmgui is made visible.
function lsmgui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to lsmgui (see VARARGIN)

% Choose default command line output for lsmgui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
initialize_gui(hObject, handles);
% UIWAIT makes lsmgui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout =lsmgui_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

 

function edit1_Callback(hObject, eventdata, handles)
n= str2double(get(hObject, 'String'));
if isnan(n)             %输入迭代次数
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
data = getappdata(gcbf, 'metricdata');
data.n = n;
setappdata(gcbf, 'metricdata', data);
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

 

function edit2_Callback(hObject, eventdata, handles)
a1 = str2double(get(hObject, 'String'));%输入参数
data = getappdata(gcbf, 'metricdata');
data.a1 = a1;
setappdata(gcbf, 'metricdata', data);
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

 

function edit3_Callback(hObject, eventdata, handles)
a2 = str2double(get(hObject, 'String'));%输入参数
data = getappdata(gcbf, 'metricdata');
data.a2 = a2;
setappdata(gcbf, 'metricdata', data);
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

 

function edit4_Callback(hObject, eventdata, handles)
b1= str2double(get(hObject,'String'));%输入参数
data = getappdata(gcbf, 'metricdata');
data.b1 = b1;
setappdata(gcbf, 'metricdata', data);
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

 

function edit5_Callback(hObject, eventdata, handles)
b2 = str2double(get(hObject, 'String'));%输入参数
data = getappdata(gcbf, 'metricdata');
data.b2 = b2;
setappdata(gcbf, 'metricdata', data);
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

 

function edit6_Callback(hObject, eventdata, handles)
i = str2double(get(hObject, 'String'));
if (i==0)
    set(hObject, 'String', 0);
    errordlg('Input must not be   0','Error');
    
else 
    data = getappdata(gcbf, 'metricdata');
    data.i = i;
    setappdata(gcbf, 'metricdata', data);
end

% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

 

function edit7_Callback(hObject, eventdata, handles)
f = str2double(get(hObject, 'String'));
if ((f==1)|(f==2)|(f==3))
   data = getappdata(gcbf, 'metricdata');
   data.f = f;
   setappdata(gcbf, 'metricdata', data);
else
   set(hObject, 'String', 0);
  errordlg('Input must be 1 or 2 or 3','Error');
 end

% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)    
data = getappdata(gcbf, 'metricdata');
R=[data.b1;data.b2 ;-1*data.a1 ;-1*data.a2;];%已知参数
k=0;%迭代次数
pi=3.142;
W=[0 0 0 0]';%计算期望输出用[u1 u2 y3 y4]
P=[1000 0 0 0;0 1000 0 0;0 0 1000 0;0 0 0 1000];%4*4的迭代矩阵初值取10^3~10^5
T=[0 0 0 0]';%要辨识的参数
Z=[0 0 0 0]';%观测的输入输出
for k=1:data.n;%迭代次数
Z=W;    
Y(k)=R'*W;%K+1时刻的观测值,系统输出
Q(k)=T'*Z;%估计的输出
E(k)=Y(k)-Q(k);%辨识误差
L=P*Z/(data.i+Z'*P*Z);%K(K+1)
T=T+L*E(k);%一次参数估计
if data.i==0
    errordlg('Input must be 1 or 2 or 3 ,default 1','Error');
    data = getappdata(gcbf, 'metricdata');
    data.i = 0.9;
    setappdata(gcbf, 'metricdata', data);
end
P=1/data.i*(P-L*Z'*P);%P矩阵迭代一次
a(k)=T(1);
b(k)=T(2);
c(k)=T(3);
d(k)=T(4);%a ,b,c,d记录估计参数的变化,绘图用
if data.f == 2            %选择输入函数
   u=5*sin(0.01*pi*k);%输入函数,进行辨识用   
elseif data.f == 1
    u=3*sin(0.01*pi*k)*3*cos(0.5*pi*k);
elseif data.f==3
   u=5*sin(0.01*pi*k)*0.2*5*sin(0.1*pi*k) ;
else 
    errordlg('Input must be 1 or 2 or 3 ,default 1','Error');
    data = getappdata(gcbf, 'metricdata');
    data.f = 1;
    setappdata(gcbf, 'metricdata', data);
    u=3*sin(0.01*pi*k)*3*cos(0.5*pi*k); 
end    
W(2)=W(1);
W(1)=u;
W(4)=W(3);
W(3)=Y(k);%W=[u W(1) y W(3)] Z(K+1)
end
k=1:data.n;%可以用axis([1,100,-1.2,1])来设定坐标范围
figure;
subplot(1,2,1),plot(k,a(k),'r'),xlabel('迭代次数 n'),ylabel('估计参数'),title('参数的辨识'),hold on;
plot(k,b(k),'y'),plot(k,-c(k),'g'),plot(k,-d(k)),text(1,-0.3,'绿色表示a1的估计值'),
text(1,0,'蓝色辨识a2的估计值'),text(1,0.3,'红色表示b1的估计值'),text(1,0.6,'黄色表示b2的估计值'),hold off
subplot(1,2,2),plot(k,Y(k)),xlabel('迭代次数 n'),ylabel('系统输出,输出估计及误差'),title('输出信息'),hold on;
plot(k,Q(k),'y'),plot(k,E(k),'r'),,text(1,0.3,'蓝色表示系统输出'),text(1,0,'黄色表示估计的输出'),text(1,-0.3,'红色表示误差'),hold off
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
initialize_gui(gcbf, handles);
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

function initialize_gui(fig_handle, handles)
data.n=10; 
data.a1=-1;
data.a2=0.632;
data.b1=0.368;
data.b2=0.264;
data.i=0.9;
 data.f = 1;
 setappdata(fig_handle, 'metricdata', data);
set(handles.edit1, 'String',data.n);
set(handles.edit2, 'String', data.a1);
set(handles.edit3, 'String', data.a2);

set(handles.edit4, 'string', data.b1);
set(handles.edit5, 'string', data.b2);

set(handles.edit6, 'String',data.i);
set(handles.edit7, 'String', data.f);



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -