[R] Plotting one dot in a graph

TGS cran.questions at gmail.com
Thu Aug 12 22:58:01 CEST 2010


# just to clean it up for my own understanding, the "difference" approach as you had suggested would be

x <- seq(.2, .3, by = .00001)
f1 <- function(x){
	x*cos(x)-2*x**2+3*x-1
}
plot(x,f1(x), type = "l")
abline(h = -.1)
abline(v = x[which.min(abs(diff((f1(x) - (-.1))**2)))], lty = 'dotted')
points(x = x[which.min(abs(diff((f1(x) - (-.1))**2)))], y = -.1)

# and the uniroot approach is:

x <- seq(.2, .3, by = .01)
f1 <- function(x){
	x*cos(x)-2*x**2+3*x-1
}
f2 <- function(x){
	-.1
}
f3 <- function(x){
	f1(x) - f2(x)
}
plot(x,f1(x), type = "l")
abline(h = -.1)
abline(v = uniroot(f = f3, interval = c(.2, .3))$root, lty = 'dotted')
points(x = uniroot(f = f3, interval = c(.2, .3))$root, y = -.1)

# Thanks David!


On Aug 12, 2010, at 1:33 PM, David Winsemius wrote:


On Aug 12, 2010, at 4:15 PM, TGS wrote:

> David, I was expecting this to work but how would I specify the vector in "diff()" in order for the following to work?
> 
> x <- seq(.2, .3, by = .01)
> f <- function(x){
> 	x*cos(x)-2*x**2+3*x-1
> }
> plot(x,f(x), type = "l")
> abline(h = -.1)
> abline(v = x[which.min(abs(diff(c(-.1, f(x)))))], lty = 'dotted')

f2 <- function(x) -0.1
f3 <- function(x) f(x) -f2(x)
abline(v=uniroot(f3, c(0.2, 0.3) )$root)
points(x=uniroot(f3, c(0.2, 0.3) )$root, y= -0.1)

If you are going to use the differences, then you probably want to minimize either the abs() or the square of the differences.

-- 
David.
> 
> On Aug 12, 2010, at 1:00 PM, David Winsemius wrote:
> 
> 
> On Aug 12, 2010, at 3:54 PM, TGS wrote:
> 
>> Actually I spoke too soon David.
>> 
>> I'm looking for a function that will either tell me which point is the intersection so that I'd be able to plot a point there.
>> 
>> Or, if I have to solve for the roots in the ways which were demonstrated yesterday, then would I be able to specify what the horizontal line is, for instance in the case where y (is-not) 0?
> 
> Isn't the abline h=0 represented mathematically by the equation y=0 and therefore you are solving just for the zeros of "f" (whaich are the same as for (f-0)? If it were something more interesting, like solving the intersection of two polynomials, you would be solving for the  zeros of the difference of the equations. Or maybe I have not understood what you were requesting?
> 
> 
>> 
>> On Aug 12, 2010, at 12:47 PM, David Winsemius wrote:
>> 
>> 
>> On Aug 12, 2010, at 3:43 PM, TGS wrote:
>> 
>>> I'd like to plot a point at the intersection of these two curves. Thanks
>>> 
>>> x <- seq(.2, .3, by = .01)
>>> f <- function(x){
>>> 	x*cos(x)-2*x**2+3*x-1
>>> }
>>> 
>>> plot(x,f(x), type = "l")
>>> abline(h = 0)
>> 
>> Would this just be the uniroot strategy applied to "f"? You then plot the x and y values with points()
>> 
> 
>> 
> 
> David Winsemius, MD
> West Hartford, CT
> 
> 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list