- Define the two functions: Start by defining the two functions 'a' and 'b'.
- Implement the Event Function: Event function will trigger when the integration time reaches 200 seconds. This function will stop the current integration. Set the 'Events' as this function's handle in odeset.
- Setup the Integration with Event Handling: Perform the first part of the integration using 'a'. When the event triggers, stop the integration.
- Continue Integration with 'b': Use the final state from the first integration as the initial condition for continuing the integration with 'b'.
state events - changing the function of the solution after a certain time value
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello everyone!
I'm using MATLAB for a university project, where I have the following problem: I have two similar functions let's call them 'a' and 'b'. The only difference between them is that 'b' consists of one additional constant parameter in the same equation. In the beginning the function 'a' is called and the integration is done accordingly. After a certain time value (e.g.: t =200s) the program should stop using 'a' for the solution and continue the integration with 'b'. Is it possible to do it by using state-events and if yes, then how? I would be very grateful, if you could help me how to do that.
0 Kommentare
Antworten (1)
Milan Bansal
am 3 Mai 2024
Hi Vörös Gergely,
I understand that you are trying to switch between two similar functions, 'a' and 'b', during the integration of an ODE in MATLAB. You wish to use 'a' for the initial phase of the integration and then switch to 'b' after t = 200s. This can be accomplished using state-events in MATLAB's ODE solvers.
Please follow the steps given below to achieve this:
Here is an example for your reference:
% Initial conditions
y0 = 1;
tspan = [0, 400]; % Total time span
% Perform the first part of the integration using 'a'
options1 = odeset('Events', @eventFcn);
[t1, y1, te, ye, ~] = ode45(@funcA, tspan, y0, options1);
% Continue with 'b' from the last point
tspan2 = [te, tspan(2)];
y0_2 = ye(end, :); % Initial condition for the next part
[t2, y2] = ode45(@(t, y) funcB(t, y), tspan2, y0_2);
% Combine the results
t = [t1; t2(2:end)]; % Avoid repeating the event time
y = [y1; y2(2:end)]; % Avoid repeating the event state
% Plot the results
figure;
plot(t, y, 'LineWidth', 2);
xlabel('Time');
ylabel('y');
grid on;
% function 'a'
function dydt = funcA(~, y)
dydt = -0.01 * y;
end
% function 'b' with an additional parameter
function dydt = funcB(~, y)
dydt = -0.01 * y + 0.5;
end
% Event function to switch from 'a' to 'b' at t = 200
function [value,isterminal,direction] = eventFcn(t, ~)
value = t - 200; % Trigger at t = 200
isterminal = 1; % Stop the integration
direction = 0; % Trigger regardless of the direction of the zero crossing
end
Pleasre refer to the following documentation link to learn more about odeset.
Hope this helps!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!