matlab 中lsim的用法,lsim的用法

lsim:针对线性是不变模型,给定任意输入,得到输出响应。

系统模型为状态方程时,同时还可以得到状态轨迹。

[y,t,x]=lsim[sys,u,t,x0]

输出y的行数与u的长度相同,列数与输出个数相同;

状态x的行数与u的长度相同,列数与状态的个数相同。

程序如下:

num=[1 1];

den=[1 3 6];

%h=tf(num,den);%获得传递函数

[A,B,C,D]=tf2ss(num,den);%将传递函数转化为状态方程

[u,t]=gensig('pulse',2,10,0.1);%采样间隔为0.1,时间长度为10,在2的倍数处信号幅度为1,其余时间为0.

x=u;

sys=ss(A,B,C,D);

x0=[0 0];

t=0:0.1:10;%此处时间为上面采样时间相同

[y,t,x]=lsim(sys,u,t,x0)%模型的输出响应

plot(t,y,'b',t,u,'g');

结果:

a4c26d1e5885305701be709a3d33442f.png

设某系统

dX(t)=Ax(t)+BU(t)

y(t)=Cx(t)+DU(t)

x(0)=x0 t∈[0,tf]

MATLAB函数

SYS=ss(A,B,C,D)(ss:state space model)

用来求出lsim函数所需的系统参数“SYS”.

lsim(SYS,U,T)

此函数画出LTl系统SYS对由U和T描述的输人信号的时间响应(timerespouse)。时间向量T由等距的时间采样点组成,U是一个矩阵,它的列数为输入的数目,它的第i行是输人在T(i)时刻的输人值.例如,

t=0:0.01:5;u=sin(t);lsim(sys,u,t)

模拟了系统SYS对输入u(t)=sin(t)持续5秒时间的响应.

对于离散时间系统,输入U的采样率应与系统本身的的采样率相同(因此参数T就是冗余的,可以略去或设为即empty矩阵)

lsim(SYS,U,T,X0)

指定了初始状态X0.其他同上一个函数.

[Y,T]=lsim(SYS,U,……)

返回输出矩阵Y和所用的时间向量T.此函数不画响应曲线.矩阵Y的行数为LENGTH(T),列数为SYS的输出数目

[Y,T,X]=lsim(SYS,U,…)

还返回了状态轨迹(state trajectory)X,它是一个矩阵,行数为LENGTH(T),列数为状态的数目.其他同上一个函数.

n=[1 1];

d=[1 1 10];

[A,B,C,D]=tf2ss(n,d);

sys=ss(tf(n,d));

A

A =

-1 -10

1 0

sys.a

ans =

-1 -5

2 0

Compute the transfer function of the state-space model with the

following data.

A=[-2 -1;1 -2], B=[1 1;2 -1], C=[1 0], D=[0 1]

To do this, type

sys = ss([-2 -1;1 -2],[1 1;2 -1],[1 0],[0 1])

tf(sys)

Transfer function from input 1 to output:

s

-------------

s^2 + 4 s + 5

Transfer function from input 2 to output:

s^2 + 5 s + 8

-------------

s^2 + 4 s + 5

a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

lsim

Simulate LTI model responses to arbitrary inputs

Syntax

lsim

lsim(sys,u,t)

lsim(sys,u,t,x0)

lsim(sys,u,t,x0,'zoh')

lsim(sys,u,t,x0,'foh')

lsim(sys)

Description

lsim simulates the

(time) response of continuous or discrete linear systems to

arbitrary inputs. When invoked without left-hand arguments,

lsim plots the response on the

screen.

lsim(sys,u,t) produces a plot of the time response

of the LTI model sys to the input time history

t,u. The vector t specifies the time

samples for the simulation and consists of regularly spaced time

samples.

t = 0:dt:Tfinal

The matrix u must have as many rows as time samples

(length(t)) and as many columns as system inputs. Each row

u(i,:) specifies the input value(s) at the time sample

t(i).

The LTI model sys can be continuous or discrete, SISO

or MIMO. In discrete time, u must be sampled at the same

rate as the system (t is then redundant and can be omitted

or set to the empty matrix). In continuous time, the time sampling

dt=t(2)-t(1) is used to discretize the continuous model.

If dt is too large (undersampling), lsim issues a warning suggesting that you use a

more appropriate sample time, but will use the specified sample

time. See Algorithm for a discussion of sample times.

lsim(sys,u,t,x0) further specifies an initial

condition x0 for the system states. This syntax applies

only to state-space models.

lsim(sys,u,t,x0,'zoh') or

lsim(sys,u,t,x0,'foh') explicitly

specifies how the input values should be interpolated between

samples (zero-order hold or linear interpolation). By default,

lsim selects the interpolation

method automatically based on the smoothness of the signal U.

Finally,

lsim(sys1,sys2,...,sysN,u,t)

