function varargout = GPC(varargin)
% GPC M-file for GPC.fig
% GPC, by itself, creates a new GPC or raises the existing
% singleton*.
%
% H = GPC returns the handle to a new GPC or the handle to
% the existing singleton*.
%
% GPC('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GPC.M with the given input arguments.
%
% GPC('Property','Value',...) creates a new GPC or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GPC_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GPC_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 GPC
% Last Modified by GUIDE v2.5 15-Dec-2005 15:29:52
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GPC_OpeningFcn, ...
'gui_OutputFcn', @GPC_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 GPC is made visible.
function GPC_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 GPC (see VARARGIN)
% Choose default command line output for GPC
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GPC wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GPC_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;
% --------------------------------------------------------------------
function project_Callback(hObject, eventdata, handles)
% hObject handle to project (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function begin_Callback(hObject, eventdata, handles)
% hObject handle to begin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global yr;
global n;
N=str2num(get(handles.N,'String'));
Nu=str2num(get(handles.Nu,'String'));
r=str2num(get(handles.r,'String'));
a=str2num(get(handles.a,'String'));
y0=str2num(get(handles.y0,'String'));
u0=str2num(get(handles.u0,'String'));
A=str2num(get(handles.modelA,'String'));
B=str2num(get(handles.modelB,'String'));
O=[1,-1];
En=zeros(1,N);
F=zeros(N,length(A));
Aa=conv(A,O);
En(1)=1;
k=zeros(size(Aa));
k(1)=1;
L=k-Aa;
F(1,:)=L(2:end);
for j=1:N-1
En(j+1)=F(j,1);
L=[F(j,:),0]-En(j+1).*Aa;
F(j+1,:)=L(2:end);
end
E=cell(1,N);
for i=1:1:N
E{i}=En(1:i);
end
H=cell(1,N);
G=cell(1,N);
for j=1:N
Gl=conv(E{j},B);
G{j}=Gl(1:j);
H{j}=Gl(j+1:end);
end
G1=zeros(N,Nu);
for i=1:N
if i<Nu
G1(i,i:-1:1)=G{N}(1:i);
end
if i>Nu
G1(i,1:Nu)=G{N}(i:-1:i-Nu+1);
end
end
Al=(-1).*A(2:end);
y=zeros(1,n);
u=zeros(1,n);
yd=zeros(N,1);
yo=zeros(N,1);
Y=zeros(length(A)-1,1);
U=zeros(length(B),1);
for i=1:(length(A)-1)
Y(i,1)=y0(i);
end
for i=1:length(B)
U(i,1)=u0(i);
end
for k=1:n
y(k)=Al*Y+B*U;
yd(1)=a*y(k)+(1-a)*yr(k);
for j=2:N
yd(j)=a*yd(j-1)+(1-a)*yr(k);
end
Yt=[y(k);Y];
for j=1:N
yo(j)=F(j,:)*Yt+conv(O,H{j})*U;
end
I=eye(Nu);
tmp=inv(G1'*G1+r.*I)*G1'*(yd-yo);
u(k)=U(1)+tmp(1);
for j=length(A)-1:-1:2
Y(j,1)=Y(j-1);
end
Y(1,1)=y(k);
for j=length(B):-1:2
U(j,1)=U(j-1);
end
U(1,1)=u(k);
end
k=1:n;
axes(handles.axesy);
plot(k,yr(k),k,y(k));
axes(handles.axesu);
plot(k,u(k));
% --------------------------------------------------------------------
function exit_Callback(hObject, eventdata, handles)
% hObject handle to exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close;
% --------------------------------------------------------------------
function help_Callback(hObject, eventdata, handles)
% hObject handle to help (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function yrmenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to yrmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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
% --- Executes on selection change in yrmenu.
function yrmenu_Callback(hObject, eventdata, handles)
% hObject handle to yrmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns yrmenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from yrmenu
global yr;
global n;
val = get(hObject,'Value');
switch val
case 2
prompt={'请输入阶跃信号设定值:','请输入运行步数:'};
title='设定曲线';
def={'10','200'};
answer=inputdlg(prompt,title,1,def);
stan=str2num(answer{1});
n=str2num(answer{2});
yr=zeros(1,n);
for i=1:n
yr(i)=stan;
end
case 3
prompt={'请输入正弦函数的幅值:','请输入正弦函数的周期:','请输入运行步数:'};
title='正弦函数输入框';
def={'10','40','200'};
answer=inputdlg(prompt,title,1,def);
a=str2num(answer{1});
b=str2num(answer{2});
n=str2num(answer{3});
for t=1:n
yr(t)=a*sin(2*3.14*t/b);
end
case 4
prompt={'请输入方波函数的最大幅值:','请输入方波函数的最小幅值:','请输入方波函数的周期:','请输入运行步数:'};
title='方波函数输入框';
def={'5','5','40','200'};
answer=inputdlg(prompt,title,1,def);
amax=str2num(answer{1});
amin=str2num(answer{2});
b=str2num(answer{3});
n=str2num(answer{4});
for m=0:n/b
for k=1:b/2
yr(m*b+k)=amax;
end
for k=b/2:b
yr(m*b+k)=-1*amin;
end
end
end;
% The user selected the second item
% etc.
% k=1:100;
% axes(handles.axesy);
% plot(k,yr(k));
% --- Executes during object creation, after setting all properties.
function modelA_CreateFcn(hObject, eventdata, handles)
% hObject handle to modelA (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - hand