Function that will receive two function handles as input arguments and plot? - Homework

3 次查看(过去 30 天)
So I am working on this problem:
Write a function that will receive two function handles as input arguments adn will display plots of these functions in two subplots(column-wise) in a single figure window. The function will create an x vector that has 100 random numbers from 1 to 100.
example: if the function is called plot2fnhand
plot2fnhand(@sqrt,@exp)
the first subplot would display the sqrt function, the second would display exp(x) function.
This is what I have for my function so far:
function rfh = plot2fnhand( x, fh1, fh2 )
x = rand(100);
subpot(2,1,1);
y = fh1(x);
plot(x,y);
title(func2str(fh1));
subpot(2,1,2);
y = fh2(x);
plot(x,y);
title(func2str(fh2));
rfh = @testhd;
end
function testhd()
disp('HANDLE PASSING');
end
I keep on getting errors. ????
  5 个评论
Nora
Nora 2013-11-2
  • Item one
  • Item two
I am calling this function like the example above:
plot2fnhand(@sqrt,@exp)
x variable will be a random number between 1 to 100. and so then the two inputs will be plotted based on that.
I am confused on how to approach this problem.
I took out the second plot for both of them but it still doesn't allow me to plot them.
function plot2fnhand( fh1, fh2 )
x = rand(100); %\\ subplot(2,1,1);
y = fh1(x);
plot(x,y);
title(func2str(fh1));
subplot(2,1,1);
y = fh2(x);
plot(x,y);
title(func2str(fh2));
end
I get this error message:
EDU>> plot2fnhand(@sin,@cos)
Undefined function or variable 'Q1'.
Error in plot2fnhand (line 18)
y = fh2(x);Q1
???

请先登录,再进行评论。

采纳的回答

Iain
Iain 2013-11-4
"Undefined function or variable 'Q1'."
Looks like you have referred to Q1 without meaning to.
A function containing
function whatever
a = 1; b = 2; c = 3; disp(c); d
end
will throw a similar error because "d" is interpreted as code.
A function containing
function whatever
a = 1; b = 2; c = 3; disp(c); % d
end
will not throw a similar error because "%" tells matlab to ignore everything, following it on the same line.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by