|
This post was updated on .
Never mind, thanks for your help, I got it. Yay
![]() 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? |
|
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? |
|
In reply to this post by Aye
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. ______________________________________________ [hidden email] 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. |
|
In reply to this post by Aye
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 <[hidden email]> 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. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
In reply to this post by Aye
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 ______________________________________________ [hidden email] 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. |
|
In reply to this post by Aye
On Apr 12, 2012, at 3:49 PM, Aye wrote: > 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? x <- seq(15, 1000, by=5) > > 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. > > ______________________________________________ > [hidden email] 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. David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] 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. |
|
On 04/12/2012 09:11 PM, David Winsemius wrote:
> > On Apr 12, 2012, at 3:49 PM, Aye wrote: > >> 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? > > x <- seq(15, 1000, by=5) > > >> >> 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. >> >> ______________________________________________ >> [hidden email] 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. > > David Winsemius, MD > West Hartford, CT > > ______________________________________________ > [hidden email] 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. You can certainly use "plot" but you could also use "curve": > curve(0.25*x^2 + 6.47*x -32.6,0,10) > f <- function(x) 0.25*x^2 + 6.47*x -32.6 > curve(f) > curve(f,0,100) Rick -- --- Richard A. Bilonick, PhD Assistant Professor 412 647 5756 Dept. of Ophthalmology, School of Medicine Dept. of Biostatistics, Graduate School of Public Health Principal Investigator: Pittsburgh Aerosol Research and Inhalation Epidemiology Study (PARIES) University of Pittsburgh ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
