How to set which variable to update in an iterative loop?

3 次查看(过去 30 天)
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 个评论
Valerio Cittadini
It is difficult to explain, I admit. Basically those c_x are the same quantity, but calculated with two different approaches, and that's why I called it in two different ways (c_x_av e c_x_av_mom). The first is calculated through a long procedure whose input is that 'a_D1', but there's no direct relation between them. The second can be calculated directly as a function of 'a_D1'. I have to find the value of 'a_D1' that makes the two equal.
Valerio Cittadini
Sorry, the two ways I called the quantity are c_x_av_D1 e c_x_av1_D1.

请先登录,再进行评论。

回答(1 个)

Anh Tran
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

类别

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