Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trouble using fzero():,,Function values at interval endpoints must be finite and real.

494 views
Skip to first unread message

Bilal

unread,
Jul 30, 2012, 11:42:31 PM7/30/12
to
Hi,

matlab gives me following error when I run M-file that uses fzero() function.
??? Error using ==> run
Error using ==> fzero
Function values at interval endpoints must be finite and real.

my M-file is:

function result = FUNCgamma(gamma, n)

m=5;
W=31;

p = 1-(1-gamma)^(n-1);
term0 = 2 * (1 - (2 * p)); % * (1 - p));
term1 = W * (1 - (2 * p)^(m + 1)) * (1 - p);
term2 = (1 - 2 * p);
term3 = ((1 - p)^(m+1));

result = gamma - (term0 / (term1 + term2 * term3)) * ((1-p^(m+1)));

Now statement at command window: result = fzero(@FUNCgamma1,[0 1],[],15)

Does anyone know what is the reason behind this error.

I also tried changing values of parameters. for example, When I reduced interval to [0 .9], it worked without error for n=15, however for larger values of 'n' it again starts giving error. BTW reducing interval is not an option with me it must be [0 1] with the value of 'n'keep changing.

Could anyone help me please.
--
Bilal

Bruno Luong

unread,
Jul 31, 2012, 2:17:07 AM7/31/12
to
Bilal <bilal...@googlemail.com> wrote in message <9c4c2724-4fb2-4b8b...@googlegroups.com>...

>
> Does anyone know what is the reason behind this error.
>

The error message tells exactly what the problem is: your function returns complex output or infinity in some cases.

Bruno

Bilal

unread,
Jul 31, 2012, 3:01:41 AM7/31/12
to Bruno Luong
Thanks Bruno for your reply.
I did some search on internet to find if there is anyway to avoid fzero returning complex outputs. I found an article that says "To avoid these problems, you need to bound your solution so that complex numbers are not generated, or you may need to adjust the output of your function to insure that it doesn't produce any complex outputs". However, it doesn't give any practical example that can help me solve my problem.
Does anyone know how to bound the solution or adjust the output of the function.

Bruno Luong

unread,
Jul 31, 2012, 3:32:51 AM7/31/12
to
Bilal <bilal...@googlemail.com> wrote in message <fcf93271-4b83-4515...@googlegroups.com>...
> Thanks Bruno for your reply.
> I did some search on internet to find if there is anyway to avoid fzero returning complex outputs. I found an article that says "To avoid these problems, you need to bound your solution so that complex numbers are not generated, or you may need to adjust the output of your function to insure that it doesn't produce any complex outputs". However, it doesn't give any practical example that can help me solve my problem.
> Does anyone know how to bound the solution or adjust the output of the function.

It's quite trivial:

x = min(x, upperbound);
x = max(x, lowerbound);
f = ....

% Bruno

Torsten

unread,
Jul 31, 2012, 3:41:46 AM7/31/12
to
On 31 Jul., 09:01, Bilal <bilalci...@googlemail.com> wrote:
> Thanks Bruno for your reply.
> I did some search on internet to find if there is anyway to avoid fzero returning complex outputs. I found an article that says "To avoid these problems, you need to bound your solution so that complex numbers are not generated, or you may need to adjust the output of your function to insure that it doesn't produce any complex outputs". However, it doesn't give any practical example that can help me solve my problem.
> Does anyone know how to bound the solution or adjust the output of the function.
>
>
>
> On Tuesday, July 31, 2012 3:17:07 PM UTC+9, Bruno Luong wrote:
> > Bilal <bilalci...@googlemail.com> wrote in message <9c4c2724-4fb2-4b8b...@googlegroups.com>...
>
> > > Does anyone know what is the reason behind this error.
>
> > The error message tells exactly what the problem is: your function returns complex output or infinity in some cases.
>
> > Bruno- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -


You must have gamma in [0;1].

So try the following:

function result = FUNCgamma(gamma_strich, n)

gamma = (sin(gamma_strich))^2;

m=5;
W=31;


p = 1-(1-gamma)^(n-1);
term0 = 2 * (1 - (2 * p)); % * (1 - p));
term1 = W * (1 - (2 * p)^(m + 1)) * (1 - p);
term2 = (1 - 2 * p);
term3 = ((1 - p)^(m+1));


result = gamma - (term0 / (term1 + term2 * term3)) * ((1-p^(m
+1)));

The zero you search for will then be (sin(gamma_strich))^2 where
gamma_strich is the solution fzero returns to the calling program.

Best wishes
Torsten.

Bilal

unread,
Jul 31, 2012, 11:14:39 PM7/31/12
to
Excellent, it worked.
thanks
--
0 new messages