how to find roots of a multivariate polynomial with only one variable - sage

Say I have a multivariate polynomial ring:
R.<w,x,y,z> = PolynomialRing(ZZ, 4, order='lex')
and a polynomial contains only one variable, for example:
f = w^4 - 1
How can I find the roots of f. Thx.

sage: f.univariate_polynomial().roots()
[(1, 1), (-1, 1)]

Related

Given an x and polynomial equation, is there way to get the y value using r?

If I have a equation like 10 + x^2 + x^3 + x^4 = y and an x value like 2. Is there way to plug this into r so it would solve for y? It sounds trivial but eventually I would like to solve for x using polynomials that higher degrees like 30. Anyone know of a possible way to do this in r but without plugging in the x value manually?
Please note: I'm trying to solve for y given a specific x value.
You can easily write your own function:
p <- function(x, coefs) c(cbind(1, poly(x, degree = length(coefs) - 1,
raw = TRUE, simple = TRUE)) %*% coefs)
p(2, c(10, 0, 1, 1, 1))
#[1] 38
Use rep if you need many coefficients of 1.

Calculate alpha parameter for beta in R

How can I calculate the alpha value for P(x>a)=0.1 using R commands for a given Beta(3,2)?
I know there are pbeta, qbeta but none of them fits the problem as far as I know...
Note that a <- qbeta(p, 3, 2) solves P(x < a) = p. Then, note that P(x >= a) = 1 - P(x<a). So, you need to calculate a <- qbeta(1 - p, 3, 2)

Using interpolation to derive a function in R

I'm trying to derive an approximated function from some X and Y values in R.
As I understand it splines can be used, but I just can't grasp how through the documentation.
Heres what I'd like to do:
x <- c(0, 1, 2, 3, 4, 5)
y <- c(200, 320, 455, 612, 899)
## Example of goal
approxfun <- findfun(x,y, pow=5)
approxfun
Returning a result of: f(x) = y = ax^5 + bx^4 + cx^3 + dx^2 + e*x^1 + f.
Where a, b, c, d, e, and f are some real numbers.
The core issue I'm trying to tackle is solving an equation in the form of sum(f(x)),n=1->N = max. IE. I'm trying to find the N that allows for the maximum amount of accumulated function sums of f(x). In other words, if i have 100 apples and eat increasing amounts each day as they being to turn over-ripe, and the amount i eat each day is f(x), I need to know how many days the apples will last.

Multivariate polynomial coefficients including zeros

I would like to get the coefficients of a multivariate polynomial including the zero coefficients (in their correct positions). I have found a similar answer as regards a polynomial f of two variables x,y
Now, I would like to extend this solution for a multivariate polynomial of n variables [xo,x1,...xn-1]
The polynomial is defined with the following code: (Note: q=next_prime(10000))
A1 = [(', '.join('x%i'%i for i in [0.. n-1]))]; ### construct a suitable multivariate ring
V = var(A1[0]) ### define a str variable
x=vector(list(V)) ### convert to vector
P1=PolynomialRing(GF(q),V)
How can I do that?
As a start, given a polynomial g, you can do P1.monomial_all_divisors(lcm(g.monomials())) to get a list of the relevant monomials. You may want to sort that list – I can't tell what order it is in by default – but then you can do this:
sage: P1.<x0, x1, x2> = PolynomialRing(GF(7)) # my simple setup
sage: g = 3*x0*x1 - x1^2 + 2*x1*x2
sage: [(m, g.monomial_coefficient(m)) for m in P1.monomial_all_divisors(lcm(g.monomials()))]
[(x0, 0),
(x1, 0),
(x0*x1, 3),
(x1^2, 6),
(x0*x1^2, 0),
(x2, 0),
(x0*x2, 0),
(x1*x2, 2),
(x0*x1*x2, 0),
(x1^2*x2, 0),
(x0*x1^2*x2, 0)]

Solving mixed linear and differential systems of equations with R [duplicate]

I've got a bit of a weird set of conditions I need to fit a curve to. I've tried looking it up elsewhere but I'm not even sure I'm using the right lingo. Any help is much appreciated.
I'm trying to fit a polynomial curve to a set of four points. Three of the points are known, but the fourth one is a little tricky. I have the x value for the maximum y value, but I don't know what the maximum y value is. For example, let's say there are known points at (0,0), (1,1), and (4,0). The maximum y value is at x=3 so the fourth point is (3, ymax). How would I fit a 4th order polynomial curve to those conditions? Thanks in advance.
Actually it is possible since you require the y value at x=3 should be maximum. So, a degree 4 polynomial has 5 coefficients to be determined and you have the following equations:
y(0) = 0
y(1) = 1
y(4) = 0
dy/dx(3) = 0 (first derivative at x=3 should be 0)
d2y/dx2(3) < 0 (2nd derivative at x=3 should be negative)
So, pick any negative value for d2y/dx2 at x=3 and solve the 5 linear equations and you will get one degree 4 polynomial. Note that the y value at x=3 obtained this way is only a local maximum, not a global maximum.
Filling in the algebra from #fang's answer (a little elementary calculus, some algebra, and some linear algebra):
y = a+b*x+c*x^2+d*x^3+e*x^4
y(0) = 0 -> a=0
Set a=0 for the rest of the computations.
y(1) = 1 -> b+c+d+e = 1
y(4) = 0 -> 4*b+16*c+64*d+256*e=0
dy/dx(3)=0 ->
b+2*x*c+3*x^2*d+4*x^3*e=0 ->
b+6*c+27*d+108*e=0
d2y/dx2(3)<0 = 2*c+6*d*x+12*e*x^2 < 0
= 2*c+18*d+108*e < 0
Pick a negative value V for the last expression, say -1:
V <- -1
A <- matrix(c(1, 1, 1, 1,
4,16,64,256,
1, 6,27,108,
0, 2,18,108),
ncol=4,byrow=TRUE)
b <- c(1,0,0,V)
(p <- solve(A,b))
## [1] 2.6400000 -2.4200000 0.8933333 -0.1133333
x <- seq(-0.5,5,length=101)
m <- model.matrix(~poly(x,degree=4,raw=TRUE))
y <- m %*% c(0,p)
Plot results:
par(las=1,bty="l")
plot(x,y,type="l")
points(c(0,1,4),c(0,1,0))
abline(v=3,lty=2)
Picking a larger magnitude (more negative) value for V will make the solution more sharply peaked at y=3.

Resources