Replace several functions with a one "master" function

1 visualizzazione (ultimi 30 giorni)
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 Commento
Stephen23
Stephen23 il 1 Ott 2018
How does the factor depend on the sequence? Do you have a formula for the factor?

Accedi per commentare.

Risposte (1)

Jan
Jan il 1 Ott 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.

Community Treasure Hunt

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

Start Hunting!

Translated by