Ishaan Sengupta
Ishaan Sengupta

Reputation: 1

How to plot a function with plot() in R with a step size of 0.5?

New to R. I'm given a function f(x)=x^3-3x+7. I need to plot this function in red and plot its derivative with a step-size of 0.5 in blue. dfx vs x in the same graph. I have plotted f(x), but i cant plot dfx with adjusted step size. My code:

f1 <- function(x) {x^3-3*x+7}
exp = D(expression(x^3-3*x+7),'x')
f2 <- function(x) {D(exp,'x')}
curve(f1,from=-2,to=2,col='red')
curve(exp,col='blue',add=TRUE,type='p')

I need the points to be plotted at an interval of 0.5 and also draw a line to connect them

Upvotes: 0

Views: 231

Answers (1)

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21440

I'm not 100% sure what you need. If by changing the step size you mean that both curves be entirely visible in the same plot, that can be done by increasing the limits on the y-axis:

curve(f1, from = -2, to = 2, col='red', add = F, ylim = c(0,10))
curve(exp, col = 'blue', add = T, type = 'p')

enter image description here

Upvotes: 1

Related Questions