题目如上所示
代码如下
%% date:2022/06/04
%author:
%version:1.0
%% initialization
% clc;
% clear;
m=4;
k=2500;
c=20;
t=0:0.001:3;
%% Solving equations
Z=dsolve('4*D2y+20*Dy+2500*y=0','y(0)=0.1','Dy(0)=0','t');
DZ=diff(Z);
D2Z=diff(Z,2);
Z_str=string(Z);
Z_str_mat=strrep(Z_str,'*','.*');
Z_str_mat_handle=strcat('@(t)',Z_str_mat);
Z=str2func(Z_str_mat_handle);
DZ_str=string(DZ);
DZ_str_mat=strrep(DZ_str,'*','.*');
DZ_str_mat_handle=strcat('@(t)',DZ_str_mat);
DZ=str2func(DZ_str_mat_handle);
D2Z_str=string(D2Z);
D2Z_str_mat=strrep(D2Z_str,'*','.*');
D2Z_str_mat_handle=strcat('@(t)',D2Z_str_mat);
D2Z=str2func(D2Z_str_mat_handle);
Z_data=Z(t);
DZ_data=DZ(t);
D2Z_data=D2Z(t);
%Z=(eZp(-(5.*t)/2).*cos((15.*11^(1/2).*t)/2))/10 + (11^(1/2).*eZp(-(5.*t)/2).*sin((15.*11^(1/2).*t)/2))/330;
%DZ=-(25.*11^(1/2).*eZp(-(5*t)/2).*sin((15.*11^(1/2).*t)/2))/33;
%D2Z=(125.*11^(1/2).*eZp(-(5*t)/2).*sin((15.*11^(1/2).*t)/2))/66 - (125.*eZp(-(5.*t)/2).*cos((15.*11^(1/2).*t)/2))/2;
%% draw an image
subplot(131);plot(t,Z_data);
title('displacement');
xlabel('time');
ylabel('displacement');
subplot(132);plot(t,DZ_data);
title('speed');
xlabel('time');
ylabel('speed');
subplot(133);plot(t,D2Z_data);
title('acceleration');
xlabel('time');
ylabel('acceleration');
结果为:
代码中主要是句柄函数与字符串之间的转换问题稍微有点小巧方法把*全部换成.*,同时给句柄函数添加@(t)以便识别未知数变量,欢迎大家评论讨论!