Algorithme de Newton-Raphson
Pouvez-vous en fait me corriger le code suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| x=c(1,2,3,4,5,6)
fx=c(1486,694,195,37,10,1)
moy <- sum(x*fx)/sum(fx)
theta=moy
i=0
repeat
{
n=sum(fx)
deriveesec=function(theta)
{
-sum(fx)/theta**2+(n*exp(-theta))/(1-exp(-theta))**2
}
i=i+1
thetaold=theta
derivee=-n+sum(fx)/theta-(n*exp(-theta))/(1-exp(-theta))
theta=theta-derivee/deriveesec
if (sqrt(sum((theta-thetaold)**2))<0.000001)
{break}
print(i)
} |
l'erreur retournée est la suivante :
Citation:
Error in derivee/deriveesec : non-numeric argument to binary operator
re :Algorithme de newton-raphson
J'ai corrigé le code précédent par le suivant, les dérivées sont numériques :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| theta=moy
i=0
repeat
{
n=sum(fx)
deriveesec={-sum(fx)/theta**2+(n*exp(-theta))/(1-exp(-theta))**2
}
i=i+1
thetaold=theta
derivee=
{
f=-n+sum(fx)/theta-(n*exp(-theta))/(1-exp(-theta))
}
theta=theta-derivee/deriveesec
if (sqrt(sum((theta-thetaold)**2))<0.001)
{break}
print(i)
} |
Cependant, il est impossible d'afficher theta et le message d'erreur est le suivant :
Citation:
Error in if (sqrt(sum((theta - thetaold)^2)) < 0.001) { :
missing value where TRUE/FALSE needed