Hey there, I want to plot 5 parabola functions, which happen to be f(x) = 0.25x? + 6,47x -32.6 g(x)=0.99x? -6x -195 j(x)= 0.77x? +14x -495 k(x)=0.001x? + 65x -785 l(x) = 0.9x? -2x -636 in the same graph. Sadly I even do not really understand how to plot just one graph... I found this code in the Internet, which plots a cos-function:>x <- seq( -10, 10, length = 1000) >plot(x, *sin(x)*, xlab="x-values", ylab="f(x)", type="l") >lines(x, cos(x), lty=3) >title( "Trigonometric functions", "sin(x) and cos(x)")If I replace the sin(x) thing (bold) with one of my functions, it doesn't work and reports an unwanted character. Can somebody help me with that? -- View this message in context: http://r.789695.n4.nabble.com/Simple-Problem-Plotting-mathematical-functions-tp4552668p4552668.html Sent from the R help mailing list archive at Nabble.com.
Hi, At the moment I am studying R at school. But I found this site very useful to explain the plot functions to me: http://www.harding.edu/fmccown/r/ Maybe that can help you too? -- View this message in context: http://r.789695.n4.nabble.com/Simple-Problem-Plotting-mathematical-functions-tp4552668p4552744.html Sent from the R help mailing list archive at Nabble.com.
Okay, i got this far: f <- function(x) 0.25*x^2 + 6.47*x -32.6 g <- function(x) 0.99*x^2 -6*x -195 h <- function(x) 0.77*x^2 +14*x -495 j <- function(x) 0.001*x^2 + 65*x -785 k <- function(x) 0.9*x^2 -2*x -636 plot(x, f(x), xlab="Elemente in der Reihung", ylab="Indexwert des Sortieraufwands"), type="l") // lines(x, g(x), lty=3) //Not sure if it works, but this is irrelevant atm. As soon as R does the plot command, he states that x is an object he can't find. So i propably have to do something like x <- XXXXXXXXXX. What do I insert for the XXXXXXXXX to make it have the values - say - from 15 to 10000? Thanks again. -- View this message in context: http://r.789695.n4.nabble.com/Simple-Problem-Plotting-mathematical-functions-tp4552668p4552894.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2012-Apr-12 19:59 UTC
[R] Simple Problem: Plotting mathematical functions
It seems that your first problem is syntax: 2x will thrown an error, while 2*x won't. Google around for a good intro tutorial (there's the main one you can access by typing help.start() and it's quite good) and these sorts of things will be explained. You might also want to use the curve function (type ?curve at the prompt for details) e.g., curve(x^2, from = 0, to = 5) plots x^2 from x = 0 to x = 5 and handles axes nicely. Michael On Thu, Apr 12, 2012 at 2:29 PM, Aye <muudle at hotmail.de> wrote:> Hey there, > I want to plot 5 parabola functions, which happen to be > f(x) = 0.25x? + 6,47x -32.6 > g(x)=0.99x? -6x -195 > j(x)= 0.77x? +14x -495 > k(x)=0.001x? + 65x -785 > l(x) = 0.9x? -2x -636 > > in the same graph. Sadly I even do not really understand how to plot just > one graph... > I found this code in the Internet, which plots a cos-function: > >>x <- seq( -10, 10, length = 1000) >>plot(x, *sin(x)*, xlab="x-values", ylab="f(x)", type="l") >>lines(x, cos(x), lty=3) >>title( "Trigonometric functions", "sin(x) and cos(x)") > > If I replace the sin(x) thing (bold) with one of my functions, it doesn't > work and reports an unwanted character. > > Can somebody help me with that? > > -- > View this message in context: http://r.789695.n4.nabble.com/Simple-Problem-Plotting-mathematical-functions-tp4552668p4552668.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi, you can use the curve() function for this. It is dedicated to plotting functions. Here is an example # turn your functions into r functions f <- function(x) 0.25 * x^2 + 6.47 * x -32.6 g <- function(x) 0.99*x^2 -6*x -195 j <- function(x) 0.77*x^2 + 14*x -495 k <- function(x) 0.001*x^2 + 65*x -785 l <- function(x) 0.9*x^2 -2*x -636 # plot f and g curve(f, from=0, to=100, lty=1) curve(g, add=TRUE, lty=2) In this example, the parameters 'from' and 'to' define the domain over wich all functions will be evaluated. The parameter add=TRUE is used to add the second function to the previous plot. HTH Matthieu Matthieu Dubois Post-doctoral fellow, Psychology Department, Univerist? Libre de Bruxelles
Apparently Analagous Threads
- get means of elements of 5 matrices in a list
- Mathematical Program
- Mathematical annotation axis in lattice
- Is R good for not-professional-statistician, , un-mathematical clinical researchers?
- what is Non-numeric argument to mathematical function in prediction ?