Write a function with three input arguments: a vector, math handle, plot handle?

10 ビュー (過去 30 日間)
Samantha Farmer
Samantha Farmer 2018 年 10 月 29 日
コメント済み: madhan ravi 2018 年 10 月 30 日
The question is: Write a function that will receive three input arguments: first argument is the data in the form of a x vector; the second argument is a handle to a math function; and the third argument is the handle to a plot function. Your Matlab function will first calculate the math function values of the x vector, then it will produce a plot using the third input argument.
For example, a call to the function HW101(x, @sin, @plot) will plot x vs. sin(x) using the line plot function.
Here is what I have so far, but you have to define the x vector, and should I be using varargin? Also this function prints out an answer? Any ideas on how to only display the plot?
% code
function[] = hw10(x,fhand,plot)
fhand(x)
plot(fhand(x))
title(['Plot Using: ',func2str(fhand)])
end
  1 件のコメント
Samantha Farmer
Samantha Farmer 2018 年 10 月 30 日
So I think I figured it out with:
% code
function[] = hw10(x,fhand,plothand)
y=fhand(x);
plothand(x,y);
end
the only thing is that you have to define the x vector, is that fine?

サインインしてコメントする。

採用された回答

madhan ravi
madhan ravi 2018 年 10 月 30 日
x=-pi:0.01:pi
hw10(x,@sin) %function calling
function hw10(x,fhand)
y=fhand(x);
plot(x,y);
end
  1 件のコメント
madhan ravi
madhan ravi 2018 年 10 月 30 日
If you just want the plot to be displayed you don’t have to put output for your function.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by