Fast and Sloppy Root Finding

Saturday, December 4, 2010 at 3:21 PM Bookmark and Share
Disclaimer: While the approach to root finding mentioned below is both slow and imprecise, it's also a cheap and incredibly handy approach when all you need to do is get "close enough". If you like R quick and dirty (hey now, get your mind out of the gutter...) this is totally the root finding method for you!

I just read a post on Root Finding (original here) by way of R-bloggers.com which was timely given that only yesterday I'd needed to do some root finding in R to make a figure for a manuscript -- something like the following image.
The blog post prompted me to mention here how I did my root finding for two reasons:
  1. Precision and computation time sometimes don't matter all that much; and
  2. The way I did my root finding was way easier to implement than convergence-based methods described in the post above.
So here's what I was aiming for, and how I implemented it in R.

The Task: Suppose you're plotting two curves (say, y=f(x) and y=g(x)) and would like to indicate their intersection with an empty circle (i.e. pch=21). In my case, the intersection of these two curves was equilibrium point for a dynamic model, and I wanted to indicate it as such.

If you can find their intersection mathematically (i.e. set f(x)=g(x) and solve for x) then awesome -- do that if you can.  But if for some reason you can't, and you know a single root exists in some interval a≤x≤b, you can find that root quickly using some straightforward vector tricks.

The Solution: Lets use the example of finding the intersection of f(x) = x/(1+x) and g(x) = (5-x)/5 over the interval (0,5).

Step 1:  Define an x vector full of values between a and b.  The smaller the step size (or, the longer the list) the better.
> x = seq(0, 5, length=5000);
Step 2: Compute the square of the difference of your two functions over that interval using x.  This is as simple as the line of code...
> fgdiff = ( x/(1+x) - (5-x)/5 )^2;
Step 3: Using the which() function, we can pick out the index for the smallest value in our list of squared differences... Once we know this index (call it j) we know the intersection occurs at, or very near, the x value x[j], and we're basically done! 
> j = which(fgdiff==min(fgdiff))[1]; 
> j; x[j];  ## show the value of j, x[j]
For a closer look at what's going on with that which() statement, check out the help for which() and following example.
## A Closer look at the which(fgdiff==min(fgdiff))
> ?which
> which(c(F,F,T,T,F))
[1] 3 4
> which(c(F,F,T,T,F))[1]
[1] 3
> xample = c(5:1,2:5); xample
[1] 5 4 3 2 1 2 3 4 5
> min(xample)
[1] 1
> xample==min(xample)
[1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
> which(xample==min(xample))
[1] 5
> which(xample==min(xample))[1]
[1] 5
Step 4: Since both functions are (approximately) equal at this x value, it only remains to decide whether you want to indicate the point of intersection using (x, f(x)) or (x, g(x)).
> points(x[j], x[j]/(1+x[j]), pch=19, cex=2)
All done!

If you'd like to tinker with this example, here's the code to produce the image above.
win.graph(5,4)   ## Open a new window, 5in x 4in.

## Plot the two functions
curve(x/(1+x), from=0, to=5, lwd=3, ylab="f(x), g(x)", 
                             ylim=c(0,1), cex.lab=1.5)
curve((5-x)/5, from=0, to=5, lwd=3, lty=2, add=T)

## Find their intersection the quick n' dirty way
x=seq(0,5,length=1000)
fgdiff = (x/(1+x) - (5-x)/5)^2;
j = which(fgdiff==min(fgdiff))[1]; # Only 1st, in case of 2 mins
j; x[j]

## Plot an empty circle at the intersection, and label curves.
points(x[j], x[j]/(1+x[j]), pch=21, col="black", bg="white", lwd=3, cex=1.7)
text(4.5, 0.65,  expression(f(x)==frac(x,1+x)), cex=1)
text(3,   0.15, expression(g(x)==frac(5-x,5)), cex=1)

8 comments:

Posted by: lilnesscapades | 12/09/2010 8:39 AM

yay! fun stuffs! = ]

Posted by: Anonymous | 4/30/2021 4:10 PM

Five weeks ago my boyfriend broke up with me. It all started when i went to summer camp i was trying to contact him but it was not going through. So when I came back from camp I saw him with a young lady kissing in his bed room, I was frustrated and it gave me a sleepless night. I thought he will come back to apologies but he didn't come for almost three week i was really hurt but i thank Dr.Azuka for all he did i met Dr.Azuka during my search at the internet i decided to contact him on his email dr.azukasolutionhome@gmail.com he brought my boyfriend back to me just within 48 hours i am really happy. What’s app contact : +44 7520 636249‬

Posted by: zoey | 6/17/2021 2:25 AM

While the method for root discovery described here is slow and imprecise, it is also
coursework uk
inexpensive and extremely useful when all you need to do is get "near enough."

Posted by: EURO-RITE kitchen Builder | 6/30/2021 10:38 AM

Typically, carpet needs to be replaced every 10 years or so. Harder surfaces such as wood, tile, and vinyl usually last at least 20 – 25 years. This all depends on your exact circumstances, though. To ensure that your new flooring lasts as long as you intend it to, make sure to purchase from a high-quality manufacturer and go through all the proper steps for maintenance.

Posted by: waterproof foundation edmonton | 6/30/2021 2:09 PM

Ramma foundation is a company and a foundation that provide crack repair snow remove and gutter clean service. We are the crack repair foundation in Alberta Edmonton. We provide services like crack repair, gutter clean services, show removal, in short we are the foundation repair in Edmonton. If you are worried about the cracks on your home do not worry we are here to get it repair. If you are worried about snow on your roof, we can remove it. We are here to clean you gutter.

Posted by: Unknown | 9/12/2021 10:01 AM

Thankfully, we’re here to help make sure your jacket/coat is on point every time you walk out the door. Here are the best of our chic leather jacket for women with different colors, available at topcelebsjackets.com.

Posted by: alexander peres | 10/10/2022 8:05 PM

My husband was cheating on me and no longer committed to me and our kids, when i asked him what the problem was he told me he has fell out of love for me and wanted a divorce.i was so heart broken i cried all day and night but he left home, i was looking for something online when i saw an article how the great and powerful dr ovie have helped so many people in similar situation like mine, his email address was written so i sent him an email telling him about my problem,he told me he shall return back to me within 24hrs, i did everything he asked me to do the next day to my greatest surprise, my husband came back home and was crying and begging for me to forgive and accept him back,now we are happy together if you need his help contact him email:droviespelltemple@gmail.com or WhatsApp him +2349056460552

Posted by: emir | 9/05/2023 11:55 PM

https://saglamproxy.com
metin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
EJD3J

Post a Comment