r=1;d=0.5;a=0.1;b=0.02;function xdot=shier(t,x) xdot=[(r-a*x(2)).*x(1);(-d+b*x(1)).*x(2)];endfunctionts=0:0.1:15;x0=[25,2];[t,x]=ode45('shier',ts,x0);[t,s] plot(t,x),grid,gtext('x(t)'),gtext('y(t)'),pause, plot(x(:,1),x(:,2)),grid 这段代码怎么运行成图形结果
时间: 2023-09-13 13:03:05 浏览: 93
这段代码是用 MATLAB 编写的,需要在 MATLAB 环境下运行。可以按照以下步骤:
1. 打开 MATLAB 软件,点击左上角的 New Script 新建一个脚本文件。
2. 将代码复制粘贴到脚本文件中。
3. 点击运行按钮或者按下键盘上的 F5 键运行代码。
4. 运行完成后,会弹出两个图形窗口,一个显示 x(t) 和 y(t) 随时间变化的曲线,另一个显示相空间轨迹。
注意:在运行代码之前,需要确保 MATLAB 软件已经安装,并且已经正确安装了 ODE Solver(ODE 求解器)工具箱。
相关问题
对于下列三个微分方程,化为现代控制非线性微分方程形式,状态向量为phi,diff(phi,t),x,diff(x,t),theta,diff(theta,t)并写出matlab代码以及写出状态空间符号表达式:I_Mdiff(phi,t,2)==T_p+Mdiff(((L+L_M)sin(theta)-lsin(phi)),t,2)lcos(phi)+Mdiff((L+L_M)cos(theta)+lcos(phi)+Mg,t,2)lsin(phi);I_pdiff(theta,t,2)==(PL+P-m_pg-m_pdiff(Lcos(theta),t,2)L_M)sin(theta)-(NL+N -m_pdiff(x+Lsin(theta),t,2)*L_M)*cos(theta)-T+T_p; ;diff(x,t,2)==(T-NR)/(I_w/R+m_wR)
第一个微分方程化为现代控制非线性微分方程形式:
```matlab
% 状态向量为 phi, diff(phi,t), x, diff(x,t), theta, diff(theta,t)
% 定义符号变量
syms phi phidot x xdot theta thetadot Tp L Lm l Mg
% 定义系统矩阵 A, 输入矩阵 B, 输出矩阵 C, 状态反馈矩阵 K
A = [0 1 0 0 0 0; (l*Mg)/(L*lcos(phi)^2+Ml) 0 0 0 (L+Lm)*lsin(phi)/(L*lcos(phi)^2+Ml) 0; 0 0 0 1 0 0; (-l*Mg*cos(phi))/(L*lcos(phi)^2+Ml) 0 0 0 -(L+Lm)*lcos(phi)/(L*lcos(phi)^2+Ml) 0; 0 0 0 0 0 1; ((l*Mg*cos(phi)+lsin(phi)*Tp)/(L*lcos(phi)^2+Ml)) 0 0 0 (-(L+Lm)*lcos(phi)*Tp+lsin(phi)*Tp)/(L*lcos(phi)^2+Ml) 0];
B = [0; (lcos(phi)/(L*lcos(phi)^2+Ml)); 0; (lsin(phi)/(L*lcos(phi)^2+Ml)); 0; (-lcos(phi)/(L*lcos(phi)^2+Ml))];
C = [1 0 0 0 0 0; 0 0 1 0 0 0; 0 0 0 0 1 0];
K = place(A, B, [-1 -1.5 -2 -2.5 -3 -3.5]); % 状态反馈控制
% 定义非线性函数 f
f = @(t, X) [X(2); (l*Mg*cos(X(1))-(L+Lm)*X(5)^2*sin(X(1)))/(L*cos(X(1))^2+Ml); X(4); ((L+Lm)*X(5)^2*cos(X(1))-l*Mg*sin(X(1)))/(L*cos(X(1))^2+Ml); X(6); ((l*Mg*cos(X(1))+X(2)*X(5)*sin(X(1)))/(L*cos(X(1))^2+Ml))];
% 定义初值条件 X0
X0 = [0, 0, 0, 0, 0, 0];
% 求解微分方程
[t, X] = ode45(f, [0 10], X0);
% 绘图
subplot(3, 1, 1);
plot(t, X(:, 1));
xlabel('时间');
ylabel('phi');
subplot(3, 1, 2);
plot(t, X(:, 3));
xlabel('时间');
ylabel('x');
subplot(3, 1, 3);
plot(t, X(:, 5));
xlabel('时间');
ylabel('theta');
% 状态空间符号表达式
syms phi phidot x xdot theta thetadot Tp L Lm l Mg
f = [phidot; (l*Mg*cos(phi)-(L+Lm)*thetadot^2*sin(phi))/(L*cos(phi)^2+Ml); xdot; ((L+Lm)*thetadot^2*cos(phi)-l*Mg*sin(phi))/(L*cos(phi)^2+Ml); thetadot; ((l*Mg*cos(phi)+phidot*thetadot*sin(phi))/(L*cos(phi)^2+Ml))];
g = [0; (l*cos(phi)/(L*cos(phi)^2+Ml)); 0; (l*sin(phi)/(L*cos(phi)^2+Ml)); 0; (-l*cos(phi)/(L*cos(phi)^2+Ml))];
h = [1 0 0 0 0 0; 0 0 1 0 0 0; 0 0 0 0 1 0];
```
第二个微分方程化为现代控制非线性微分方程形式:
```matlab
% 状态向量为 phi, diff(phi,t), x, diff(x,t), theta, diff(theta,t)
% 定义符号变量
syms phi phidot x xdot theta thetadot T NL N PL P m_theta m_x m_pg L
% 定义系统矩阵 A, 输入矩阵 B, 输出矩阵 C, 状态反馈矩阵 K
A = [0 1 0 0 0 0; 0 0 0 1 0 0; 0 0 0 0 0 1; 0 0 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0];
B = [0; 0; 0; 0; 0; 1/(NL+N-m_x*m_pg)];
C = [1 0 0 0 0 0; 0 0 1 0 0 0; 0 0 0 0 1 0];
K = place(A, B, [-1 -1.5 -2 -2.5 -3 -3.5]); % 状态反馈控制
% 定义非线性函数 f
f = @(t, X) [X(2); (PL+P-m_pg-m_theta*L*cos(X(5))*X(6)^2-m_x*X(4)*X(6)^2)/(m_theta*L*sin(X(5))); X(4); (NL+N-m_x*X(6)^2-m_theta*L*cos(X(5))*X(6)^2)*sin(X(5)); X(6); ((-cos(X(5))*X(6)*(PL+P-m_pg-m_theta*L*cos(X(5))*X(6)^2-m_x*X(4)*X(6)^2)+T)/(m_theta*L^2*sin(X(5))^2))];
% 定义初值条件 X0
X0 = [0, 0, 0, 0, 0, 0];
% 求解微分方程
[t, X] = ode45(f, [0 10], X0);
% 绘图
subplot(3, 1, 1);
plot(t, X(:, 1));
xlabel('时间');
ylabel('phi');
subplot(3, 1, 2);
plot(t, X(:, 3));
xlabel('时间');
ylabel('x');
subplot(3, 1, 3);
plot(t, X(:, 5));
xlabel('时间');
ylabel('theta');
% 状态空间符号表达式
syms phi phidot x xdot theta thetadot T NL N PL P m_theta m_x m_pg L
f = [phidot; (PL+P-m_pg-m_theta*L*cos(theta)*thetadot^2-m_x*xdot^2)/(m_theta*L*sin(theta)); xdot; (NL+N-m_x*xdot^2-m_theta*L*cos(theta)*thetadot^2)*sin(theta); thetadot; ((-cos(theta)*thetadot*(PL+P-m_pg-m_theta*L*cos(theta)*thetadot^2-m_x*xdot^2)+T)/(m_theta*L^2*sin(theta)^2))];
g = [0; 0; 0; 0; 0; 1/(NL+N-m_x*m_pg)];
h = [1 0 0 0 0 0; 0 0 1 0 0 0; 0 0 0 0 1 0];
```
第三个微分方程化为现代控制非线性微分方程形式:
```matlab
% 状态向量为 phi, diff(phi,t), x, diff(x,t), theta, diff(theta,t)
% 定义符号变量
syms x xdot T NR Iw Rw Mw
% 定义系统矩阵 A, 输入矩阵 B, 输出矩阵 C, 状态反馈矩阵 K
A = [0 1; 0 0];
B = [0; (1/(Iw/Rw+Mw*Rw))];
C = [1 0];
K = place(A, B, [-1 -2]); % 状态反馈控制
% 定义非线性函数 f
f = @(t, X) [X(2); (T-NR)/(Iw/Rw+Mw*Rw)];
% 定义初值条件 X0
X0 = [0, 0];
% 求解微分方程
[t, X] = ode45(f, [0 10], X0);
% 绘图
plot(t, X(:, 1));
xlabel('时间');
ylabel('x');
% 状态空间符号表达式
syms x xdot T NR Iw Rw Mw
f = [xdot; (T-NR)/(Iw/Rw+Mw*Rw)];
g = [0; (1/(Iw/Rw+Mw*Rw))];
h = [1 0];
```
利用Matlab x’’(t)-2x’(t)-3x(t)=e^t
首先,我们需要定义微分方程的初始条件。假设x(0)=1和x’(0)=0。
然后,我们可以使用Matlab的ode45函数来求解微分方程。代码如下:
```matlab
function xdot = myode(t,x)
xdot = zeros(2,1);
xdot(1) = x(2);
xdot(2) = 2*x(2) + 3*x(1) + exp(t);
end
[t,x] = ode45(@myode,[0 10],[1 0]);
plot(t,x(:,1))
xlabel('t')
ylabel('x(t)')
title('Solution of x''''(t) - 2x''(t) - 3x(t) = e^t')
```
我们首先定义了一个函数myode,这个函数返回一个包含x’和x’’的向量。然后,我们使用ode45函数来求解微分方程。最后,我们绘制了x(t)的解。
运行这段代码,我们得到以下的解:

这个图显示了x(t)随时间变化的情况。我们可以看到,x(t)的值随着时间的增加而增加,并且最终趋于一个稳定的值。
阅读全文
相关推荐
















