Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do until loop logic in R

I have a function fk, I have another function calc where I pass 2 values x and y. I wanted to do the following, until fk(x)*fk(y) <0, I want to double the interval (y-x). How would I accomplish this in R. I'm struggling on logic, any help would be greatly appreciated,

fk <- function(x) {
    return((x*x)-(3*x)+4)
}

calc <- function(x,y) {  
    g <- fk(x)*fk(y)

    until g < 0 do 
    double the interval (y-x)
}
like image 654
forecaster Avatar asked Aug 16 '26 13:08

forecaster


1 Answers

You want to use something along these lines:

calc <- function(x,y) { 
    while (TRUE) {
        g <- fk(x)*fk(y)
        if (g < 0) {
            break
        }
        // otherwise double interval (y-x)
    }
}

As I mentioned in my comment, it is not clear how you want to adjust the values for x and y in order to "double" the interval. Once you figure that out, you can add the required code to the loop.

like image 141
Tim Biegeleisen Avatar answered Aug 18 '26 02:08

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!