How to get the exact step size used by ode45?

7 ビュー (過去 30 日間)
伟
2024 年 10 月 23 日
コメント済み: 2024 年 10 月 24 日
Hi, everyone,
I am trying to solve GNLSE which has the following form:
,
,
.
It is a PDE describing pulse evolving along an optical fiber.
I can make a transformation so that it can be turned into an ODE, and the transformation is:
.
where h is the step size.
The resulting ODE is:
,
.
Now I want to solve the transformed equation using ode45.The RHS equation can be obtained numerically as long as the step size h is known. So how can I get the exact current step size used by ode45?

採用された回答

Torsten
Torsten 2024 年 10 月 23 日
移動済み: Torsten 2024 年 10 月 23 日
If the stepsize is part of the ODE equation, you cannot use ODE45 to solve. You have to write your own integrator.
  1 件のコメント
伟
2024 年 10 月 24 日
Thank you, Torsten. I will write my own integrator then.

サインインしてコメントする。

その他の回答 (1 件)

Rahul
Rahul 2024 年 10 月 23 日
Hi @伟,
I understand that you’re trying to solve a GLNSE PDE and making a variable transformation involving the current step size of the solver function, and you need the current step size adopted by the ode45 function at that time step.
In MATLAB, when using the ode45 function to solve ordinary differential equations (ODEs), the solver adapts the step size dynamically based on the error estimates of the solution. Unfortunately, there is no direct option to get the current step size at each iteration using just the ode45 function.
However, you can use an output function to access the step size and other solver details at each time step. You can set up an output function to capture the current step size at every iteration:
function dydt = odefun(t, y)
dydt = -2 * y + t; % Example ODE
end
% Define an output function to capture the step sizes
function status = outputFcn(t, y, flag)
persistent last_t
status = 0; % Status is used to stop integration if necessary (0 = continue)
if strcmp(flag, 'init') % Initialization
last_t = t; % Store the initial time
elseif isempty(flag) % During the integration process
step_size = t(end) - last_t(end); % Compute the step size
fprintf('Current step size: %f\n', step_size);
last_t = t; % Update the last time
elseif strcmp(flag, 'done') % Finalization
% Clear persistent variable
clear last_t
end
end
% Set options for ode45 with the output function
options = odeset('OutputFcn', @outputFcn);
% Call ode45 with the options
[t, y] = ode45(@odefun, [0 5], 1, options);
  • Output Function (outputFcn): This function is called by ode45 after every time step, to capture and display the step size at each iteration.
  • Step Size Calculation: The current step size is calculated by taking the difference between the current time (t(end)) and the previous time (last_t(end)).
For more information regarding usage of ‘ode45’ function to solve PDE(s), refer to the following documentation link:
Best
  2 件のコメント
Torsten
Torsten 2024 年 10 月 23 日
When the OutputFcn function is called, the step has finished. Thus it's too late to adjust the ODE function.
伟
2024 年 10 月 24 日
Thank you, Rahul. I guess that there is no perfect solution just using ode45. I will write my own integrator.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by