0

Here is the equation to be solved:

sum( (x-miu)/(1+l*(x-miu)) ) == 0

x is a vector

x<- c(0.490239414, -0.041047069, -0.062440582, -0.020759616, -0.084667527,  0.006101447,  0.985401602, -0.665158752,  0.153003354,  0.515112122)

miu=0.1 

I tried to use "uniroot"

a<-uniroot(function(l){sum((x-miu)/(1+l*(x-miu)))},c(-20,20))$root

I found "a" is 8.280825. However, when I want to substitute l into a to have a check, I found

sum((x-miu)/(1+a*(x-miu))) 

is -11257.84, instead of 0.

Charles Yan
  • 83
  • 1
  • 1
  • 9
  • Sorry, this is my first time posting here. I will be careful next time. Actually this is not homework..... I looked through the introduction of uniroot but not sure whether it would work in this situation. x_1 might be 1.3 and x_2 might be negative. Anyway, I am still trying to figure it out. – Charles Yan Dec 03 '15 at 18:02
  • Another thing, it seems I need to know the interval of lambda so that computer can help me with that. However, I have no idea of lambda....so I am thinking whether uniroot can work..... – Charles Yan Dec 03 '15 at 18:08
  • Okay, maybe I didn't describe it precisely. Sorry for my poor English. I don't need optimization and I just need to solve lambda.... Anyway, thx :) – Charles Yan Dec 03 '15 at 18:10
  • I attached the code just then~~~~ – Charles Yan Dec 04 '15 at 16:54
  • I've removed my comments since there is now sufficient detail to work with. I think you will be using `x` in the position of 'lambda' for the reasons stated below. Basically you will want to solve for `x` while using constant or measured data inserted into the 'positions' currently occupied by your `x`'s. I'm wondering if you set up the expression correctly since the resulting function seems so "pathological". – IRTFM Dec 04 '15 at 19:17

1 Answers1

2

I can reproduce your difficulties and will show you how I have attempted to overcome them. First off, you do not want x to be the name for to be those data values. You want them assigned to some other name since R will choose x as the default formal parameter to vary for curve and uniroot, so I chose to reverse x with l and further to make it ll since the letter l is so similar to the numeral 1 so:

ll<- c(0.490239414, -0.041047069, -0.062440582, -0.020759616, -0.084667527,  0.006101447,  0.985401602, -0.665158752,  0.153003354,  0.515112122)
miu=0.1

I tried to plot the function with initial failure:

curve(function(x){sum((ll-miu)/(1+ll*(x-miu)))}, -20,20)
Error in curve(function(x) { : 
  'expr' did not evaluate to an object of length 'n'

That mathematical expression on the top of your question wasn't being evaluated as a summation since the sum function isn't "vectorized". So I decided to use the Vectorize function to make a function that would behave in the manner we expect for calculation ( or at least for plotting) purposes:

Vfunc <- Vectorize( function(x){sum((ll-miu)/(1+x*(ll-miu)))} )
png(); curve(Vfunc, -20,20); abline(h=0, lty=3, col="red"); dev.off()  

enter image description here

So looking at the curve lets you see its asymptotic behavior near each of the points at which one would expect the 'll'-values in the denominator to create a degeneracy at the solutions to (1+x*(ll-miu) == 0, and therefore to decide on appropriate ranges over which to seek solution within the non-degenerate intervals.

> Vfunc(-15)
[1] -0.2580872   # Ooops, not far enough to the left, try another point.
> Vfunc(-18)
[1] 0.7168581  # better
> uniroot(Vfunc, c(-18,-5) )   
$root
[1] -16.72707

$f.root
[1] -3.477722e-06

$iter
[1] 7

$init.it
[1] NA

$estim.prec
[1] 6.103516e-05

> png(); curve(Vfunc, -20,20); abline(h=0, lty=3, col="red"); abline(v= -16.72707, col="blue"); dev.off()

> Vfunc(-16.72707)
[1] -4.13423e-06

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487