How to solve a cubic function in R - r

Good day to all!
I have a following cubic equation.
Left <- P^3+4*P^2+6*P
Right <- 2
How do I get R to solve for P to get Left = Right?
Thanks in advance.

1. uniroot()
You could use uniroot() to search for a root of a function with respect to its first argument.
uniroot(\(x, y) x^3 + 4*x^2 + 6*x - y, c(0, 1), y = 2, extendInt = "yes")
$root
[1] 0.278161
$f.root
[1] -1.779565e-05
$iter
[1] 6
$init.it
[1] NA
$estim.prec
[1] 6.103516e-05
2. polyroot()
If the function is a real or complex polynomial, you could specifically use polyroot(z), where z is the vector of polynomial coefficients in increasing order.
y <- 2
polyroot(c(-y, 6, 4, 1))
# [1] 0.2781631-0.000000i -2.1390815+1.616897i -2.1390815-1.616897i
Both approaches solve the equation with the root 0.278161. (Besides a real root, polyroot also gives two imaginary roots)

If you want symbolic solutions, I guess you can try Ryacas like below
> library(Ryacas)
> yac_str("Solve(P^3+4*P^2+6*P==2,P)")
[1] "{P==(71/27+Sqrt(187/27))^(1/3)-(Sqrt(187/27)-71/27)^(1/3)-4/3,P==Complex(-(4/3+((71/27+Sqrt(187/27))^(1/3)-(Sqrt(187/27)-71/27)^(1/3))/2),Sqrt(3/4)*((71/27+Sqrt(187/27))^(1/3)+(Sqrt(187/27)-71/27)^(1/3))),P==Complex(-(4/3+((71/27+Sqrt(187/27))^(1/3)-(Sqrt(187/27)-71/27)^(1/3))/2),-Sqrt(3/4)*((71/27+Sqrt(187/27))^(1/3)+(Sqrt(187/27)-71/27)^(1/3)))}"

Related

R - which of these functions is giving the correct value of the integrals?

I am trying to compute this integral in R:
I found three functions which can be used for this and they are all giving me different results. Here is the code:
integrand <- function(x){
r <- 1/x
return(r)
}
First is the option from base R:
integrate(integrand,-Inf, Inf)
Giving the result:
0 with absolute error < 0
The second is from the pracma package:
quadinf(integrand, -Inf, Inf)
Giving this output:
$Q
[1] -106.227
$relerr
[1] 108.0135
$niter
[1] 7
And the last one is from the cubature package:
cubintegrate(integrand, -Inf, Inf)
Which gives the following result:
$integral
[1] Inf
$error
[1] NaN
$neval
[1] 15
$returnCode
[1] 0
So then, which one of these is correct and which should I trust? Is it 0, infinity, or -106.227? Why are they all different in the first place?
1/x isn't integrable in [-Inf,Inf] range, because not integrable in 0.
On an integrable range, results are similar:
integrate(\(x) 1/x,1,2)
#0.6931472 with absolute error < 7.7e-15
pracma::quadinf( \(x) 1/x,1,2)
#$Q
#[1] 0.6931472
#$relerr
#[1] 7.993606e-15
#$niter
#[1] 4
Note that integral of 1/x in ]0,Inf] range is log(x):
log(2)-log(1)
#[1] 0.6931472

How do I optimize a function in R for all values of y?

So I'm trying to plot a function into a graph and ultimately I landed on the easiest option being to give alpha-values and optimize velocity for the x_position values. The problem is, I'm doing something wrong with the optimization.
Here's what I've got thus far:
y <- seq(0, 55, 0.1)
x_position <- function(alpha,velo)
{velo*cos(alpha)*((velo*sin(alpha)+sqrt((velo^2*sin(alpha))^2+2*16.5*9.81)/9.81))}
x <- optimize(x_position,c(1,1000),alpha=y,maximum=TRUE)$objective
Basically, I'm trying to make "y" a vector for the angle and "x" a vector of maximum function value for each angle value so that I could then plot the x,y vector for the function. The problem is, I can't get the x-vector right. For whatever reason it just keeps telling me "invalid function value in 'optimize'". Changing the optimization interval doesn't seem to accomplish anything and I'm out of ideas. The function seems to work just fine when I tested it with e.g. alpha 55 and velocity 10.
y <- seq(0, 55, 0.1)
x_position <- function(velo,alpha){
velo*cos(alpha)*((velo*sin(alpha)+sqrt((velo^2*sin(alpha))^2+2*16.5*9.81)/9.81))
}
optimize(f = x_position, interval = c(1,1000), maximum=TRUE, alpha = y[1])
#> $maximum
#> [1] 999.9999
#>
#> $objective
#> [1] 1834.098
a <- sapply(y, function(y) optimize(f = x_position, interval = c(1,1000),
maximum=TRUE, alpha = y)$objective)
head(a)
#> [1] 1834.098 10225190.493 20042734.667 29061238.316 36921162.118
#> [6] 43309155.705
Created on 2021-09-29 by the reprex package (v2.0.1)