simulates the responses of several LTI models to the same input

history t,u and plots these responses on a single

figure. As with bode or plot, you can specify a

particular color, linestyle, and/or marker for each system, for

example,

lsim(sys1,'y:',sys2,'g--',u,t,x0)

The multisystem behavior is similar to that of bode or

step.

When invoked with left-hand arguments,

[y,t] = lsim(sys,u,t)

[y,t,x] = lsim(sys,u,t) % for state-space models only

[y,t,x] = lsim(sys,u,t,x0) % with initial state

return the output response y, the time vector

t used for simulation, and the state trajectories

x (for state-space models only). No plot is drawn on the

screen. The matrix y has as many rows as time samples

(length(t)) and as many columns as system outputs. The

same holds for x with "outputs" replaced by states.

lsim(sys) opens the Linear Simulation Tool GUI.

For more information about working with this GUI, see Working with the Linear Simulation Tool in the

Example

Simulate and plot the response of the system

to a square wave with period of four seconds. First generate the

square wave with gensig. Sample every 0.1 second during 10

seconds:

[u,t] = gensig('square',4,10,0.1);

Then simulate with lsim.

H = [tf([2 5 1],[1 2 3]) ; tf([1 -1],[1 1 5])]

lsim(H,u,t)

Algorithm

Discrete-time systems are simulated with ltitr (state

space) or filter (transfer function and

zero-pole-gain).

Continuous-time systems are discretized with c2d using

either the 'zoh' or 'foh' method ('foh'

is used for smooth input signals and 'zoh' for

discontinuous signals such as pulses or square waves). The sampling

period is set to the spacing dt between the user-supplied

time samples t.

The choice of sampling period can drastically affect simulation

results. To illustrate why, consider the second-order model

To simulate its response to a square wave with period 1 second,

you can proceed as follows:

w2 = 62.83^2

h = tf(w2,[1 2 w2])

t = 0:0.1:5; % vector of time samples

u = (rem(t,1)>=0.5); % square wave values

lsim(h,u,t)

lsim evaluates the specified

sample time, gives this warning

Warning: Input signal is undersampled. Sample every 0.016 sec or

faster.

and produces this plot.

To improve on this response, discretize H(s) using

the recommended sampling period:

dt=0.016;

ts=0:dt:5;

us = (rem(ts,1)>=0.5)

hd = c2d(h,dt)

lsim(hd,us,ts)

This response exhibits strong oscillatory behavior hidden from

the undersampled version.

### MATLAB `lsim` 函数详解 #### 功能描述 `lsim` 是用于模拟线性系统的时域响应的函数。该函数可以处理连续时间或离散时间系统,并允许指定任意输入信号来观察系统的动态行为[^1]。 #### 基本语法 最简单的调用方式如下所示: ```matlab lsim(sys, u, t) ``` 其中: - `sys`: 表示要仿真的线性模型,通常由传递函数、状态空间表示或其他形式定义; - `u`: 输入向量或矩阵,每一列表示对应时刻下的输入值; - `t`: 时间向量,指定了仿真过程中各个采样点的时间戳; 对于多输入多输出 (MIMO) 系统,则需要提供相应的维度匹配的输入数据[^2]。 #### 返回值说明 如果希望获取具体的输出轨迹而不只是绘图展示,可以通过以下方式捕获返回的结果: ```matlab [y, t_out, x] = lsim(sys, u, t); ``` 这里额外获得了三个变量: - `y`: 输出响应的数据序列; - `t_out`: 实际使用的计算时间节点; - `x`: 对于状态空间模型而言,这是内部状态的变化过程记录; #### 示例代码 下面给出一个具体例子来演示如何使用此功能: ```matlab % 定义二阶低通滤波器作为被控对象 num = [0.5]; den = [1 1 0.5]; sys = tf(num, den); % 设置仿真参数 Tfinal = 10; % 总持续时间为10秒 Tsampling = 0.1; % 采样间隔为0.1秒 timeVec = 0:Tsampling:Tfinal; inputSignal = sin(timeVec); % 正弦波激励源 % 执行仿真并绘制结果 figure(); subplot(2, 1, 1); plot(timeVec, inputSignal); title('Input Signal'); xlabel('Time(s)'); ylabel('Amplitude'); [yOut, ~, stateTraj] = lsim(sys, inputSignal', timeVec'); subplot(2, 1, 2); plot(timeVec, yOut); title('System Response to Sinusoidal Input'); xlabel('Time(s)'); ylabel('Output Amplitude'); grid on; % 显示部分中间状态变化情况 disp('Initial states:'); disp(stateTraj(:, 1)); disp('Final states:'); disp(stateTraj(:, end)); ``` 上述脚本创建了一个简单的一阶惯性环节,并对其施加正弦波形测试信号,最后通过图形化界面展示了整个仿真的全过程以及起始和终止状态下的一些特征信息[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值