Plotting an Anonymous Function

20 ビュー (過去 30 日間)
Jake Farley
Jake Farley 2017 年 1 月 31 日
コメント済み: Jake Farley 2017 年 1 月 31 日
I have created an anonymous function
sqr = @(x) x.^2;
and I am trying different methods for plotting this function, and none seem to plot the function correctly (aside from ezplot) I have tried
figure
plot(sqr(-5:5))
figure
fplot(sqr(-5:5))
Using plot(), it creates a correct looking plot but my axes are not correct, it displays the function from 1 to 11, centered at 6. (so the range is correct)
using fplot() it creates horizontal lines from x = -5:5 at y = 0, 2, 4, 9, 16, 25
Is this what is supposed to happen and I just don't fully understand the functions yet? Or have I set my function up incorrectly?
Thanks.

採用された回答

James Tursa
James Tursa 2017 年 1 月 31 日
編集済み: James Tursa 2017 年 1 月 31 日
You need to explicitly give plot the "x" information, not just the "y" information. E.g.,
x = -5:5;
plot(x,sqr(x))
  1 件のコメント
Jake Farley
Jake Farley 2017 年 1 月 31 日
Thanks! Such a simple mistake.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by