Not enough input arguments for an anonymous function.

조회 수: 20 (최근 30일)
Connor
Connor 2023년 2월 12일
편집: the cyclist 2023년 2월 12일
I have this anonymous function:
g = @(x,k) exp(cos(x))^k
I am getting this error:
Not enough input arguments.
Error in solution>@(x,k)exp(cos(x))^k (line 5)
g = @(x,k) exp(cos(x))^k
Im not sure the fix for this because x and k are both represented in the function.
  댓글 수: 2
Torsten
Torsten 2023년 2월 12일
g = @(x,k) exp(cos(x)).^k;
g(2,4)
ans = 0.1893
Voss
Voss 2023년 2월 12일
x and k are both represented in the function, yes, but how are you calling the function? If you call it with fewer than two arguments you'll get that error.
% defining g:
g = @(x,k) exp(cos(x))^k
g = function_handle with value:
@(x,k)exp(cos(x))^k
% calling g with 2 arguments:
g(pi,2) % ok
ans = 0.1353
% calling g with 1 argument:
g(pi) % error
Not enough input arguments.

Error in solution (line 1)
g = @(x,k) exp(cos(x))^k
You haven't shown how you are calling the function g, so check on that.

댓글을 달려면 로그인하십시오.

답변 (1개)

the cyclist
the cyclist 2023년 2월 12일
편집: the cyclist 2023년 2월 12일
I think you probably wanted to raise to the power element-wise, so I changed your function to
g = @(x,k) exp(cos(x)).^k;
But that won't cause the error you got.
How are you calling it? This works:
g = @(x,k) exp(cos(x)).^k;
g(1:5,3)
ans = 1×5
5.0577 0.2870 0.0513 0.1407 2.3420

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by