Nick M
Nick M

Reputation: 11

Adding an arbitrary number of functions into a function handle MATLAB

I'm trying to generate .bmp graphics in MATLAB and I'm having trouble summing functions together. I'm designing my function such that given an arbitrary set of inputs, my function will add an arbitrary number of functions together and output a function handle. The inputs are coefficients to my general function so I can specify any number of functions (that only differ due to their coefficients) and then add them together into a function handle. What I've tried to do is create each function as a string and then concatenate them and then write them as a function handle. The main problem is that because x and y aren't defined (because I'm trying to create a function handle) MATLAB can't add them regularly. My current attempt:

    function HGHG = anyHGadd(multi) %my array of inputs
    m=length(multi);
    for k=3:3:m;
    m1=multi(k-2); %these three are the coefficients that I'd like to specify
    n1=multi(k-1);
    w1=multi(k);
    HGarrm1=hermite(m1); %these generate arrays
    HGarrn1=hermite(n1);
    arrm1=[length(HGarrm1)-1:-1:0];%these generate arrays with the same length
    arrn1=[length(HGarrn1)-1:-1:0];%the function below is the general form of my equation
    t{k/3}=num2str(((sum(((sqrt(2)*x/w1).^arrm1).*HGarrm1))*(sum(((sqrt(2)*y/w1).^arrn1).*HGarrn1))*exp(-(x^2+y^2)/(w1^2))));
    end
    a=cell2mat(t(1:length(t)));
    str2func(x,y)(a);

Any help would be much appreciated. I haven't seen much on here about this, and I'm not even sure this is entirely possible. If my question isn't clear, please say so and I'll try again.

Edit: The fourth from last line shouldn't produce a number because x and y aren't defined. They can't be because I need them to be preserved as a part of my function handle. As for a stripped down version of my code, hopefully this gets the point across:

    function HGHG = anyHGadd(multi) %my array of inputs
    m=length(multi);
    for k=3:3:m;
    m1=multi(k-2); %these three are the coefficients that I'd like to specify
    n1=multi(k-1);
    w1=multi(k);
    t{k/3}=num2str(genericfunction(x,y,n1,m1,n1,w1); %where x and y are unspecified
    end
    a=cell2mat(t(1:length(t)));
    str2func(x,y)(a);

Edit I am expecting this to output a single function handle that is the sum of an arbitrary number of my functions. However, I'm not sure if using strings would be the best method or not.

Upvotes: 1

Views: 1346

Answers (2)

Amro
Amro

Reputation: 124573

Your question is not very clear to me, but I think you are trying to create a function that generate output functions parametrized by some input.

One way is to use closures (nested function that access its parent function workspace). Let me illustrate with an example:

function fh = combineFunctions(funcHandles)
    %# return a function handle
    fh = @myGeneralFunction;

    %# nested function. Implements the formula:
    %# f(x) = cos( f1(x) + f2(x) + ... + fN(x) )
    %# where f1,..,fN are the passed function handles 
    function y = myGeneralFunction(x)
        %# evaluate all functions on the input x
        y = cellfun(@(fcn) fcn(x), funcHandles);

        %# apply cos(.) to the sum of all the previous results
        %# (you can make this any formula you want)
        y = cos( sum(y) );
    end
end

Now say we wanted to create the function @(x) cos(sin(x)+sin(2x)+sin(5x)), we would call the above generator function, and give it three function handles as follows:

f = combineFunctions({@(x) sin(x), @(x) sin(2*x), @(x) sin(5*x)});

Now we can evaluate this created function given any input:

>> f(2*pi/5)         %# evaluate f(x) at x=2*pi/5
ans =
     0.031949

Note: The function returned will work on scalars and return a scalar value. If you want it vectorized (so that you can apply it on whole vector at once f(1:100)), you'll have to set UniformOutput to false in cellfun, then combine the vectors into a matrix, sum them along the correct dimension, and apply your formula to get a vector result.

Upvotes: 3

Jonas
Jonas

Reputation: 74940

If your goal is to create a function handle that sums the output of an arbitrary number functions, you can do the following:

n = 3; %# number of function handles
parameters = [1 2 4];
store = cell(2,3);

for i=1:n
  store{1,i} = sprintf('sin(t/%i)',parameters(i));
  store{2,i} = '+'; %# operator
end

%# combine such that we get
%# sin(t)+sin(t/2)+sin(t/4)
funStr = ['@(t)',store{1:end-1}]; %# ignore last operator

functionHandle = str2func(funStr)

functionHandle = 

    @(t)sin(t/1)+sin(t/2)+sin(t/4)

Upvotes: 0

Related Questions