Clear Filters
Clear Filters

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

2 views (last 30 days)
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 Comments
Valerio Cittadini
Valerio Cittadini on 6 Sep 2017
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
Valerio Cittadini on 6 Sep 2017
Sorry, the two ways I called the quantity are c_x_av_D1 e c_x_av1_D1.

Sign in to comment.

Answers (1)

Anh Tran
Anh Tran on 8 Sep 2017
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!