How do i store the output from my for loop for each iteration

조회 수: 3 (최근 30일)
Hung Yu
Hung Yu 2023년 8월 30일
댓글: Torsten 2023년 8월 30일
I am trying to store the output of the solution after each iteration. can someone help?
initial_guess = f_0 ;
sol = tosolve(initial_guess)
function [sol] = tosolve(initial_guess)
rho = 1.23 ; %fluid density kg/m^3
mu = 1.79*10^(-5) ; %fluid viscosity Ns/m^2
D = 0.005 ; %pipe diameter m
u = 40 ; %fluid velocity m/s
e = 0.0015e-3; %pipe roughness mm
Re = rho*u*D/mu ; %Reynolds number
sol = initial_guess ;
for i = 1:10
RHS = -2.0*(log10( (e./(3.7*D)) + (2.51./(Re.*sqrt(sol)))));
f_est = ((1./RHS)).^2 ;
error = (sol - f_est)./sol ;
if abs(error) > 0.00005
sol = f_est ;
store(i,:) = [sol] ;
else
end
end
end

답변 (1개)

Torsten
Torsten 2023년 8월 30일
편집: Torsten 2023년 8월 30일
You must return "store", not "sol" to the program calling the function "tosolve".
  댓글 수: 2
Hung Yu
Hung Yu 2023년 8월 30일
can you show it in my code thanks.
Torsten
Torsten 2023년 8월 30일
f_0 = 10.0;
initial_guess = f_0 ;
store = tosolve(initial_guess)
store = 1×7
10.0000 0.0168 0.0313 0.0287 0.0290 0.0290 0.0290
function [store] = tosolve(initial_guess)
rho = 1.23 ; %fluid density kg/m^3
mu = 1.79*10^(-5) ; %fluid viscosity Ns/m^2
D = 0.005 ; %pipe diameter m
u = 40 ; %fluid velocity m/s
e = 0.0015e-3; %pipe roughness mm
Re = rho*u*D/mu ; %Reynolds number
sol = initial_guess ;
store(1) = sol;
for i = 1:10
RHS = -2.0*(log10( (e./(3.7*D)) + (2.51./(Re.*sqrt(sol)))));
f_est = ((1./RHS)).^2 ;
error = (sol - f_est)./sol ;
if abs(error) > 0.00005
sol = f_est ;
store = [store,sol] ;
else
end
end
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by