How to change many loops to recursive function?

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2019-2-24
编辑:Walter Roberson 2019-2-24
Code attached showing how to use ndgrid for this kind of work.
On my system execution time is less than 0.15 seconds.
  22 个评论
Walter Roberson
Walter Roberson 2019-3-4
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 2019-3-5
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by