0

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?

1
  • 1
    What are you trying to accomplish? Your syntax doesn't make much sense. Are you expecting test to return three values? Make it return a 3-value vector, then say final(:,i)=test(x,y(i)). And do preallocate final! Commented Mar 24, 2021 at 14:45

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

3 Comments

I would like my main loop to output res2,res3,res4 10 times based on my linspace definition, ie, 3 by 10 matrix.
@user9106985 then define function [res2, res3, res4]=test(x,y), but you need to capture that output with something else, like final(:,i)
Thanks! I will check this later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.