微分方程为
dy=zeros(2,1);
dy(1)=y(2);
dy(2)=(4*sin(2*t)-C*y(2)-K*y(1))/M;
其中C,K,M为30×30的矩阵
求解的程序为
tspan=0:0.01:20;
y0=zeros(30,2);
[t,x]=ode45(@odefun_wffc,tspan,y0);
一求解就报错,说无法执行赋值,因为左侧和右侧的元素数目不同。这是为什么?该怎么改?
微分方程即Mx''+Cx'+Kx=4sin2t
追答function dy = odefun_wffc(t,y,M,C,K)
dy(1:30,1) = y(31:60,1);
dy(31:60,1)= y(61:90,1);
dy(61:90,1)= M\(-C*y(31:60,1)-K*y(1:30,1)+4*sin(2*t));
% 上面三行可以简写为
% dy = [y(31:90,1);M\(-C*y(31:60,1)-K*y(1:30,1)+4*sin(2*t))];
% 参考链接
求解程序如下
M = rand(30);
C = rand(30);
K = rand(30);
tspan = 0:0.01:20; % tspan = [0 20]
y0 = zeros(90,1);
[t,y]=ode45(@(t,y)odefun_wffc(t, y, M, C, K),tspan, y0);
plot(t, y(:,1:30))