Function to Create a Function

2 views (last 30 days)
Mat Arnold
Mat Arnold on 22 May 2021
Answered: Samay Sagar on 16 Feb 2024
Currently I have a function that manupulates variables and symbolic variables, the uses "matlabFunction()" to generate a stand alone function for me. For example (not a real function but illustrative of the usage:
function stateFunction = EOM_Constructor(x, y, z)
syms u v w
dotX = x * u;
dotY = y * v;
dotZ = z * w;
stateFunction = matlabFunction([dotX;dotY;dotZ],'Vars',{t,[u;v;w]});
end
This works really well for me, as I end up using 'stateFunction' in ode45, and x, y, and z are constant for the duration of a run. Unfortunatey it looks like as I move forward I am going to need to use interpolation, which doesn't work with symbolic variables. I have many parameters that are constant during a run, and ode45 ends up calling 'stateFunction' 100s of thousands of times, so I'd really prefer not to make them arguments of 'stateFunction'. I could get around this by making x y and z global variable but that feels like bad practice.
Is there a way to do as I have above, x y z are parameters of a function I call once, which outputs a function I can call many times with parameters (t,[u;v;w]) without using symbolic variables? I tried nesting a function and passing a function handle of the nested function as the outer functions output but that did not work, the nested function is not created as 'standalone'. For example :
function stateFunction = function_builder(x,y,z)
function dotVector = f(t, U)
U(1) = u;
U(2) = v;
U(3) = w;
dotX = x * u;
dotY = y * v;
dotZ = z * w;
dotVector = [dotX;dotY;dotZ];
end
stateFunction = @f;
end
This lets me call 'stateFunction' by itself, but it doesn't 'save' x y and z as their constant values and the function behaves abnormally.
Thanks!

Answers (1)

Samay Sagar
Samay Sagar on 16 Feb 2024
To create a MATLAB function with fixed parameters that can be called multiple times without passing these parameters each time, you can utilize anonymous functions. This method allows you to capture the constant values of the parameters at the time the function handle is created.
Here's a concise way to achieve this using anonymous functions:
function stateFunction = function_builder(x, y, z)
% This anonymous function captures the values of x, y, and z
stateFunction = @(t, U) [
x * U(1); % Computes dotX using the constant value of x
y * U(2); % Computes dotY using the constant value of y
z * U(3); % Computes dotZ using the constant value of z
];
end
When you call function_builder with specific values for x, y, and z, it returns a function handle stateFunction that retains those values. The function handle stateFunction can now be used with “ode45”, and it will consistently use the provided constant values of x, y, and z for each call made by the solver.
Read more about parameterizing functions, anonymous function and the “ode” function here:

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!