Replace several functions with a one "master" function

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 个评论

How does the factor depend on the sequence? Do you have a formula for the factor?

请先登录,再进行评论。

回答(1 个)

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.

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by