Do while loop with recursive make the answer incorrect?

Dear all,
I have question about doing while loop since i try to find a value of h0 in while loop by using Newton's method and i let the 4 input parameters be recursive in the folowing(just want to recheck that's why i set N=2). And i also let it print the value of each parameters to recheck the value of h0.
function h0 = minftnaja
mmin = 3;
mmax = 12;
almin = 0.1;
almax = 1;
R0min = 0.013;
R0max = 0.015;
R1min = 0.020;
R1max = 0.050;
ita = 0.1;
frad = 220;
rpm = 20000;
N = 2 ;
[m_, al_, R0_, R1_] = ndgrid( linspace(mmin, mmax, N), ...
linspace(almin, almax, N), ...
linspace(R0min, R0max, N), ...
linspace(R1min, R1max, N));
h0 = 0.00002;
heps = 0.0001;
errF = 1e-4;
err = 1;
while all(err > errF) % will not stop as long as even one entry is out of tolerance,
% so when you stop then all of them will be within tolerance.
k = pi .* (R0_ + R1_) .* tan(al_ .* pi ./ 180) ./ (m_ .* h0);
fu = 6 .* (R1_ - R0_) .* (-rpm .* pi .* (R0_ + R1_) ./ 60) .* ita .* (-log(k+1) + ((2*k)./(k+2))) ./ (tan(al_ .* pi ./ 180)).^2;
f = fu-frad;
h0 = h0+heps;
kk = pi .* (R0_ + R1_) .* tan(al_ .* pi ./ 180) ./ (m_ .* h0);
fu = 6 .* (R1_ - R0_) .* (-rpm .* pi .* (R0_ + R1_) ./ 60) .* ita .* (-log(kk+1) + ((2*kk)./(kk+2))) ./ (tan(al_ .* pi ./ 180)).^2;
f1 = fu-frad;
h0 = h0-heps;
jacobian = (f1-f)/heps;
h0 = h0 - 0.01*f./jacobian;
err = abs(f/frad);
if err > errF
else
m_
al_
R0_
R1_
end
end
I use the value from each parameter to recheck the value of h0 by the function which is not using recursive to be input in the following.
function h0 = GG
frad = 220; %loading
rpm = 20000;
m = 3; %1
alpha = 0.1; %2
R0 = 0.013; %3
R1 = 0.020; %4
ita=0.01;
h0 = 0.00002;
heps = 0.0001;
errF = 1e-4;
err = 1;
while all(err > errF)
k = (2*pi*(R0+R1)/(2*m))*tan(alpha*pi/180)/h0;
fu = -(6*ita*(rpm*2*pi*(R0+R1)/(2*60))*(R1-R0)*(2*pi*(R0+R1)/(2*m))^2)*(-log(k+1)+(2*k)/(k+2))/((k^2)*(h0^2));
f = fu-frad;
h0 = h0+heps;
kk = (2*pi*(R0+R1)/(2*m))*tan(alpha*pi/180)/h0;
fu = -(6*ita*(rpm*2*pi*(R0+R1)/(2*60))*(R1-R0)*(2*pi*(R0+R1)/(2*m))^2)*(-log(kk+1)+(2*kk)/(kk+2))/((kk^2)*(h0^2));
f1 = fu-frad;
h0 = h0-heps;
jacobian = (f1-f)/heps;
h0 = h0 - 0.01*f/jacobian;
err = abs(f/frad);
end
end
The result is not the same and i also recheck that is this h0 correct from while loop with recursive? by use the values of 4 parameters and h0 to find value of fu which should give the value approximately equal to frad but it is not.
So i just wonder may be because of doing recursive in while loop that's the reason why the result is not the same. Am i right??

 採用された回答

Geoff Hayes
Geoff Hayes 2019 年 3 月 14 日

1 投票

Paranee - please clarify what recursive means to you. You may want to start with N equal to one and compare the results. If they are still different then ask yourself why. For example, should you get identical results when parameters such as alpha, ita, m, R0, R1, etc. are initialized to different values in your two functions? If they are the same then you will get identical results...

4 件のコメント

Paranee Sawad
Paranee Sawad 2019 年 3 月 14 日
編集済み: Paranee Sawad 2019 年 3 月 14 日
Dear Geoff,
I also check even if N=1 the result is not the same. I just want to do like brute force that let every parameters meet each other but it seems like when they go inside the while loop they are not working what i expected. I also try to change this to basic one by doing 4 for loops for each parameter but the result is different again. Where should i focus now? Can you recommend? Should i focus when they do while loop or where? Because for me now I think since everything in while loop is the same so they shold give the same result.
function h0 = GGGG
mmin = 3;
mmax = 12;
almin = 0.1;
almax = 1;
R0min = 0.013;
R0max = 0.015;
R1min = 0.020;
R1max = 0.050;
frad = 220;
rpm = 20000;
ita = 0.1;
h0 = 0.00002;
heps = 0.0001;
errF = 1e-4;
err = 1;
n = 1;
for v = 0 : n
m = mmin + (mmax-mmin)*(v/(n));
for w = 0 :n
al = almin + (almax-almin)*(w/(n));
for x = 0 :n
R0 = R0min + (R0max-R0min)*(x/(n));
for y = 0 :n
R1 = R1min + (R1max-R1min)*(y/(n));
while any(err > errF)
k = pi .* (R0 + R1) .* tan(al .* pi ./ 180) ./ (m .* h0);
fu = 6 .* (R1 - R0) .* (-rpm .* pi .* (R0 + R1) ./ 60) .* ita .* (-log(k+1) + ((2*k)./(k+2))) ./ (tan(al .* pi ./ 180)).^2;
f = fu-frad;
h0 = h0+heps;
kk = pi .* (R0 + R1) .* tan(al .* pi ./ 180) ./ (m .* h0);
fu = 6 .* (R1 - R0) .* (-rpm .* pi .* (R0 + R1) ./ 60) .* ita .* (-log(kk+1) + ((2*kk)./(kk+2))) ./ (tan(al .* pi ./ 180)).^2;
f1 = fu-frad;
h0 = h0-heps;
jacobian = (f1-f)/heps;
h0 = h0 - 0.01*f./jacobian;
err = abs(f/frad);
if all(err > errF)
else
m
al
R0
R1
end
end
end
end
end
end
Thank you very much
Geoff Hayes
Geoff Hayes 2019 年 3 月 14 日
Right, so if the answers are different when N equals one, then this suggests that the differences are do to something else. Why do you think you should get identical answers (between the two scripts) if the alpha, ita, m, R0, R1, etc. variables are different?
Paranee Sawad
Paranee Sawad 2019 年 3 月 14 日
Dear Geoff,
Because i think i let them do the same thing to find value of h0 which makes f = fu-frad = 0 then when it returns the result they should be the same. even though the input are different i mean one each value are set constant another one let them create values in array or in for loop. but when they do in a package like (m, al, R0, R1) together when i recheck i think they should be equal.
Paranee Sawad
Paranee Sawad 2019 年 3 月 16 日
Dear Geoff,
you are right. i set the different value of ita.
Thank you (:

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by