How do you write a loop which loops certain equations until a particular condition is met?
6 次查看(过去 30 天)
显示 更早的评论
I have a couple design variables which are outputs from a true design point calculation:
% True Design Point Output: SDMFc_DP=10; TTR_DP=4;
The two variables above do not change at different design point conditions i.e. these values are constant no matter if the inputs are changed.
I run the previous calculation again using initially guessed inputs, stated below.
% Initially Guessed Inputs: r_c_ODPi=5; TET_ODPi=1000;
which results in these output:
% Initially Guess Design Point Outputs: SDMFc_ODPi=4; TTR_ODPi=6;
As previously stated the initially guessed design point outputs should equal the true design point outputs: SDMFc_DP==SDMFc_ODPi, and TTR_DP==TTR_ODPi. Therefore, I need to alter my inputs until this condition is met. The inputs are freely changeable.
Therefore, I'm looking for way to write an infinite loop that will alter the inputs: r_c_ODPi and TTR_ODPi by a certain value i.e +/- 0.001 for example until the condition of SDMFc_DP==SDMFc_ODPi and TTR_DP==TTR_ODPi, then when the condition is met to stop looping.
Apologies for the confusing nature of this. Hopefully I've explained it clearly enough.
Thanks for any assistance. Colin
0 个评论
回答(1 个)
Star Strider
2016-3-29
I don’t entirely understand what you want to do in terms of your calculations. It would seem that a while loop would be most appropriate. This is easiest with a ‘greater than’ or ‘less than’ condition. If your condition is to be ‘equal to’, be sure to include a tolerance value (perhaps 1E-4 or so) to account for floating-point approximation error and the inherent accumulation of these errors in iteration or calculation.
2 个评论
Star Strider
2016-3-29
My pleasure.
Perhaps I’m failing to understand something, but is it possible to invert your ‘design point calculation’ so you can use the desired outputs as inputs to the inverted system and then just solve for the inputs of the ‘design point calculation’ as outputs of the inverted system?
Another option would be a nonlinear function minimisation or nonlinear curve-fitting approach. Genetic algorithms and the patternsearch function (Global Optimization Toolbox) would likely be more efficient than just iterating.
That said, a while loop would work like this:
X = 0;
Y = 0;
while Y < 9
X = X + 0.1;
Y = 10 * sin(X);
end
That’s definitely not the most efficient way to optimise a function, though!
It would be helpful if you shared your ‘design point calculation’. I certainly understand your not wanting to if it’s part of your research.
另请参阅
类别
在 Help Center 和 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!