Initial guess Error using fsolve

This is my code
clear all
tic
% fun= @(x)qfunc(2-0.5*x)*exp(-x^2);
% %
P0=0.5;
P1=1-P0;
P=P0/P1;
rho=0.3; %covariance
s1=0.5; %mean of sensor 1
s2=1;%mean of sensor 2
Sm=max(s1,s2);
t1=-20:0.01:20 ; %threhsold for sensor 1
stop=0;
stop_1=0;
for i=1:length(t1)
fun= @(x2) exp(-s2.^2./2).*exp(x2.*s2).*qfunc((t1(i)-s1-rho.*x2+rho.*s2)./sqrt(1-rho.^2))-P.*qfunc((t1(i)-rho.*x2)./sqrt(1-rho.^2))
myfun= @(y) exp(-s2^2./2).*exp(y*s2)*(1-qfunc((t1(i)-s1-rho*y+rho*s2)./sqrt(1-rho^2)))./(1-qfunc((t1(i)-rho.*y)./sqrt(1-rho^2)))-P;
y0=rand;
tw0=fsolve(myfun,y0);
end
I get this error msg ''Objective function is returning undefined values at initial point. fsolve cannot
continue.''
I think the function handle becomes of 0/0 form for initial guess because of qfunc of what I have underlined. How do I put a condtion to use the approximate of qfunc whenever the fsolve counters 0/0 form?

2 comentarios

Array indices must be positive integers or logical values.
Error in @(y)exp(-s2^2./2).*exp(y*s2)*(1-qfunc((t1(i)-s1-rho*y+rho*s2)./sqrt(1-rho^2)))./(1-qfunc((t1(i)-rho.*y)./sqrt(1-rho^2)))-P
Your t1 is a vector, but your i is not defined so it defaults to being sqrt(-1) which is not a valid index.
Shailee Yagnik
Shailee Yagnik el 23 de Ag. de 2019
Hello,
I have editted the above code in question kindly go through it. It still gives the error: ''Objective function is returning undefined values at initial point. fsolve cannot continue''

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Ag. de 2019
NZ = @(a,b) a ./ (b + (a==0 & b == 0));
myfun=@(y) exp(-s2^2./2).*exp(y*s2).*NZ(1-qfunc((t1(i)-s1-rho*y+rho*s2)./sqrt(1-rho^2)),1-qfunc((t1(i)-rho.*y)./sqrt(1-rho^2)))-P;
This detects the case where numerator and denominator are both 0, and substitutes 1 for the denominator in that case, giving a 0/1 result. Perhaps you might prefer
NZ = @(a,b) (a + (a==0 & b == 0)) ./ (b + (a==0 & b == 0));

7 comentarios

Shailee Yagnik
Shailee Yagnik el 23 de Ag. de 2019
Hi, I dont understand how it detects the 0/0 case. Can u please make me understand that ?
also i would like to substitue the myfun with
@(tw0) exp(-s2.^2./2).*exp(tw0.*s2).*(1-(1./(sqrt(2.*pi).*(t1-s1-rho.*tw0+rho.*s2)./sqrt(1-rho.^2))).*exp(-((t1-s1-rho.*tw0+rho.*s2)./sqrt(1-rho.^2)).^2./2))./(1-(1./(sqrt(2.*pi).*(t1-rho.*tw0)./sqrt(1-rho.^2))).*exp(-((t1-rho.*tw0)./sqrt(1-rho.^2)).^2./2))-P
so that it can avoid the 0/0 form.
Can you please help me out with that?
The code
myfun=@(y) exp(-s2^2./2).*exp(y*s2).*NZ(1-qfunc((t1(i)-s1-rho*y+rho*s2)./sqrt(1-rho^2)),1-qfunc((t1(i)-rho.*y)./sqrt(1-rho^2)))-P;
transforms the (1-qfunc((t1(i)-s1-rho*y+rho*s2)./sqrt(1-rho^2)) ./ (1-qfunc((t1(i)-rho.*y)./sqrt(1-rho^2))) subexpression into NZ(1-qfunc((t1(i)-s1-rho*y+rho*s2)./sqrt(1-rho^2)), 1-qfunc((t1(i)-rho.*y)./sqrt(1-rho^2))) which is a call to NS with two parameters.
NZ has been defined as
NZ = @(a,b) (a + (a==0 & b == 0)) ./ (b + (a==0 & b == 0));
This checks whether both of the arguments are zero, and if so adds 1 to both the numerator and denominator, transforming it from a 0 / 0 expression to a (0+1)/(0+1) = 1/1 expression, thus substituting 1 for the 0/0
Shailee Yagnik
Shailee Yagnik el 24 de Ag. de 2019
Thanks for the explanation. Is it possible to substitute an expression in the function when both the arguments of original function forms 0/0 form?
NZ = @(a,b) (a + (a==0 & b == 0) .* TheExpression) ./ (b + (a==0 & b == 0));
In the case of 0, 0, this would give TheExpression on the numerator and 1 in the denominator.
Note: this will not work if TheExpression evaluates to nan or inf when a is non-zero or b is non-zero.
Shailee Yagnik
Shailee Yagnik el 24 de Ag. de 2019
Thank You. It solved the problem.
NEED Help!
is it correct to write and execute the function in context to the above problem:
NZ = @(a,b,y) ( (a+ (a==0 & b==0).*(1-1./(sqrt(2.*pi).*(t1-s1-rho.*y+rho.*s2)./sqrt(1-rho.^2))).*exp(-((t1-s1-rho.*y+rho.*s2)./sqrt(1-rho.^2)).^2./2))./ (b + (a==0 & b==0 )*(1-1./(sqrt(2.*pi).*(t1-rho.*y)./sqrt(1-rho.^2))).*exp(-((t1-rho.*y)./sqrt(1-rho.^2)).^2./2)));
myfun= @(y) (exp(-s2^2./2).*exp(y*s2)*NZ(qfunc(-(t1(i)-s1-rho*y+rho*s2)./sqrt(1-rho^2)),qfunc(-(t1(i)-rho.*y)./sqrt(1-rho^2)),y))-(P);
Walter Roberson
Walter Roberson el 29 de Ag. de 2019
Possibly. However for clarity I would probably move some of that expressions into helper functions; in its current form, it is difficult to read.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Audio I/O and Waveform Generation en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by