matlab: making the domain of a function dependent on the domain of the inputs

0

Suppose one has functions f(i) and g(j). How could one write a function h(k), where the domain k of h is made up of all k=i+j (i.e., each point h(k) is some function of f at i and g at j for all pairs of i and j satisfying k=i+j). For instance:

enter image description here

for all k=i+j. The domain of h would thus be k=2:25 and, for instance, h(3) would be equal to f(1)*g(2) + f(2)*g(1) since both of these combinations satisfy k=i+j.

This is simple to do using loops, but I wish to compose the function in anonymous function form (i.e., h = @(k) f(i) ... g(j)). How can this be accomplished?

user001

Posted 2016-10-16T23:36:54.303

Reputation: 2 074

According to your formal example (with the sigma sums), h is constant. Want you actually want is an algorithm to find all valid sets of i,j for a given k, such that k=i+j. Sounds like a math problem. – jiggunjer – 2016-10-17T02:45:50.927

Answers

1

Let a and b be known variables for the domains i and j. Then the function you describe might look like this:

fun=@(k) sum(sum(transpose(f1(k-b(ismember(b,(k-a)))))*f2(b(ismember(b,(k-a))))))

where f1 and f2 are anonymous functions corresponding to f(i) and g(j). k is a valid scalar.

Note: it might not be considered good practice to use an anonymous function for something non-trivial.

Note2: I haven't considered scenarios with non-unique domains for i and j, nor negative values.

jiggunjer

Posted 2016-10-16T23:36:54.303

Reputation: 831