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

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 Comments

Here is my revised script:
But I still have errors which I am not sure what they are?
function plot2fnhand( fh1, fh2 )
x = rand(100);
subplot(2,1,1);
y = fh1(x);
plot(x,y);
title(func2str(fh1));
subplot(2,1,2);
y = fh1(x);
plot(x,y);
title(func2str(fh1));
subplot(2,1,1);
y = fh2(x);
plot(x,y);
title(func2str(fh2));
subplot(2,1,2);
y = fh2(x);
plot(x,y);
title(func2str(fh2));
end
Doesn't it tell you what they are, in red text? How are you calling this function? What are fh1 and fh2? How did you create those and pass them into this functions? Please give the code for the calling routine also so we can know how to call this function. Also, please read and understand this: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
You make two mistakes I guess. The first is that you plot twice each function (why?), and the second is that you don't call RAND in a correct fashion. The way you do it in the current version of your code creates a 100x100 array of random numbers uniformly distributed on the interval (0,1). Instead, you want a vector of 100 random numbers in the range [1,100] (or maybe (1,100) to make it simpler).
  • 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
???

Sign in to comment.

 Accepted Answer

"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.

More Answers (0)

Asked:

on 2 Nov 2013

Answered:

on 4 Nov 2013

Community Treasure Hunt

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

Start Hunting!