0
votes

Do you know if there is any R package or function could help solve "min y=x^3 - 3*x^2 + 4" like the following SAS code?

proc optmodel;      
   var x;
   min y=x**3 - 3*x**2 + 4;
 solve;
 print x;
 quit;

PROC OPTMODEL is documented here: http://support.sas.com/documentation/cdl/en/ormpug/67517/HTML/default/viewer.htm#ormpug_optmodel_toc.htm

I am looking for the value of x that minimizes y. Thanks in advance!

1
Is optimize(function(x)x^3 - 3*x^2 + 4, interval=c(0, 5)) not good enough?Khashaa
no... x = -inf for min(y), but I cant get it using any R function.Baader Meinhof
I wonder what would your desired output be like, since y doesn't have min on that interval.Khashaa
hm in this case I would want R tell me that.Baader Meinhof
The output from the solver in SAS (assuming you turn on multi-start in attempt to get out of the local min at (x,y) (2,0)) will be (-5,957,051 , -2.113947e20). The iteration after that in the solver pushes y past DBL_MIN, the solver stops are reports the problem as unbounded.DomPazz

1 Answers

0
votes

You are trying to find the roots of a polynomial. The R function to do this is polyroot(). The argument to polyroot() is a vector of polynomial coefficients, arranged in increasing order. Try this:

polyroot(c(4, 0, -3, 1))
[1] -1+0i  2-0i  2+0i

Now you can use this information to visualuse the polynomial, by setting the plot range of curve():

curve(x^3 - 3*x^2 + 4, from=-2, to=3, col="blue")
abline(h=0)

enter image description here