文档介绍:(2)用MATLAB语言编写S函数
(2)用MATLAB语言编写S函数
《飞行器系统仿真与 CA》学****报告
第一部分仿真(40)
题目1:给定导弹相对于目标的运动学方程组为
r(0) = 5km, q(0) = 60deg, ?(0) = 30deg,V = , V , 1Ma = 340m/s, k = 2
建立系统的方框图模型;
用MATLAB语言编写S一函数
(2)用MATLAB语言编写S函数
(2)用MATLAB语言编写S函数
(3)
(4)
用命令行对(1),(2进行仿真,以图形显示结果
(1)
用窗口菜单对(1), (2世行仿真,动态显示结果;
(2)用MATLAB语言编写S函数
(2)用MATLAB语言编写S函数
function [sys,x0,str,ts]=CAD1_sfun(t,x,u,flag) switch flag
case0
[sys,x0,str,ts]=mdlInitializeSizes;
case1
sys = mdlDerivatives(t,x,u);
case3
sys = mdlOutputs(t,x,u);
case{2,4,9}
sys = [];
otherwise
error('unhandled flag=,'num2str(flag))
end
function [sys,x0,str,ts]=mdlInitializeSizes sizes=simsizes;
=3;
=0;
=3;
=0;
=1;
=1;
sys=simsizes(sizes);
str=[];
x0=[5000,pi/3,pi/6];
ts=[0 0];
functionsys=mdlDerivatives(t,x,u)
vm=*340;
v=*340;
k=2;
dx(1)=vm*cos(x(2))-v*cos(x(2)-x(3));
dx(2)=(v*sin(x(2)-x(3))-vm*sin(x(2)))/x(1);
dx(3)=k*dx(2);
sys=dx;
function sys=mdlOutputs(t,x,u)
sys=x;
调用 S 函数的模型框图
(3)框图仿真结果:
S 函数仿真结果:
( 4)命令输入
clear;clc
[t x ] = sim('CAD1');
hSimulink = figure();
subplot(3, 1, 1);
plot(t,x(:,1)); grid; ylabe'lr(');
subplot(3, 1, 2);
plot(t,x(:,2)); grid; ylabe'lq(');
subplot(3, 1, 3);
plot(t,x(:,3)); grid; ylabe'ls(igma)';
[t x ] = sim('CAD1_S');
hSFun = figure();
subplot(3, 1, 1);
plot(t,x(:,1)); grid; ylabe'lr(');
subplot(3, 1, 2);
plot(t,x(:,2)); grid; ylabe'lq(');
subplot(3, 1, 3);
plot(t,x(:,3)); grid; ylabe'ls(igma)';
模型仿真结果:
S 函数仿真结果:
题目 2:给出动态方程 x ( 1 x 2 ) x tx 1 ; x ( 0 ) 1 , x ( 0 ) 0
(1)用MATLAB语言编写S-函数;
(2)用命令行gear/adam汕对(1世行仿真,显示曲线 x(t=0: 100);
(3) 建立方框图 ,用 RK45 仿真 50秒,显示曲线
答:
(1)用MATLAB语言编写S一函数
function[sys,x0,str,ts]=CAD2_sfun(t,x,u,fl otherwise
ag)
error('unhandled flag=,'num2str(flag))
switch flag
end
case0
function [sys,x0,str,ts]=mdlInitializeSizes
sizes=simsizes;
[sys,x0,str,ts]=mdlInitializeSizes;
=2;
case1
=0;
sys=mdlDerivatives(t,x,u);
=2;
case3
=0;
sys=mdlOutputs(t,x,u);
=