Choosing an ode solver in function input

3 views (last 30 days)
Hi everyone,
I am working on a function that simulates a heat storage tank and uses ODE solvers to calculate the energy balances. The tank uses heat exchangers (HX), direct dis/charging ports (DP) and an auxiliary heater (AUX).
The ODE solvers ode45, ode23, ode23tb and ode113 all generate correct results, but the speed of each one depends on whether or not DP, HX, AUX, or a combination of two or three are used, and also on the external time step of the simulation "delt" (which can vary between 5 seconds and 3600 seconds).
So, for example, ode23 is fastest if delt = 60 and only DP and HX are used, but if delt >= 900, ode23tb is faster. If all three are used, ode45 is fastest for delt <= 60, ode113 is fastest for 120 <= delt <= 170, and ode23 is fastest for delt > 170.
Currently, I use if conditions in the function to determine which solver to use. This can save time compared to using a slower solver, but it can also almost double the time needed to complete the simulation compared to using the fastest solver.
I hope I explained my problem well enough... Here's my question:
To optimize speed, I would like to have the user determine which solver to use via input of the function, for example:
[outputs] = MarcsHeatStorageTank(inputs, 'ode23')
or
[outputs] = MarcsHeatStoragetank(inputs, 1) % the number indicates which ODE solver to use
Is there a way to do this without using eval() or if conditions?
Thanks!
- Marc

Accepted Answer

Marc Jakobi
Marc Jakobi on 22 May 2015
Never mind, I figured it out.
I decided to pass the solver as a function handle. So the function is written as
function [outputs] = MarcsHeatstorageTank(solver,inputs)
...
...
...
[results] = solver(@odefun,timeframe,y0);
...
...
end
and called as
[outputs] = MarcsHeatstorageTank(@ode23,inputs)
for example. The simulation speed seems to be fine now.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!