Borrar filtros
Borrar filtros

How to change many loops to recursive function?

1 visualización (últimos 30 días)
Paranee Sawad
Paranee Sawad el 24 de Feb. de 2019
Comentada: Paranee Sawad el 5 de Mzo. de 2019
Hi, i would like to replace for loops to recursive function. I also would like to have my 5 variables m,al,R0,R1,e in array of (min, max, step,name) because may be in the future i might have more variables so i just add more array and change my function f.
function xm = minfricloop
xm = [ Inf Inf Inf Inf Inf Inf]; %For minimization
mmin = 3;
mmax = 12;
almin = 0.2;
almax = 1;
R0min = 0.014;
R0max = 0.020;
R1min = 0.025;
R1max = 0.030;
emin = 0.5;
emax = 0.9;
rpm = 20000;
ita = 0.1;
h0 = 2.51849556599183e-05;
n = 5;
for v = 0 :20
m = mmin + (mmax-mmin)*(v/(20));
for w = 0 :20
al = almin + (almax-almin)*(w/(20));
for x = 0 :20
R0 = R0min + (R0max-R0min)*(x/(20));
for y = 0 :20
R1 = R1min + (R1max-R1min)*(y/(20));
for z = 0 :20
e = emin + (emax-emin)*(z/(20));
k = (2*pi*e*(R0+R1)/(2*m))*tan(al*pi/180)/h0;
f = (R1-R0)*(rpm*2*pi*(R0+R1)/(2*60))*ita*(2*pi*e*(R0+R1)/(2*m))*((6/(k+2))+(4*log(k+1)/k))/h0;
if f < xm(6) %For minimization
xm = [m al R0 R1 e f];
end
end
end
end
end
end
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de Feb. de 2019
Editada: Walter Roberson el 24 de Feb. de 2019
Code attached showing how to use ndgrid for this kind of work.
On my system execution time is less than 0.15 seconds.
  22 comentarios
Walter Roberson
Walter Roberson el 4 de Mzo. de 2019
Just before you calculate error, set
h0 = max(h0, realmin);
This will prevent h0 from going negative, and so will prevent complex values from being calculated. Somehow it is able to satisfy the error conditions.
Paranee Sawad
Paranee Sawad el 5 de Mzo. de 2019
Dear Walter Roberson,
Thank you very much (:
and now i try to recheck the value back, for example in attached file G.m this is the original code when i use to compute h0 if i know the value for m, al, R0, R1 but it seems like when i substitute it didnt return the value h0 same as the value that compute in file minftnormalit123.m

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center 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