Replace several functions with a one "master" function
Show older comments
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 Comment
Stephen23
on 1 Oct 2018
How does the factor depend on the sequence? Do you have a formula for the factor?
Answers (1)
Jan
on 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.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!