Create continuous custom functions like the built in's

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 commentaires

Is your TIME_CONSUMING_FUNCTIONS (which I'll abbreviate TCF) vectorized or parallelized or does it only accept a scalar as input and return a scalar? Can it be vectorized or parallelized?
Are you executing this as part of an iterative process, where the value of TCF(x) at one iteration is used to compute the value of x for the next iteration? Or are all the values of x available at once and you're just evaluating TCF on one value of x at a time?
Can you show the mathematical form of TCF or say a little more about what it actually is? It's possible there's already a function in MATLAB (or one of the toolboxes) to evaluate it in a vectorized fashion.
Basil Tsekenis
Basil Tsekenis le 12 Juil 2022
Modifié(e) : Basil Tsekenis le 12 Juil 2022
The TCF cannot be changed.
The x each time is random. I think the most suitable solution is to calculate the f for many values of x's and store it in a new vector. For the rest (let's say in between) to use interpolation on the new vector..
dpb
dpb le 13 Juil 2022
The above is a fairly common strategy in Monte Carlo simulation of large, complex, time-consuming models -- replace the detailed TCF with a RSM (response surface model). Typically, a RSM is a quadratic polynomial model in some dimension of input variables. You don't indicate if X above is a scalar or vector. There's a whole statistical modeling arena of design for RSM to aid in effective building of such models.
x is a scalar. didn't really get what RSM is..
Stephen23
Stephen23 le 14 Juil 2022
Modifié(e) : Stephen23 le 14 Juil 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...

Connectez-vous pour commenter.

Réponses (1)

Simar
Simar le 6 Oct 2023
Modifié(e) : Simar le 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

Catégories

En savoir plus sur Interpolation dans Centre d'aide et File Exchange

Produits

Version

R2022a

Modifié(e) :

le 6 Oct 2023

Community Treasure Hunt

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

Start Hunting!

Translated by