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

5 visualizaciones (últimos 30 días)
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 comentarios
Nora
Nora el 2 de Nov. de 2013
  • 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
???

Iniciar sesión para comentar.

Respuesta aceptada

Iain
Iain el 4 de Nov. de 2013
"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.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by