필터 지우기
필터 지우기

How to change many loops to recursive function?

조회 수: 5 (최근 30일)
Paranee Sawad
Paranee Sawad 2019년 2월 24일
댓글: Paranee Sawad 2019년 3월 5일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by