Not sure why I am getting errors for my anonymous functions.

6 次查看(过去 30 天)
I have this code:
k = randn(1); % random variable
% define all of your variables below
f = @(x) (sin(x)+4.*x)/x.^2
A = f(5)
g = @(x,k) exp(cos(x)).^k
B = g(pi/4,k)
x = [0:0.1:1]
C = g(f(g(x)))
and when I run it I get these two errors:
Not enough input arguments.
Error in solution>@(x,k)exp(cos(x)).^k (line 5)
g = @(x,k) exp(cos(x)).^k
Error in solution (line 8)
C = g(f(g(x)))

回答(2 个)

Voss
Voss 2023-2-12
That's one error.
g takes two inputs, but you are giving it only one when you use it here:
C = g(f(g(x)))
The input given to g is x. What k is it supposed to use?

Torsten
Torsten 2023-2-13
编辑:Torsten 2023-2-13
This would work:
k = randn(1); % random variable
% define all of your variables below
f = @(x) (sin(x)+4*x)./x.^2;
A = f(5);
g = @(x,k) exp(cos(x)).^k;
B = g(pi/4,k);
x = [0:0.1:1];
C = g(f(g(x,k)),k)
C = 1×11
0.9670 0.9671 0.9675 0.9680 0.9688 0.9698 0.9710 0.9725 0.9741 0.9759 0.9778

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by