Clear Filters
Clear Filters

How to change many loops to recursive function?

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 24 Feb 2019
Edited: Walter Roberson on 24 Feb 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 Comments
Walter Roberson
Walter Roberson on 4 Mar 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 on 5 Mar 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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!