Simple question about creating an anonymous function,
Accepted Answer
More Answers (1)
0 votes
Hi @Noob,
In MATLAB, when you create an anonymous function like myrhs = @(t, z) model(t, z, p), you are indeed specifying the inputs to the model, which are the current time t and the current state z. The parameter structure p is not included in the anonymous function because it is treated as a constant or fixed parameter that does not change during the integration process. The ode45 solver calls myrhs multiple times during its execution, passing different values of t and z while keeping p constant. This is a common practice in MATLAB to simplify the function signature when the parameters do not vary. If you were to include p in the anonymous function, it would imply that p could change with each call, which is not the case here. Thus, the correct interpretation is that @(t, z) specifies the inputs to the model, allowing ode45 to efficiently compute the solution while treating p as a fixed parameter. Here is a simple example for clarity:
function dzdt = model(t, z, p) dzdt = p.mass * z; % Example equation end
p.mass = 5; % Example parameter myrhs = @(t, z) model(t, z, p); % Anonymous function without p [t, z] = ode45(myrhs, [0 10], initial_conditions);
In this example, p is fixed, and myrhs only needs t and z as inputs.
Hope this helps.
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!