% main.m
clc,clear,close all
t0 = 2;t1=200; h=0.02;x0=1;
T = t0:h:t1;X = zeros(length(x0),length(T));X(:,1)=x0;
for jj = 1:length(T) -1
X(:,jj+1) =RK45(T(jj),X(:,jj),@(t,x) f(t,x),h);
end
%%
figure,subplot(211),plot(T(1:100),X(1,1:100)),grid on,title('进入振子前时域')
xq = X(1,1:100);
xh = X(1,101:end);
%%
N = length(xh);
fs = 1/h;
f = (0:N-1)*fs/N;
y = abs(fft(xh));
subplot(212),plot(f,y),grid on,title('进入振子前频域')
%%
fs =1/h;
N=length(xq);
n = 0:N-1;
y= fftshift(abs(fft(xq)));
f = n*fs/N -fs/2;
fs =1/h;
N=length(xh);
n = 0:N-1;
y= fftshift(abs(fft(xh)));
f = n*fs/N -fs/2;
figure,
subplot(211),plot(T(101:end),X(1,101:end)),grid on,title('进入振子后时域')
subplot(212),plot(f,y),grid on
title('进入振子后频域')
不知道对不对,参考下