solutionFcn
Syntax
Description
specifies options using one or more name-value arguments. For example, specify
fh
= solutionFcn(F
,tmin,tmax
,Name=Value
)OutputVariables=[1 2]
to limit the function handle
fh
to interpolate only the first two solution components.
Examples
Interpolate ODE Solution
Create an ode
object for the equation @(t,y) y(1)
with the initial value y=1
.
F = ode(ODEFcn=@(t,y) y(1),InitialValue=1);
Create a function handle that can evaluate the solution at any time value in the range [0 10]
by using the solutionFcn
method.
fh = solutionFcn(F,0,10);
Use linspace
to generate 100 points, and then evaluate the solution at those points and plot the results.
t = linspace(0,10);
y = fh(t);
plot(t,y,"-o")
Interpolate ODE Solution Outside of Original Interval
Create an ode
object for the equations @(t,y) [y(2); 1000*(1-y(1)^2)*y(2)-y(1)]
with the initial conditions [2 0]
. Specify the solver as "stiff"
.
F = ode(ODEFcn=@(t,y) [y(2); 1000*(1-y(1)^2)*y(2)-y(1)],InitialValue=[2 0],Solver="stiff");
Create a function handle that can evaluate the solution of the problem in the interval [0 1000]
by using the solutionFcn
method.
Specify
Extension="on"
to enable the function handle to evaluate the solution at times outside of the original interval[0 1000]
.Specify
OutputVariables=1
so that the function handle interpolates only the first solution variable.Specify two outputs to also return the integration results in the original interval.
[fh,S] = solutionFcn(F,0,1000,Extension="on",OutputVariables=1)
fh = function_handle with value:
@ode.solutionFcn/interpolate
S = ODEResults with properties: Time: [0 1.4606e-05 2.9212e-05 4.3818e-05 1.1010e-04 1.7639e-04 2.4267e-04 3.0896e-04 4.5006e-04 5.9116e-04 7.3226e-04 8.7336e-04 0.0010 0.0012 0.0013 0.0015 0.0017 0.0018 0.0021 0.0024 0.0027 0.0030 0.0033 0.0044 0.0055 ... ] (1x227 double) Solution: [2x227 double]
Extract and plot the integration results for the first solution variable in S
, and then use the function handle fh
to also evaluate the solution in the extended interval [1000 3000]
. Plot the extended solution in red.
plot(S.Time,S.Solution(1,:),"-o") t = linspace(1000,3000); ys = fh(t); hold on plot(t,ys,"r-o") hold off
Input Arguments
F
— ODE problem to solve
ode
object
ODE problem to solve, specified as an ode
object.
tmin,tmax
— Time limits for interpolation
scalars
Time limits for interpolation, specified as two scalars that define the range of
acceptable values of t
that you can use as inputs to
fh
for interpolation. The tmin
and
tmax
arguments must satisfy tmin <= t0 <=
tmax
, where t0
is the value of
F.InitialTime
.
Example: fh = solutionFcn(F,0,10)
returns a function handle
fh
that can interpolate F
at any time in the
range [0 10]
.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: fh = solutionFcn(F,0,10,OutputVariables=[1
2],Extension="on")
OutputVariables
— Subset of variables for interpolation
scalar index | vector of indices
Subset of variables for interpolation, specified as a scalar index or vector of
indices. Use this option when the value returned by F.ODEFcn
is a
vector and you want to limit the number of variables that fh
can
interpolate. Specify one or more indices for the solution variables returned by
F.ODEFcn
that fh
can interpolate.
The value of this option affects only the behavior of fh
; it
does not affect the solution returned in S
. Use
F.SolverOptions.OutputSelection
to limit the number of output
variables returned in S
.
Example: fh = solutionFcn(F,0,5,OutputVariables=3)
returns a
function handle fh
that interpolates the third solution component
of F
.
Example: fh = solutionFcn(F,0,5,OutputVariables=[1 3])
returns a
function handle fh
that interpolates the first and third solution
components of F
.
Extension
— Time interval extension mode
"off"
(default) | "on"
Time interval extension mode, specified as "on"
or
"off"
. Use this option to enable querying fh
with time values outside of the solution interval [tmin tmax]
.
By default,
Extension="off"
and the commandfh(t)
returnsNaN
for values oft
outside of the solution interval[tmin tmax]
.If
Extension="on"
, then you can queryfh(t)
with values oft
outside of the solution interval[tmin tmax]
. This requires the solver to run again to extend the solution domain, so there can be a performance difference between querying points inside or outside the solution interval.
Example: fh = solutionFcn(F,0,3,Extension="on")
enables
fh
to be queried with time values outside of the interval
[0 3]
.
Output Arguments
fh
— Function for interpolation
function handle
Function for interpolation, returned as a function handle. fh
uses the built-in interpolant for the solver, which enables efficient interpolation
during intermediate steps of the integration. You can specify one or two outputs when
you use fh
:
y = fh(t)
returns the computed solution at the times specified byt
. The outputy
matchesS.Solution
ifS = solve(F,t)
.[y,dydt] = fh(t)
also returns the interpolated derivative valuesdydt
.
S
— Computed solution
ODEResults
object
Computed solution, returned as an ODEResults
object.
Version History
Introduced in R2023b
See Also
ode
| solve
| ODEResults
| odextend
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)