Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot a polynomial function like on the wolframalpha-website so that it is easy to understand

Plotting a function in the wolfram-alpha-website looks like this:

enter image description here

http://www.wolframalpha.com/link

Plotting the same function in R looks like this:

plot( function(x) x^2 - 3*x - 10 ) enter image description here

The default plot from Wolfram is much easier to understand. I think this is because it shows the x-axis (at y=0), and centers the parabola.

I am not good enough at math to just look at the formula of a function and see where I should center the plot, and I am plotting the functions to learn about how different functions create different lines, so I need this centering to be done automatically, because otherwise I might misunderstand a plot.

Is it possible to create the Wolfram-plot automatically i.e. without me telling R where it would be sensible to center the plot?

like image 781
Rasmus Larsen Avatar asked Nov 27 '13 19:11

Rasmus Larsen


2 Answers

The polynom package will create some sensible defaults.

eg.

library(polynom)

# your polynomial (coefficients in ascending powers of x order)
p <- polynomial(c(-10,-3,1))
plot(p)

enter image description here

 # a more complicated example, a polynomial crossing the x axis at -1,0,1,2,3,4,5

 p2 <- poly.calc(-1:5)
 p2 
 # -120*x + 154*x^2 + 49*x^3 - 140*x^4 + 70*x^5 - 14*x^6 + x^7 
 plot(p2)

enter image description here

like image 106
mnel Avatar answered Sep 29 '22 16:09

mnel


You can set the desired interval to plot over, as described in ?plot.function. Also see curve and abline.

plot( function(x) x^2 - 3*x - 10 , -15, 15) ; abline(h=0,v=0,lty=3)

or

curve(x^2 - 3*x - 10 , -15, 15) ; abline(h=0,v=0,lty=3)
like image 39
Aaron left Stack Overflow Avatar answered Sep 29 '22 15:09

Aaron left Stack Overflow