Clear Filters
Clear Filters

Create continuous custom functions like the built in's

13 views (last 30 days)
Hey, I'm trying to create a custom function like "cos", 'sin", "exp".
At the time I have sth like
f = @(x) TIME_CONSUMING_FUCTIONS(x)
I need to calculate f for many inpus and that takes a lot of time because the handler is running at each loop.
Can make it like a ready-made function which will give me instantly the answers? Maybe running it for a wide range of x's and use interpolation after? Don't have an idea.
Thanks!
  5 Comments
Stephen23
Stephen23 on 14 Jul 2022
Edited: Stephen23 on 14 Jul 2022
"Can make it like a ready-made function which will give me instantly the answers?"
"Maybe running it for a wide range of x's and use interpolation after?
If you know the data range AND can generate sufficient points to give you the required output accuracy...

Sign in to comment.

Answers (1)

Simar
Simar on 6 Oct 2023
Edited: Simar on 6 Oct 2023
Hi Basil,
I understand that you are seeking a faster way to calculate a custom time-consuming function, potentially by precomputing values and using interpolation for new inputs.Yes, it can be achieved and is a good strategy to speed up the execution. Here is an example that would help:
1. Define your function. For example, let us assume the function is a complex mathematical expression and is vectorized and can handle array inputs:
TIME_CONSUMING_FUNCTION = @(x) sin(x) + cos(x.^2) + exp(x.^3);
2. Define range of inputs for which you want to pre-calculate function values:
x_min = -10;
x_max = 10;
step_size = 0.01;
3. Calculate function values for this range of inputs and store them in a reference table for future use:
x_array = x_min:step_size:x_max;
f_array = TIME_CONSUMING_FUNCTION(x_array);
4. Create an anonymous function that uses interpolation to estimate function value for any input within the predefined range. The spline option in the interp1 function specifies that spline interpolation should be used. This method can provide a good approximation for many types of functions.
f = @(x) interp1(x_array, f_array, x, 'spline');
Use f(x) to get interpolated value. This should be much faster than calling TIME_CONSUMING_FUNCTION(x), especially if x is a large array.
Here is the complete code:
% Define the time-consuming function
TIME_CONSUMING_FUNCTION = @(x) sin(x) + cos(x.^2) + exp(x.^3);
% Define the range of inputs and the step size
x_min = -10;
x_max = 10;
step_size = 0.01;
% Create the input array
x_array = x_min:step_size:x_max;
% Calculate the function values for these inputs
f_array = TIME_CONSUMING_FUNCTION(x_array);
% Create an anonymous function for interpolation
f = @(x) interp1(x_array, f_array, x, 'spline');
% Now you can use f(x) to get the interpolated value at ‘x = 0.5’
result = f(0.5);
This approach works well when your function is smooth and does not have rapid changes. If the function has rapid changes, you may need to use a smaller step size or a more sophisticated interpolation method.
Please refer to the documentation link-
Hope it helps!
Best Regards,
Simar

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!