代码如下(matlab编程):
被控对象:
function x = mass(x,u,f,t)
x1 = x(1);
x2 = x(2);
dx1 = x2;
dx2 = u+f;
x1 = x1+dx1*t;
x2 = x2+dx2*t;
x = [x1,x2];
end
滑模控制器:
function [u,inf_out] = SMC(x,inf_in,parameter)
x1 = x(1);
x2 = x(2);
c = inf_in(1);
rho = parameter;
sigma = x2+c*x1;
u = -c*x2-rho*sign(sigma);
inf_out = sigma;
end
仿真程序:
clc;clear;
close
%% 变量定义
length = 10000; % 迭代次数
dt = 0.001; % 步长
t = zeros(length,1);
x = zeros(2,1);
x1 = zeros(length,1); % 状态变量
x2 = zeros(length,1); % 状态变量
f = zeros(length,1); % 干扰
u = zeros(length,1); % 控制量
c = 1.5;
rho = 2;
inf_in = c;
parameter = rho;
sigma = zeros(length,1);
%% 初始化
i = 1;
t(i) = (i-1)*dt;
x1(i) = 1;
x2(i) = -2;
x = [x1(i),x2(i)];
f(i) = sin(2*t(i));
[u(i),sigma(i)] = SMC(x,inf_in,parameter);
%% 迭代循环
for i = 2:1:length
x = mass(x,u(i-1),f(i-1),dt);
x1(i) = x(1);
x2(i) = x(2);
[u(i),sigma(i)] = SMC(x,inf_in,parameter);
t(i) = (i-1)*dt;
f(i) = sin(2*t(i));
end
%% 绘图
figure(1);
plot(t,sigma,'linewidth',2);
hold on;
grid on;
xlabel('time(s)');
ylabel('Sliding variable');
figure(2);
plot(t,x1,'-.','linewidth',2);
hold on;
grid on;
plot(t,x2,'linewidth',2);
xlabel('time(s)');
ylabel('x(m)');
legend('x1','x2');
figure(3);
plot(x1,x2,'linewidth',2);
hold on;
grid on;
xlabel('x1');
ylabel('x2');
figure(4);
plot(t,u,'linewidth',2);
hold on;
grid on;
xlabel('time(s)');
ylabel('u');