Ptit_Bleu
2007-Nov-06 08:13 UTC
[R] How to find the zero (only the real solution) with the package polynom ?
Hello, I have 3 columns : a, b and a*b I would like to find the pair (a,b) so that a*b is the minimum but not from the points I measured but from the fit of the curve (I have more points that the ones given below but I fit only on this part because I know that the minimum a*b is in this interval). I thought doing it this way : - to fit a*b=f(a) abfit<-lm(ab ~ poly(a,8,raw=T)) - to use the package polynom to make the polynom library(polynom) polynomial(abfit$coefficients) 0.03537506 - 0.8506516*x + 8.302545*x^2 - 44.80418*x^3 + 144.2161*x^4 - 283.2458*x^5 + 331.1776*x^6 - 210.5144*x^7 + 55.86646*x^8 - to derive the polynom deriv(polynomial(pfit$coefficients)) -0.8506516 + 16.60509*x - 134.4125*x^2 + 576.8643*x^3 - 1416.229*x^4 + 1987.066*x^5 - 1473.601*x^6 + 446.9317*x^7 - to find the zero solve(deriv(polynomial(pfit$coefficients))) [1] 0.1749897+0.0568886i 0.1749897-0.0568886i 0.3742571-0.1235393i 0.3742571+0.1235393i 0.4778418+0.0000000i 0.8604070+0.1306799i 0.8604070-0.1306799i And here is my little problem : I don't want the complex solutions, only the real one (0.4778418). I also tried uniroot but I got an error message : uniroot(deriv(polynomial(pfit$coefficients)), c(0.8,0.2)) Error in uniroot(deriv(polynomial(abfit$coefficients)), c(0.8, 0.2)) : Impossible to find the function "f" Is there a way to extract this real solution ? I thank you in advance for your help, Have a nice day, Ptit Bleu. ------------------------------------------------- Set of points a b a*b 0.8 0.033320357 0.026656286 0.76 0.023930636 0.018187283 0.72 0.0153903 0.011081016 0.68 0.007844914 0.005334542 0.64 0.001598132 0.001022804 0.6 -0.00315939 -0.001895634 0.56 -0.006402796 -0.003585566 0.52 -0.008379886 -0.004357541 0.48 -0.009528793 -0.004573821 0.44 -0.010198446 -0.004487316 0.4 -0.010638282 -0.004255313 0.36 -0.010937536 -0.003937513 0.32 -0.011169004 -0.003574081 0.28 -0.011343229 -0.003176104 0.24 -0.0114878 -0.002757072 0.2 -0.011630636 -0.002326127 0.16 -0.011724586 -0.001875934 -- View this message in context: http://www.nabble.com/How-to-find-the-zero-%28only-the-real-solution%29-with-the-package-polynom---tf4756646.html#a13602364 Sent from the R help mailing list archive at Nabble.com.
Ptit_Bleu
2007-Nov-06 08:30 UTC
[R] How to find the zero (only the real solution) with the package polynom ?
Hi again, I found a solution (but maybe it is not the best one) : I converted the poynom into a function. pderiv<-as.function((deriv(polynomial(pfit$coefficients)))) uniroot(pderiv, c(0.8,0.2))[[1]] [1] 0.4778413 Now I just have to find the corresponding "b" to this "a" but I think it must be quite easy (am I right ?). I don't delete my post as it maybe be helpful to someone else but if the Administrator wants to delete it, it is ok to me. See you, Ptit Bleu. ------------------- Ptit_Bleu wrote:> > Hello, > > I have 3 columns : a, b and a*b > I would like to find the pair (a,b) so that a*b is the minimum but not > from the points I measured but from the fit of the curve (I have more > points that the ones given below but I fit only on this part because I > know that the minimum a*b is in this interval). > > I thought doing it this way : > > - to fit a*b=f(a) > abfit<-lm(ab ~ poly(a,8,raw=T)) > > - to use the package polynom to make the polynom > library(polynom) > polynomial(abfit$coefficients) > 0.03537506 - 0.8506516*x + 8.302545*x^2 - 44.80418*x^3 + 144.2161*x^4 - > 283.2458*x^5 + 331.1776*x^6 - 210.5144*x^7 + 55.86646*x^8 > > - to derive the polynom > deriv(polynomial(pfit$coefficients)) > -0.8506516 + 16.60509*x - 134.4125*x^2 + 576.8643*x^3 - 1416.229*x^4 + > 1987.066*x^5 - 1473.601*x^6 + 446.9317*x^7 > > - to find the zero > solve(deriv(polynomial(pfit$coefficients))) > [1] 0.1749897+0.0568886i 0.1749897-0.0568886i 0.3742571-0.1235393i > 0.3742571+0.1235393i 0.4778418+0.0000000i 0.8604070+0.1306799i > 0.8604070-0.1306799i > > And here is my little problem : I don't want the complex solutions, only > the real one (0.4778418). > > I also tried uniroot but I got an error message : > uniroot(deriv(polynomial(pfit$coefficients)), c(0.8,0.2)) > Error in uniroot(deriv(polynomial(abfit$coefficients)), c(0.8, 0.2)) : > Impossible to find the function "f" > > Is there a way to extract this real solution ? > > I thank you in advance for your help, > Have a nice day, > Ptit Bleu. > > ------------------------------------------------- > Set of points > a b a*b > 0.8 0.033320357 0.026656286 > 0.76 0.023930636 0.018187283 > 0.72 0.0153903 0.011081016 > 0.68 0.007844914 0.005334542 > 0.64 0.001598132 0.001022804 > 0.6 -0.00315939 -0.001895634 > 0.56 -0.006402796 -0.003585566 > 0.52 -0.008379886 -0.004357541 > 0.48 -0.009528793 -0.004573821 > 0.44 -0.010198446 -0.004487316 > 0.4 -0.010638282 -0.004255313 > 0.36 -0.010937536 -0.003937513 > 0.32 -0.011169004 -0.003574081 > 0.28 -0.011343229 -0.003176104 > 0.24 -0.0114878 -0.002757072 > 0.2 -0.011630636 -0.002326127 > 0.16 -0.011724586 -0.001875934 > >-- View this message in context: http://www.nabble.com/How-to-find-the-zero-%28only-the-real-solution%29-with-the-package-polynom---tf4756646.html#a13602541 Sent from the R help mailing list archive at Nabble.com.