简体   繁体   English

在Matlab中收敛的迭代,无需使用while循环

[英]Iteration for convergence in Matlab without using a while loop

I have to iterate a process where I have an initial guess for the Mach number (M0). 我必须迭代一个过程,对马赫数(M0)进行初步猜测。 This initial guess will give me another guess for the Mach number by using two equations (Mn). 最初的猜测将使我通过使用两个方程式(Mn)对马赫数进行另一个猜测。 Eventually, i want to iterate this process untill the error between M0 and Mn is small. 最终,我要重复此过程,直到M0和Mn之间的误差很小为止。 I have the following piece of code and it actually works well with a while loop. 我有以下代码,它在while循环中可以很好地工作。

However, I am afraid that the while loop will take many iterations and computational time for certain inputs since this will be part of a bigger code which most likely will give unfeasible inputs for the while loop. 但是,对于某些输入,我担心while循环会花费很多迭代和计算时间,因为这将是更大代码的一部分,这很可能会为while循环提供不可行的输入。

Therefore my question is the following. 因此,我的问题如下。 How can I iterate this process within Matlab without consulting a while loop? 如何在不咨询while循环的情况下在Matlab中迭代此过程? The code that I am implementing now is the following: 我现在实现的代码如下:

%% Input
gamma = 1.4;
theta = atan(0.315);
cpi = -0.732;
%% Loop
M0 = 0.2; %initial guess 
Err =  100;
iterations = 0;

while Err > 0.5E-3  
B = (1-(M0^2)*(1-M0*cpi))^0.5; 
Mn = (((gamma+1)/2) * ((B+((1-cpi)^0.5)*sec(theta)-1)^2/(B^2 + (tan(theta))^2)) - ((gamma-1)/2) )^-0.5;
Err = abs(M0 - Mn);
M0 = Mn;
iterations=iterations+1;
end

disp(iterations) disp(Mn)

Many thanks 非常感谢

I think you need to clarify the issue. 我认为您需要澄清这个问题。 If it the issue you want to solve is that some inputs take a long time to calculate, it is not the while loop that takes the time, it is the execution of the code multiple times that causes it. 如果您要解决的问题是某些输入需要很长时间才能计算出来,则不是while循环会花费时间,而是多次执行代码才导致该问题。 Any method that loops through will be restricted by the time the block of code takes to execute multiplied by the number of iterations required to converge. 循环通过的任何方法都将受到代码块执行所需时间乘以收敛所需迭代次数的限制。

You can introduce something to stop at a certain number of iterationtions, conceptually: 从概念上讲,您可以引入一些方法来停止一定数量的迭代:

While ((err > tolerance) && (numIterations < limit)) While((错误>公差)&&(numIterations <限制))

If you want an answer which does not require iterating over the code, this is akin to finding a closed form solution, and I suspect this does not exist. 如果您想要一个不需要遍历代码的答案,那类似于找到一个封闭形式的解决方案,我怀疑这不存在。

Edit to add: by not exist I mean in a practical form which can be implemented in a more efficient way then iterating to a solution. 编辑添加:不存在,是指以一种实用的形式,可以用一种更有效的方式实施,然后迭代到一个解决方案。

Since M0 is calculated in each iteration and you have trigonometric functions, you cannot use another way than iteration structures (ie while ). 由于M0是在每次迭代中计算的,并且您具有三角函数,因此除了迭代结构(即while ),您不能使用其他方法。

If you had a specific increase or decrease at M0 , then you could initialize a vector of M0 and do vector calculations for B and Err . 如果在M0处有特定的增加或减少,则可以初始化M0的向量,并对BErr进行向量计算。

But, with sec and tan this is not possible. 但是,使用sectan不可能。

Another wat would be to use the parallel processing. 另一个麻烦是使用并行处理。 But, since you change the M0 at each iteration then you cannot use the parfor loop. 但是,由于每次迭代都更改M0 ,因此无法使用parfor循环。

As for a for loop, in MATLAB you need an array for for "command" argument (eg 1:10 or 1:length(x) or i = A, where A = 1:10 or A = [1:10;11:20]). 作为一个for循环,在MATLAB需要一个阵列,用于for “命令”参数(例如,1:10或1:长度(x)或I = A,其中A = 1:10或A = [1:10; 11 :20])。 Since you evaluate a condition and depending on the result of the evaluation you judge if you continue the execution or not, it seems that the while loop (or do while in another language) is the only way to go. 由于您评估条件,并根据评估结果判断是否继续执行,似乎while循环(或用另一种语言执行while)是唯一的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM