How to define nested function?

20 Ansichten (letzte 30 Tage)
Aarthika Balakumar
Aarthika Balakumar am 11 Apr. 2021
Bearbeitet: Cris LaPierre am 12 Apr. 2021
I want to define this f fucntion
f = @(x,y)((2.*x+y).*(x<d & y<d)+ (3.*x.*y-1).*(x>d & y<d)+...
(1+x+y).*(x<d & y>d)+ (3.*x+2.*y).*(x>d & y>d)+ ((2.*x(N/2)+y)+...
(3.*x(N/2+2).*y-1)/2).*(x==d & y<d) + (((2.*x+y(N/2))+...
(1+x+y(N/2+2)))/2).*(y==d & x<d)+(((1+x(N/2)+y)+...
(3.*x(N/2+2)+2.*y))/2).*(x==d & y>d)+(((3.*x.*y(N/2)-1)+...
(3.*x+2.*y(N/2+2)))/2).*(y==d & x>d)+(((2.*x(N/2)+y(N/2))+...
(3.*x(N/2+2).*y(N/2)-1)+(1+x(N/2)+y(N/2+2))+(3.*x(N/2+2)+...
2.*y(N/2+2)))/4).*(x==d &y==d));
If i define like this, I am getting error which is mentioned below
Error using
trying1>@(x,y)((2.*x+y).*(x<d&y<d)+(3.*x.*y-1).*(x>d&y<d)+(1+x+y).*(x<d&y>d)+(3.*x+2.*y).*(x>d&y>d)+((2.*x(N/2)+y)+(3.*x(N/2+2).*y-1)/2).*(x==d&y<d)+(((2.*x+y(N/2))+(1+x+y(N/2+2)))/2).*(y==d&x<d)+(((1+x(N/2)+y)+(3.*x(N/2+2)+2.*y))/2).*(x==d&y>d)+(((3.*x.*y(N/2)-1)+(3.*x+2.*y(N/2+2)))/2).*(y==d&x>d)+(((2.*x(N/2)+y(N/2))+(3.*x(N/2+2).*y(N/2)-1)+(1+x(N/2)+y(N/2+2))+(3.*x(N/2+2)+2.*y(N/2+2)))/4).*(x==d&y==d))
Too many input arguments.
Please help me to correct it. Thanks in advance.

Antworten (1)

Cris LaPierre
Cris LaPierre am 11 Apr. 2021
Bearbeitet: Cris LaPierre am 12 Apr. 2021
I get other errors when I try to implement your function. Perhaps you could share the code you ran that created this error?
The error message suggests you tried calling your function with more than 2 inputs.
To replicate your error, I'll define an anonymous function that has one input, but call it with 3.
a = @(x) (x-5).^2;
% correct - one input, a vector
a([3,5,7])
ans = 1×3
4 0 4
% incorrect - 3 inputs, scalars
a(3,5,7)
Error using solution
Too many input arguments.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by