It is possible to solve equation R that are not linear?

I want to build a function that takes E[x] and Var[X] and give me the mean and standard error of a univariate lognormal variable.
E[x] = exp(mu + theta)
Var[x] = exp(2*mu + theta)*(exp(theta) - 1)
The function would take E[x] and Var[x] as input and as output would give me theta and mu
There are several packages that provide ways and means to solve a system of nonlinear equations. One of these is nleqslv.
You nee to provide a function that function that returns the differences between the actual value of the equations and the desired value.
Load package nleqslv and define the following function
library(nleqslv)
f <- function(x,Ex,Varx) {
y<- numeric(length(x))
mu <- x[1]
theta <- x[2]
y[1] <- exp(mu+theta) - Ex
y[2] <- exp(2*mu+theta)*(exp(theta)-1) - Varx
y
}
The vector x in the function contains the values of mu and theta.
An example with Ex=2 and Varx=3 and some random starting values
xstart <- c(1,1)
nleqslv(xstart,f,Ex=2,Varx=3)
gives the following
$x
[1] -0.6931472 1.3862944
$fvec
[1] -8.095125e-11 -8.111645e-11
$termcd
[1] 1
$message
[1] "Function criterion near zero"
$scalex
[1] 1 1
$nfcnt
[1] 31
$njcnt
[1] 2
$iter
[1] 22
See the manual of nleqslv for the meaning of the different elements of the return value of nleqslv.
If you want to investigate the effect of the different solving methods try this
testnslv(xstart,f,Ex=2,Varx=3)

Find intersection between a beta and a normal distribution

I have 2 distributions - 1 beta and 1 normal and I need to find the intersection of their pdfs. I know the parameters for both and am able to visually see the intersection, but am looking for a way for R to calculate the exact point. Anybody have an idea of how to do this?
Use uniroot().
uniroot(function(x) dbeta(x, 1, 2)-dnorm(x, 0, 1), c(0, 1))
## $root
## [1] 0.862456
##
## $f.root
## [1] 5.220165e-05
##
## $iter
## [1] 3
##
## $estim.prec
## [1] 6.103516e-05
This solves an equation dbeta(x, ...) == dnorm(x, ...) w.r.t. x (in the inverval [0,1], as this is the support of a beta distribution), i.e. finds the root of dbeta(x, ...) - dnorm(x, ...). The resulting list's root field gives you the answer (more or less precisely).

Multiple roots in the complex plane with R

I've been trying to find a function that returns all complex solutions of an equation such as:
16^(1/4) = 2+i0, -2+i0, 0+i2, 0-i2
As it stands, if I enter 16^(1/4) into the console, it only returns 2. I can write a function for this but I was wondering if there is a simple way to do this in R.
You need polyroot():
polyroot(z = c(-16,0,0,0,1))
# [1] 0+2i -2-0i 0-2i 2+0i
Where z is a "vector of polynomial coefficients in increasing order".
The vector I passed to z in the example above is a compact representation of this equation:
-16x^0 + 0x^1 + 0x^2 + 0x^3 + 1x^4 = 0
x^4 - 16 = 0
x^4 = 16
x = 16^(1/4)
Edit:
If polyroot's syntax bothers you, you just could write a wrapper function that presents you with a nicer (if less versatile) interface:
nRoot <- function(x, root) {
polyroot(c(-x, rep(0, root-1), 1))
}
nRoot(16, 4)
# [1] 0+2i -2-0i 0-2i 2+0i
nRoot(16, 8)
# [1] 1.000000+1.000000i -1.000000+1.000000i -1.000000-1.000000i
# [4] 1.000000-1.000000i 0.000000+1.414214i -1.414214-0.000000i
# [7] 0.000000-1.414214i 1.414214+0.000000i

Resources