Understanding continuous integrator blocks in Simulink
조회 수: 14 (최근 30일)
이전 댓글 표시
I'm struggling to understand how the continuous integrator block works. Let's say we are setting up a fixed step continuous case and we select a solver such as ode3. Ode3 uses the Bogacki-Shampine method. This method requires evaluating the deriviative y'=f(t,y) at specified values of t and y. And it seems Simulink evaluates at minor timesteps for those intermediate values of t.
This is quite straightforward if you have an actual function f(t,y). However, I'm don't understand how this works when the input isn't a function you can just evaluate at any point you wish (eg: an arbitrary input). And even if the block input were a function Simulink is integrating it numerically, not analytically, so you still have the same issue.
I tried building a simulink model of just an integrator block into C to see how it works, but i'm not great at C so I didn't really understand it. Can anyone help, or point me to resources you think might be useful?
댓글 수: 0
답변 (1개)
Sam Chak
2025년 5월 13일
Hi @C
I don't what is the mathematical 'C' function in your differential equation. If the derivative function is unavailable, but you have the rate-of-change time series data, you will need to interpolate the data using the interp1() function. While I am unsure how this works in Simulink, the general idea is outlined below. This is my first attempt at implementing the Bogacki–Shampine method, so it may contain errors. Please check before using it.
%% The Script
% Parameters
c = 1;
% Simulation time span
tStart = 0;
h = 0.2; % step size
tEnd = 10;
t = (tStart:h:tEnd);
% Data
tdata = 0:0.5:10;
dydata = - exp(-c*tdata);
% Ordinary Differential Equation with Time-Dependent Terms
dy = @(t) interp1(tdata, dydata, t);
odefcn = @(t) dy(t);
% Initial value
y0 = 1;
% Call ode3 Solver
yode3 = ode3(@(t, y) odefcn(t), t, y0);
% True solution
ysol = y0*exp(-c*t);
% Plot the solution
figure
hold on
plot(t, yode3)
plot(t, ysol, 'o')
hold off
grid on
xlabel('Time')
ylabel('Amplitude')
title('Bogacki–Shampine method')
legend('Numerical solution', 'True solution', 'location', 'best')
%% Bogacki–Shampine method (from your Wikipedia link)
function y = ode3(f, t, y0)
y(:, 1) = y0; % initial condition
h = t(2) - t(1); % step size
n = length(t); % number of steps
for j = 1 : n-1
k1 = f(t(j), y(:, j));
k2 = f(t(j) + 1/2*h, y(:, j) + 1/2*h*k1);
k3 = f(t(j) + 3/4*h, y(:, j) + 3/4*h*k2);
y(:, j+1) = y(:, j) + 2/9*h*k1 + 1/3*h*k2 + 4/9*h*k3;
end
end
댓글 수: 2
Sam Chak
2025년 5월 16일
이동: Sam Chak
2025년 5월 16일
Hi @C
It may be confusing for you that I used the solution itself to artificially generate the data for the demo previously. This time, only plain data is used. The code for solving the data-based derivative remains unchanged. The ode3 solver doesn't require the derivative equation at all, only the data.
%% The Script
% Parameters
c = 1;
% Simulation time span
tStart = 0;
h = 0.2; % step size
tEnd = 10;
t = (tStart:h:tEnd);
% Data
tdata = [0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0];
dydata = [-1 -0.60653066 -0.367879441 -0.22313016 -0.135335283 -0.082084999 -0.049787068 -0.030197383 -0.018315639 -0.011108997 -0.006737947 -0.004086771 -0.002478752 -0.001503439 -0.000911882 -0.000553084 -0.000335463 -0.000203468 -0.00012341 -7.48518E-05 -4.53999E-05];
% Ordinary Differential Equation with Time-Dependent Terms
dy = @(t) interp1(tdata, dydata, t);
odefcn = @(t) dy(t);
% Initial value
y0 = 1;
% Call ode3 Solver
yode3 = ode3(@(t, y) odefcn(t), t, y0);
% True solution
ysol = y0*exp(-c*t);
% Plot the solution
figure
hold on
plot(t, yode3)
plot(t, ysol, 'o')
hold off
grid on
xlabel('Time')
ylabel('Amplitude')
title('Bogacki–Shampine method')
legend('Numerical solution', 'True solution', 'location', 'best')
%% Bogacki–Shampine method (from your Wikipedia link)
function y = ode3(f, t, y0)
y(:, 1) = y0; % initial condition
h = t(2) - t(1); % step size
n = length(t); % number of steps
for j = 1 : n-1
k1 = f(t(j), y(:, j));
k2 = f(t(j) + 1/2*h, y(:, j) + 1/2*h*k1);
k3 = f(t(j) + 3/4*h, y(:, j) + 3/4*h*k2);
y(:, j+1) = y(:, j) + 2/9*h*k1 + 1/3*h*k2 + 4/9*h*k3;
end
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Sources에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!