Trouble With Graphing Functions

1 次查看(过去 30 天)
Josh Glenn
Josh Glenn 2016-5-19
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