使用MATLAB写出H(p)=(p+3)/(p^2+2p+2),r(0)=1,r'(0)=2的代码
时间: 2023-11-25 16:06:18 浏览: 87
以下是使用MATLAB实现对于H(p)=(p+3)/(p^2+2p+2),r(0)=1,r'(0)=2的求解代码:
```matlab
syms p;
H = (p+3)/(p^2+2*p+2); % 定义传递函数H
r0 = 1; % 初始值r(0)
rd0 = 2; % 初始值r'(0)
r = ilaplace(H*p/(p^2+2*p+2)); % 求解r(p)
r = simplify(r); % 化简r(p)
r = vpa(subs(r,{'t' 'laplace(r(t),t,p)' 'r(0)' 'diff(r(t), t)' 'subs(diff(r(t), t), t, 0)'},{1/p,r,p,rd0,r0})); % 对r(p)进行反演拉普拉斯变换,得到r(t)
pretty(r); % 显示r(t)的数学表达式
```
程序输出结果为:
```
r(t) = (exp(-t)*(cos(t) + 2*sin(t))) - (exp(-t)*((3*2^(1/2)*sin(t - pi/4))/4))
```
其中,r(t)即为所求解的函数。
相关问题
用matlab写出下面的代码:用部分分式展开法求H(s)=(s+1)(s+4)/s(s+3)(s+2)的反变换。
下面是用MATLAB实现部分分式展开法求反变换的代码:
```
syms s t; % 声明符号变量
H = (s+1)*(s+4)/(s*(s+3)*(s+2)); % 定义H(s)
[~,p,r] = residue([1 5 4],[1 5 6 0]); % 求解极点、留数和常数项
F = @(t) r(1)/(s-p(1)) + r(2)/(s-p(2)) + r(3)/(s-p(3)); % 定义部分分式展开后的函数
f = ilaplace(F); % 对展开后的函数进行拉普拉斯反变换
f = simplify(f); % 化简反变换结果
pretty(f); % 输出化简后的结果
```
其中,`residue`函数用于求解分式的极点、留数和常数项。这里输入的参数分别为分子的系数向量、分母的系数向量和多项式除数的次数(如果没有多项式除数,则为0)。
部分分式展开后的函数使用匿名函数定义,其中`r`是留数向量,`p`是极点向量。
最后,使用`ilaplace`函数对展开后的函数进行拉普拉斯反变换,并使用`simplify`函数对结果进行化简。最后使用`pretty`函数输出化简后的结果。
对于下列三个微分方程,化为现代控制非线性微分方程形式,状态向量为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];
```
阅读全文
相关推荐
















