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

조회 수: 1 (최근 30일)
%plot to view graph
g =@(x) 3*x^3 +x^2 -2*x - 5;
figure(2)
o = -40:40;
fplot(g,[-40 40])

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by