matlab中匿名函数的问题
我打印h_a_b时遇到问题。我能得到函数f和g,但不能得到这个。我需要使用h_a_b函数,这样我可以做h(f(x),g(x))并计算h(a,b)的sqrt。见方程式 我总是犯这样的错误matlab中匿名函数的问题,matlab,anonymous-function,function-handle,Matlab,Anonymous Function,Function Handle,我打印h_a_b时遇到问题。我能得到函数f和g,但不能得到这个。我需要使用h_a_b函数,这样我可以做h(f(x),g(x))并计算h(a,b)的sqrt。见方程式 我总是犯这样的错误 Undefined function 'h_a_b' for input arguments of type 'function_handle'. 我想写一个程序,创建3个表示函数的匿名函数 需要的方程式 f(x)=10*cos x g(x)=5*sin*x,和 h(a,b)=\sqrt(a^2+b^2) 这是
Undefined function 'h_a_b' for input arguments of type 'function_handle'.
我想写一个程序,创建3个表示函数的匿名函数
需要的方程式
f(x)=10*cos x
g(x)=5*sin*x,和
h(a,b)=\sqrt(a^2+b^2)
这是我的密码
f = @ (x) 5*sin(x);
g = @ (x) 10*cos(x);
h_a_b = @ (a,b) sqrt(a.^2 + b.^2);
然后我用给我的这个函数绘制它
function plotfunc(fun,points)
%PLOTFUNC Plots a function between the specified points.
% Function PLOTFUNC accepts a function handle, and
% plots the function at the points specified.
% Define variables:
% fun -- Function handle
% msg -- Error message
%
msg = nargchk(2,2,nargin);
error(msg);
% Get function name
fname = func2str(fun);
% Plot the data and label the plot
plot(points,fun(points));
title(['\bfPlot of ' fname '(x) vs x']);
xlabel('\bfx');
ylabel(['\bf' fname '(x)']);
grid on;
end
因为您的函数(h_a_b
)将向量作为输入,并将标量作为输出,它表示一个曲面,因此plot
不能用于可视化它(这仅适用于二维标量图)
你在找这样的东西吗
f = @ (x) 5*sin(x);
g = @ (x) 10*cos(x);
h_a_b = @ (a,b) sqrt(a.^2 + b.^2);
z = @(a,b) sqrt(h_a_b(f(a),g(b)));
[A, B] = meshgrid(0:0.1:8, 0:0.1:9);
Z = z(A,B);
surfc(A,B,Z)
xlabel('a')
ylabel('b')
figure
contourf(A,B,Z)
xlabel('a')
ylabel('b')
第二个选项,将
z
视为标量函数,并使用plotfunc
函数:
f = @ (x) 5*sin(x);
g = @ (x) 10*cos(x);
h_a_b = @ (a,b) sqrt(a.^2 + b.^2);
z = @(x) sqrt(h_a_b(f(x),g(x)));
points = 0:0.1:8;
plotfunc(z,points)
哪一条线是曲面的一部分。哪一条线触发MATLAB控制台返回错误消息?我想您需要另一个匿名函数来表示所有内容。例如:h_x=@(x)h_a_b(f(x),g(x));哦我没有意识到!!!好主意!如果我把它画成二维,它会是什么样子?@user2353565请向上投票并接受答案。@kkuilla注意提问者还没有获得投票权(15分钟代表)。@DennisJaheruddin啊,没有想到这一点。嗯,现在他们做到了…:-)