Replace several functions with a one "master" function
Afficher commentaires plus anciens
I need to evaluate different integral functions which basically consist of the product of two factors, one of which stays the same and the other changes slightly. So far I´m doing it by creating different functions but I´m wondering if there´s a way of creating only one function and passing the changing factor as an argument. As a simple example, I need to compute the integrals of something like the following:
y1 = (x + 1)
y2 = x * ( x + 1)
y3 = 2/x * (x + 1)
y4 = (x - 2)/x * (x + 1)
So as you can see, all functions could be expressed as a product A * y1, and it would make my script much cleaner if I could avoid defining a different function for any minor change. Since this A factor is not a constant but it includes the variable itself I haven´t figured out how to pass it as an argument. Is it even possible? I have the feeling it might be possible with function handles but I don´t fully understand those yet either.
1 commentaire
Stephen23
le 1 Oct 2018
How does the factor depend on the sequence? Do you have a formula for the factor?
Réponses (1)
Jan
le 1 Oct 2018
What about:
funcList = {@(t,x) (x + 1); ...
@(t,x) x * ( x + 1); ...
@(t,x) 2/x * (x + 1); ...
@(t,x) (x - 2)/x * (x + 1)};
Now e.g. funcList{3} is the 3rd function.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!