shoggananna
shoggananna

Reputation: 565

array of anonymous function inside a function matlab

I have defined an array of anonymous functions in side a function as shown below.

function test(x,y)

    f={@(x,y) (3*y*x^2)
       @(x,y) (x*y)
       @(x,y) (x*2*y^2)
       @(x,y) (2*x*y)}
res2=f{2}(x,y)-2*f{1}(x,y)
res3=f{3}(x,y)-5*f{2}(x,y)
res4=f{4}(x,y)-4*f{2}(x,y))
    
end

I want to obtain a 3 by 10 matrix via

x=2
y=linspace(0.0001,0.001,10)
for i=1:length(y)
final(i)=test(x,y(i));
end

However, I get an error stating there are too many input variables. How could I correct this?

Upvotes: 0

Views: 190

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

function test(x,y)

This function returns nothing. So in your main code, you are asking too many output arguments.

You define functions that return as:

function [output1, output2 , ... , outputn]=f(input1, input2, ... , inputn)

Not sure in your case what you want, as your main loop only captures 1 output, yet inside the function you compute 3 variables.

Upvotes: 2

Related Questions