How to set which variable to update in an iterative loop?
3 views (last 30 days)
Show older comments
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
Answers (1)
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
0 Comments
See Also
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!