Plotting a function in the wolfram-alpha-website looks like this:
http://www.wolframalpha.com/link
Plotting the same function in R looks like this:
plot( function(x) x^2 - 3*x - 10 )
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?
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)
# 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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With