fzero function is returning initial value rather than finding zero.

3 vues (au cours des 30 derniers jours)
J_stat
J_stat le 4 Juin 2019
Commenté : Star Strider le 4 Juin 2019
I wrote following code but it is not returning zero. fzero returns initial values. Is there anything I did wrong?
alp=0.05; x=3; n=25;
syms k a;
ftest=@(a) 1-alp/2-.5*nchoosek(n,x)*a^x*(1-a)^(n-x) - symsum(nchoosek(n,k)*a^k*(1-a)^(n-k+1), k, 0, x-1);
fzero(ftest,0.02)
ans =
0.0200

Réponse acceptée

Star Strider
Star Strider le 4 Juin 2019
It seems that fzero is having problems with the symbolic part of your function. If you first create it as a separate anonymnous function (using the matlabFunction function):
alp=0.05; x=3; n=25;
syms k a;
nck = matlabFunction(symsum(nchoosek(n,k)*a^k*(1-a)^(n-k+1), k, 0, x-1));
ftest=@(a) 1-alp/2-.5*nchoosek(n,x)*a^x*(1-a)^(n-x) - nck(a);
ZV = fzero(ftest,0.5)
the result is:
ZV =
0.018920195502436
Note that the initial estimate and the result are different, so it is not returning the initial estimate as the optimised result.
  2 commentaires
J_stat
J_stat le 4 Juin 2019
Thank you so much.
Star Strider
Star Strider le 4 Juin 2019
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by