Regarding improving efficiency of the code using fzero and int (integral) function in for loop

Hi ,
I am working on data generation based on user input values using probability distribution functions. I have to repetatively solve an equation with single variable , which sometimes involves integration too. I used the 'fzero' and' int' function in the code in the 'for' loop. I am facing 2 problems here:
  1. The fzero function gets stuck in a loop , as it cannot find a real and finite solution
  2. In case , even if it finds a solution , the program takes long time to generate the data.
I have attached the code for your reference. Run the 'f_example.m' file to check the code.

Respuestas (1)

I tried to plot your function
c1 = .9;
c2 = 0.7;
c3 = 0.6;
x = -1:0.1:1;
f = @(x) c1 - exp(c2*x).*(1-c3*x);
plot(x,f(x))
As you can see you can have only 2 roots max
So this piece of a code needs to be improved
while (isnan(k(i1)) || isinf(k(i1)) )
x_sol_0=rand;
if isreal(fun(x_sol_0))
k(i1) = fzero(fun,x_sol_0);
end
end
Especially this line
x_sol_0=rand;
Maybe this way
x_sol_0=-x_sol;
Are you sure there are always roots that meet condition?

4 comentarios

x_sol_0=rand;
I use this to change the initial value for fzero to solve , if it encounters any NaN or inf value in the previous line. It should have the roots always. I will try your way and see , if it helps.
while (isnan(k(i1)) || isinf(k(i1)) )
x_sol_0=rand;
if isreal(fun(x_sol_0))
k(i1) = fzero(fun,x_sol_0);
end
end
This one is used because the line before( which I have show below )gives NAN or inf values sometimes. So I have checked that condition and tried to change the initial point for fzero to solve.
fun = @(kt)(kt_u*lamda*v*(1+erf(x(i1)/sqrt(2)))/(2*c))+1 - (exp(kt*lamda) *(1-v*kt));
k(i1,1) = fzero(fun,x_sol_0);
But some how , it gets stuck in the above while loop sometimes.
Hi darova,
x_sol_0 =-x_sol_0
This still doesnt work. It didnot help.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Preguntada:

el 29 de Mayo de 2020

Comentada:

el 29 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by