Graph gives warning : function behaves unexpectedly on array inputs. What does this mean?

 採用された回答

Chris
Chris 2022 年 9 月 17 日
編集済み: Chris 2022 年 9 月 17 日
You are using matrix (linear algebra) operators, which include * and ^. If you try to calculate the function directly, You get an error because raising a vector to a power doesn't work.
g =@(x) 3*x^3 +x^2 -2*x - 5;
o = -40:40;
y = g(o);
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise
power.

Error in solution (line 1)
g =@(x) 3*x^3 +x^2 -2*x - 5;
To operate on each x value individually, use elementwise operators (e.g, .^ .* ./ )
g =@(x) 3 .* x .^ 3 + x .^ 2 - 2 .* x - 5;
fplot(g,[-40,40])

3 件のコメント

g =@(x) 3 .* x .^ 3 + x .^ 2 - 2 .* x - 5;
fplot(g,[-40,40])
Stephanie Mendoza
Stephanie Mendoza 2022 年 9 月 17 日
Thank you soo much!! The error went away!
Chris
Chris 2022 年 9 月 17 日
No problem. :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSpecialized Power Systems についてさらに検索

製品

リリース

R2022a

質問済み:

2022 年 9 月 17 日

コメント済み:

2022 年 9 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by