Trouble With Graphing Functions

조회 수: 1 (최근 30일)
Josh Glenn
Josh Glenn 2016년 5월 19일
댓글: Star Strider 2016년 5월 20일
Hello...I am making a basic program that has the original function, the derivative, and the integral. I then am trying to have it graph them all on the same graph but for some reason it's not working. Every time I run the program an error comes up that says:
Error using plot Non-numeric data is not supported in 'Line'
Is this because I have x declared as a variable using syms x? If so, how do I keep x as a variable but still have it graph?
Thank you!
clc
clear
syms x; %Makes x a variable
%Original Function
O = 2*x; %THIS IS THE ONE THAT CAN BE CHANGED
display('Original Function')
pretty(simplify(O)) %Makes it nice
%Find the Derivative of the Original Function
D = diff(O);
display('Derivative')
pretty(simplify(D)) %Makes it nice
%Find the Integral of the Original Function
I = int(O);
display('Integral')
pretty(simplify(I)) %Makes it nice
%Now, to graph them
B = -10:1:10; %Bounds of the graph
plot(B,O,'k--',B,D,'g--',B,I,'r--');
xlabel('x')
ylabel('y')
title('Original, Derivative, Integral')
grid on %Graph Grid on
  댓글 수: 1
Star Strider
Star Strider 2016년 5월 20일
A few observations:
  • ‘O’ could be improved upon as a function name, since you could confuse it with zero (0),
  • ‘D = diff(O);’ will leave you one element short, so use the gradient function instead,
  • ‘I = int(O);’ will probably try to invoke the Symbolic Math Toolbox integration and will of course fail, so use trapz or cumtrapz instead.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by