Error with plotting my function

1 回表示 (過去 30 日間)
Ethan Bales
Ethan Bales 2021 年 9 月 18 日
コメント済み: Star Strider 2021 年 9 月 18 日
Having issues getting my nonlinear function to plot.
f = @(x) 2.^x-5*x+2; %Given Function
xaxis = [0:0.5:2]; %x-axis
plot(xaxis,f) %plotting function //DOESN'T WORK
  2 件のコメント
Jan
Jan 2021 年 9 月 18 日
"Doesn't work" does not explain the problem. You get an error message:
Error using plot
Invalid data argument.
This is an important hint. The 2nd input is a function handle, but Matlab expect numerical values.
For a solution see Star Strider's answer.
Ethan Bales
Ethan Bales 2021 年 9 月 18 日
yeah sorry about that. This is my first time using a forum like this and I'm still relatively new to coding. Thanks for insight!

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

採用された回答

Star Strider
Star Strider 2021 年 9 月 18 日
It works if you give ‘f’ a vector to work with
f = @(x) 2.^x-5*x+2; %Given Function
xaxis = [0:0.5:2]; %x-axis
plot(xaxis,f(xaxis)) %plotting function //DOESN'T WORK —> YEs, IT DOES!
Experiment to get different results.
.
  2 件のコメント
Ethan Bales
Ethan Bales 2021 年 9 月 18 日
ahhhh I see the issue now, thanks for help!!
Star Strider
Star Strider 2021 年 9 月 18 日
My pleasure!
.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by