How to set which variable to update in an iterative loop?
이전 댓글 표시
Hello, I have a couple of problems with my Matlab code. I have to set an iterative loop for the variable 'a_D1(z)'. 'a_D1(z)' is an input for a procedure that ends with a result, the one called 'c_x_av_D1(z)' in the code. But there is not an explicit function to relate them. Then I have another quantity, the one called 'c_x_av_mom_D1(z)', that can be explicitly expressed as a function of 'a_D1(z)'. Basically I have to iterate, updating the value of 'a_D1(z)', so that those 2 quantities are equal. Thank you.
a_D1(z)=0.1
.
.
.
.
.
.
.
.
.
.
V_D1(z)=(1-a_D1(z))*V_0
.
.
.
.
.
V_E1(z)=(1-2*a_D1(z))*V_0
.
.
.
.
c_x_av_D1(z)=F_x_av_D1(z)/(1/2*rho*V_0^2*A_d(z))
.
.
.
.
.
.
c_x_av1_D1(z)=4*a_D1(z)*(1-a_D1(z))
댓글 수: 4
James Tursa
2017년 9월 5일
It is not clear to me. Are you trying to find values for which a system of equations will be equal? If so, what exactly do you what to be equal to what?
Image Analyst
2017년 9월 5일
What does "set an iterative loop" mean???
Valerio Cittadini
2017년 9월 6일
Valerio Cittadini
2017년 9월 6일
답변 (1개)
Anh Tran
2017년 9월 8일
HI Valerio,
My understanding is you just want to find a value of 'a_D1(z)' that makes 'c_x_av_D1' and 'c_x_av1_D1' have equal. You may use a 'while' loop which increases a_D1(z) by a small step size each iteration and terminates when the difference is equal to zero or less than a tolerance. 2 numbers can rarely be exactly equal because of floating-point representation accuracy, so I would suggest setting up a tolerance for their difference. You may need to change step_size and tolerance for your application (might result in an infinite loop if not set carefully along with your initial guess of a_D1). Good luck!
step_size = 0.0001;
tolerance = 0.001;
while terminate ~= 1
a_D1(z) = a_D1(z) + step_size; % increment a_D1(z) with small step_size
% calculate c_x_av_D1
% calculate c_x_av1_D1
if abs(c_x_av_D1 - c_x_av1_D1) < tolerance
terminate = 1; % terminate the while loop
end
end
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!