3

I have N functions in MATLAB and I can define them using strcat, num2str and eval in a for loop. So without defining by hand I am able to define N functions. Let N=4 and let them be given as follows:

f1=@(x) a1*x+1;
f2=@(x) a2*x+1;
f3=@(x) a3*x+1;
f4=@(x) a4*x+1;

Now I add these four functions and I can do this by hand as follows:

f=@(x)(f1(x)+f2(x)+f3(x)+f4(x));

Here I can do it by hand because I know that N=4. However, in general I never know how many functions I will have. For all cases I cannot write a new function.

Is there any way to do this automatically? I mean if I give N=6 I am expecting to see MATLAB giving me this:

f=@(x)(f1(x)+f2(x)+f3(x)+f4(x)+f5(x)+f6(x));

Whenever I give N=2 then I must have the function f, defined as follows:

f=@(x)(f1(x)+f2(x));

How can we do this?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120

1 Answers1

2

First of all, you should read this answer that gives a series of reasons to avoid the use of eval. There are very few occasions where eval is necessary, in all other cases it just complicates things. In this case, you use to dynamically generate variable names, which is considered a very bad practice. As detailed in the linked answer and in further writings linked in that answer, dynamic variable names make the code harder to read, harder to maintain, and slower to execute in MATLAB.

So, instead of defining functions f1, f2, f3, ... fN, what you do is define functions f{1}, f{2}, f{3}, ... f{N}. That is, f is a cell array where each element is an anonymous function (or any other function handle).

For example, instead of

f1=@(x) a1*x+1;
f2=@(x) a2*x+1;
f3=@(x) a3*x+1;
f4=@(x) a4*x+1;

you do

N = 4;
a = [4.5, 3.4, 7.1, 2.1];
f = cell(N,1);
for ii=1:N
   f{ii} = @(x) a(ii) * x + 1;
end

With these changes, we can easily answer the question. We can now write a function that outputs the sum of the functions in f:

function y = sum_of_functions(f,x)
   y = 0;
   for ii=1:numel(f)
      y = y + f{ii}(x);
   end
end

You can put this in a file called sum_of_functions.m, or you can put it at the end of your function file or script file, it doesn't matter. Now, in your code, when you want to evaluate y = f1(x) + f2(x) + f3(x)..., what you write is y = sum_of_functions(f,x).

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • I just changed my code to cell structure but I still have the same problem. From your answer I am able to evaluate the sum of N functions but I am not able to obtain them. Why do I need it at all? Because there is a function FindRoots(fun, parameters) that I must call. Here fun is the sum of N functions. That's why I cannot simply evaluate the sum of N functions and be happy. I must send it to another function and another function is not written by myself. What to do here? – Seyhmus Güngören Sep 30 '19 at 12:22
  • @SeyhmusGüngören: You can use `@sum_of_functions` to get a handle to the function. That is what you would pass as an argument to your `FindRoots` function. – Cris Luengo Sep 30 '19 at 12:37
  • I could but I cannot.. Because your function has two inputs; f and x but FindRoots accepts one function handle with a single parameter x. – Seyhmus Güngören Sep 30 '19 at 12:50
  • Ah, sorry, yes. Then use `@(x) sum_of_functions(f,x)`. The `f` must be defined when you write that, and will become embedded into the anonymous function. @SeyhmusGüngören – Cris Luengo Sep 30 '19 at 12:55
  • I have the feeling that this is probably better than eval but it doesn't seem to be efficient. Because for every evaluation of x, one need to sum N functions. The complexity is N times more than usual. Normally, one can make one time N calculations and then for each evaluation of the summed function, one would need only 1 time computation instead of N. – Seyhmus Güngören Sep 30 '19 at 15:11
  • I just asked another question.. It seems that the denominator in my new question is making alot of problems to use the code you provided.. – Seyhmus Güngören Sep 30 '19 at 18:38