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
clear;
clc;
close all;
%clean all
%Init all variable
global u;
t0=0
tf=1000000;
x0=[0 0]';%not sure~
%sliding mode init 
e=0;
e0=0;
e_dot=0;
lunda=20;
s=0; %  s=e_dot+landa*e
end_time =1;
dt=0.001;
loops = ceil(end_time/dt);%ceil=取整數,四捨五入小數點 (採樣頻率=1K HZ)

u=0; %   u=Ueq+Uhit
Ueq=0;
Uhit=0;

x=0;
x0=0;
xd=80 %假設
%xd=sin(1.57*tspan(1));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:loops
   tspan = dt*[(k-1) (k-0.5) k] ;
 %  xd=sin(1.57*tspan(0));
 
e=x0(1)-xd;%sin(1.57*tspan(k)); %此次輸出與期望輸出的誤差
e_dot=e-e0;%這次誤差 和上次誤差比較

s=e_dot+lunda*e; %sliding surface 
Ueq=1.5*e_dot.^2*cos(3.*x0(1))-20.*e_dot
%                     -(3.14)^2/4 sin (3.14/2*t)
Uhit=(0.5*e_dot.^2*abs(cos(3.*x0(1)))+0.1)*sign(s);
u=Ueq+Uhit;
fprintf('sign(s)=%f\n',sign(s));
fprintf('x=%f  xd=%f s=%f uh=%f ueq=%f u=%f\n',x,xd,s,Uhit,Ueq,u);


[t,x]=ode23('ztwoorder',tspan,x0);


x0=x(3, :);
e0=e;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plot(t,x)
title('ode23 result 1')
grid
pause;