最小二乘法模型辨识算法

  • P4_427185
    了解作者
  • 23.6KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-06 02:13
    上传日期
自己写的最小二乘算法,包含了整批算法、递推算法和广义最小二乘算法,并包含对它们的误差分析比较,学习算法建模时具有一定参考价值。
LeastSquareMethod.zip
  • module_bianshi.m
    33.3KB
  • panduanjieci.m
    825B
  • e_build.m
    616B
  • mxulie.m
    344B
  • zhengpisuanfa.m
    758B
  • guangyierchengfa.m
    1.2KB
  • dituisuanfa.m
    1.2KB
  • module_bianshi.fig
    14.6KB
内容介绍
function varargout = module_bianshi(varargin) % MODULE_BIANSHI MATLAB code for module_bianshi.fig % MODULE_BIANSHI, by itself, creates a new MODULE_BIANSHI or raises the existing % singleton*. % % H = MODULE_BIANSHI returns the handle to a new MODULE_BIANSHI or the handle to % the existing singleton*. % % MODULE_BIANSHI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in MODULE_BIANSHI.M with the given input arguments. % % MODULE_BIANSHI('Property','Value',...) creates a new MODULE_BIANSHI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before module_bianshi_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to module_bianshi_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 module_bianshi % Last Modified by GUIDE v2.5 04-Jan-2016 19:28:25 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @module_bianshi_OpeningFcn, ... 'gui_OutputFcn', @module_bianshi_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(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 end % --- Executes just before module_bianshi is made visible. function module_bianshi_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 module_bianshi (see VARARGIN) % Choose default command line output for module_bianshi handles.output = hObject; % Update handles structure guidata(hObject, handles); global sys input_u output_y sys_zhengpi sys_ditui sys_guangyi flag; input_u=0; output_y=0; sys_zhengpi=0; sys_ditui=0; sys_guangyi=0; flag= false(3,1); num=[0 1 -0.5]; den=[5 -2.5 3.5]; Ts=0.1; sys=filt(num,den,Ts); set_text(num,den,handles.module); % UIWAIT makes module_bianshi wait for user response (see UIRESUME) % uiwait(handles.figure1); end % --- Outputs from this function are returned to the command line. function varargout = module_bianshi_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; end % --- Executes on button press in input_module. function input_module_Callback(hObject, eventdata, handles) % hObject handle to input_module (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global sys; options.Resize='on'; options.WindowStyle='normal'; options.Interpreter='none'; prompt={'分子多项式矩阵:','分母多项式矩阵:','采样周期:'};%prompt{3}=prompt{2}= title='离散传递函数输入'; defalt_ans={'[0 1 -0.5]','[5 -2.5 3.5]','0.1'}; answer=inputdlg(prompt,title,1,defalt_ans,options); if isempty(answer) return; end Ts=str2num(answer{3}); num=str2num(answer{1}); den=str2num(answer{2}); for x=1:length(den) if den(1)~=0 l=length(den); break; else den(1)=[]; l=0; end end if (l==0)||(length(num)~=length(den)) errordlg('输入错误,请重新输入!'); return; end set_text(num,den,handles.module); sys=filt(num,den,Ts); end % --- Executes on button press in analys_module. function analys_module_Callback(hObject, eventdata, handles) % hObject handle to analys_module (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global input_u output_y sys sys_zhengpi sys_ditui sys_guangyi flag; T=sys.Ts; s0=['原 始 系 统:',set_answer(sys)]; x=get(handles.jieshu_if,'value'); if numel(input_u)<2 errordlg('算不了!请先选择输入类型,求出系统响应。'); return; end if x==1 n=fix(str2double(get(handles.jieshu_n,'string'))); if n<1||n>10 errordlg('系统阶次不好算,换个简单的吧!'); return; end else n=panduanjieci(input_u,output_y); if n==-1 errordlg('对不起,阶次没判断出来!'); return; end set(handles.jieshu_n,'string',num2str(n)); end mthod1=get(handles.method1,'value'); mthod2=get(handles.method2,'value'); mthod3=get(handles.method3,'value'); if mthod1==1 tic;%开始计时 den_num=zhengpisuanfa(input_u,output_y,n); toc;%计时结束 den=[1,den_num(1:n)']; num=den_num(n+1:end)'; sys_zhengpi=filt(num,den,T); flag(1)=true; elseif mthod2==1 tic;%开始计时 den_num=dituisuanfa(input_u,output_y,n); toc;%计时结束 den=[1,den_num(1:n)']; num=den_num(n+1:end)'; sys_ditui=filt(num,den,T); flag(2)=true; elseif mthod3==1 tic;%开始计时 den_num=guangyierchengfa(input_u,output_y,n); toc;%计时结束 den=[1,den_num(1:n)']; num=den_num(n+1:end)'; sys_guangyi=filt(num,den,T); flag(3)=true; end s=s0; if flag(1) s1=['整 批 算 法:',set_answer(sys_zhengpi)]; s=char(s,s1); end if flag(2) s2=['递 推 算 法:',set_answer(sys_ditui)]; s=char(s,s2); end if flag(3) s3=['广义最小二乘法:',set_answer(sys_guangyi)]; s=char(s,s3); end set(handles.module_out,'string',s); end % --- Executes on button press in respond_module. function respond_module_Callback(hObject, eventdata, handles) % hObject handle to respond_module (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global sys input_u output_y flag; flag= false(3,1); set(handles.zhengpi_mean,'string',''); set(handles.zhengpi_var,'string',''); set(handles.ditui_mean,'string',''); set(handles.ditui_var,'string',''); set(handles.guangyi_mean,'string',''); set(handles.guangyi_var,'string',''); T=sys.Ts; p=str2num(get(handles.input_p,'string')); zhouqi=p+1; s_type=get(handles.inputsign_type,'Value'); n=nextpow2(str2num(get(handles.inputsign_long,'string'))); val=str2num(get(handles.arange,'string')); switch s_type case 1 % t=(0:2^n-2)'; % w=2*pi/(t(end)+1); % u1=sin(w*t); % u=[]; % for x=1:zhouqi % u=[u;u1]; % end % t=[0:length(u)-1]*T; % N=2^n-1; [u,t]=mxulie(n,T,zhouqi,val); N=2^n-1; case 2 u1=val*wgn((2^n-1),1,1); u=[]; for x=1:zhouqi u=[u;u1]; end t=[0:length(u)-1]*T; N=2^n-1; case 3 u1=val*randn((2^n-1),1); u=[]; for x=1:zhouqi u=[u;u1]; end t=[0:length(u)-1]*T; N=2^n-1; otherwise errordlg('程序运行中出现错误,请与作者取得联系!'); close; end [y,t] = lsim(sys,u,t); y(1:N)=[]; n_type=get(handles.noise_type,'Value'); switch n_type case 1 noise=zeros(N*p,1); case 2 noise0=randn(N*p,1); noise_sys=filt([1 0 0],[5 -2.5 3.5],T); t=[0:length(noise0)-1]*T; noise=lsim(noise_sys,noise0,t); case 3 noise=randn(N*p,1); otherwise errordlg('程序运行中出现错误,请与作者取得联系!'); close; end % figure; % plot(u);
评论
    相关推荐