matlab高手,在for循环中,设置每个循环的最大时间,如果超过该时间,跳出本次循环,进入下一次循环。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab高手,在for循环中,设置每个循环的最大时间,如果超过该时间,跳出本次循环,进入下一次循环。相关的知识,希望对你有一定的参考价值。
举个例子:
for i=1:10000
z=fmincon(@f(x),x0);
end.
这个例子里,如果我在某次,比如第k次吧,寻求f(x)的最小值时耗时过长,我需要直接进入到k+1次循环,请问具体的实现方法是什么?请大神指点!
假设我每次循环执行的时间上限是1分钟。
事实上,这种机制要求完成时间限制的进程具备更高的优先级,能够终结循环体对应的进程,也就是异常退出。我想不出自己熟悉的编程语言有哪个提供了这种能力。
之所以不提供这种机制,原因也是可以理解的:如果要求一旦到达指定的最大时间限制,就直接立即结束该进程,显而易见的后果是,当然会导致这个进程不能提供正常的输出。而更严重的是,在这个进程内部,完全可能存在会造成其它影响的行为,异常结束进程往往会存在安全隐患。
作为一种变通的做法,可以通过改写循环体的内容,一定程度上达到希望的要求。比如,改写fmincon函数,在其主循环中加入时间判断,一旦到了要求的时间,就放弃该次优化,返回失败的结果。但这并不能保证精确地满足时间限制。 参考技术A 建议不要这么弄,你可以试一试不同的初值,然后能够解决问题的那个就好。追问
恩,这个我知道,不过因为是循环,我不确定到底到哪次会出现这种跑不下去的问题。
追答[X,FVAL,EXITFLAG]=fmincon(@f(x),x0);while(EXITFLAG~=1)
[X,FVAL,EXITFLAG]=fmincon(@f(x),rand(size(x0)));
end本回答被提问者和网友采纳
Matlab for循环,Python中的不同值
我在matlab中有代码,我正尝试将其“重写”为Python。问题在于for循环的值不同。不幸的是我找不到解决方案。
问题在每个数组中都有值:nu
元素中的theta
mu
M
i
。
在Python中,DTOR
或RTOD
是弧度,反之亦然。
对于两个代码n=30
----------------------------------------- Matlab: --- ----------------------------------
for t = 1:2
i=n+2;
h = i;
k=0;
z=2;
for j= 1:n-1
while (i<=h+n-k-1)
if (i==h)
if (i==node_number-1)
y(i)=0;
theta(i)=0;
M(i) = Me;
nu(i) =PrandtlMeyer(M(i),gamma);
mu(i)= asin(1/M(i))*180/pi;
mI = tand((mu(i-n+k)-theta(i-n+k)+mu(i))/2);
x(i)= (y(i-n+k)/mI)+x(i-n+k);
if (t==2)
plot([x(i-n+k) x(i)],[y(i-n+k) y(i)]);
hold on;
end
else
y(i)=0;
theta(i)=0;
nu(i) = theta(i-n+k)+nu(i-n+k)+(1/(sqrt((M(i-n+k)^2)-1)-cotd(theta(i-n+k))));
M(i) = InversePrandtlMeyer(nu(i));
mu(i)= asin(1/M(i))*180/pi;
mI = tand((mu(i-n+k)-theta(i-n+k)+mu(i))/2);
x(i)= (y(i-n+k)/mI)+x(i-n+k);
if (t==2)
plot([x(i-n+k) x(i)],[y(i-n+k) y(i)]);
hold on;
end
end
else if (i==h+1)
if (i==node_number)
mI = tand((theta(i-n+k)+theta(i))/2);
mII =tand((mu(i-1)+theta(i-1)+theta(i)+mu(i))/2);
x(i) = (y(i-1)-y(i-n+k)+(x(i-n+k)*mI)-(x(i-1)*mII))/(mI-mII);
y(i) = y(i-1)+((x(i)-x(i-1))*mII);
y1(z) = y(i-1)+((x(i)-x(i-1))*mII);
x1(z) = (y(i-1)-y(i-n+k)+(x(i-n+k)*mI)-(x(i-1)*mII))/(mI-mII);
theta(i) = theta(i-1);
nu(i) = nu(i-1);
M(i) = M(i-1);
mu(i)= asin(1/M(i))*180/pi;
z=z+1;
if (t==2)
plot([x(i-1) x(i)],[y(i-1) y(i)]);
hold on;
plot([x(i-n+k) x(i)],[y(i-n+k) y(i)]);
hold on;
end
else
mI = tand((mu(i-n+k)-theta(i-n+k)+mu(i)-theta(i))/2);
mII = tand((mu(i)+theta(i)+mu(i-1))/2);
x(i) = (y(i-n+k)+(x(i-1)*mII)+(x(i-n+k)*mI))/(mII+mI);
y(i) = (x(i)-x(i-1))*mII;
nu(i) = (theta(i-n+k)+nu(i-n+k)+(nu(i-1)/2)+((1/(sqrt((M(i-n+k)^2)-1)-cotd(theta(i-n+k))))*((y(i)-y(i-n+k))/y(i-n+k))))/1.5;
theta(i)= (nu(i)-nu(i-1))/2;
M(i) = InversePrandtlMeyer(nu(i));
mu(i)= asin(1/M(i))*180/pi;
if (t==2)
plot([x(i-1) x(i)],[y(i-1) y(i)]);
hold on;
plot([x(i-n+k) x(i)],[y(i-n+k) y(i)]);
hold on;
end
end
else if (i==h+n-k-1)
mI = tand((theta(i-n+k)+theta(i))/2);
mII =tand((mu(i-1)+theta(i-1)+theta(i)+mu(i))/2);
x(i) = (y(i-1)-y(i-n+k)+(x(i-n+k)*mI)-(x(i-1)*mII))/(mI-mII);
y(i) = y(i-1)+((x(i)-x(i-1))*mII);
y1(z)=y(i-1)+((x(i)-x(i-1))*mII);
x1(z) = (y(i-1)-y(i-n+k)+(x(i-n+k)*mI)-(x(i-1)*mII))/(mI-mII);
theta(i) = theta(i-1);
nu(i) = nu(i-1);
M(i) = M(i-1);
mu(i)= asin(1/M(i))*180/pi;
z=z+1;
if (t==2)
plot([x(i-1) x(i)],[y(i-1) y(i)]);
hold on;
plot([x(i-n+k) x(i)],[y(i-n+k) y(i)]);
hold on;
end
else
mI = tand((mu(i-n+k)-theta(i-n+k)+mu(i)-theta(i))/2);
mII = tand((mu(i-1)+theta(i-1)+mu(i)+theta(i))/2);
x(i) = (y(i-n+k)-y(i-1)+(x(i-n+k)*mI)+(x(i-1)*mII))/(mII+mI);
y(i) = y(i-1)+((x(i)-x(i-1))*mII);
theta(i) = (theta(i-n+k)+nu(i-n+k)+theta(i-1)-nu(i-1)+((1/(sqrt((M(i-n+k)^2)-1)-cotd(theta(i-n+k))))*((y(i)-y(i-n+k))/y(i-n+k)))-((1/(sqrt((M(i-1)^2)-1)-cotd(theta(i-1))))*((y(i)-y(i-1))/y(i-1))))/2;
nu(i) = (theta(i-n+k)+nu(i-n+k)-theta(i-1)+nu(i-1)+((1/(sqrt((M(i-n+k)^2)-1)-cotd(theta(i-n+k))))*((y(i)-y(i-n+k))/y(i-n+k)))+((1/(sqrt((M(i-1)^2)-1)-cotd(theta(i-1))))*((y(i)-y(i-1))/y(i-1))))/2;
M(i) = InversePrandtlMeyer(nu(i));
mu(i)= asin(1/M(i))*180/pi;
if (t==2)
plot([x(i-1) x(i)],[y(i-1) y(i)]);
hold on;
plot([x(i-n+k) x(i)],[y(i-n+k) y(i)]);
hold on;
end
end
end
end
i = i+1;
end
k=k+1;
h=i;
end
end
以及Matlab“ theta”值的前几行:
0.4397 0.8793 1.3190 1.7587 2.1983 2.6380 3.0776 3.5173
3.9570 4.3966 4.8363 5.2760 5.7156 6.1553 6.5949 7.0346
7.4743 7.9139 8.3536 8.7933 9.2329 9.6726 10.1122 10.5519
10.9916 11.4312 11.8709 12.3106 12.7502 13.1899 13.1899 0
0.3032 0.7453 1.1877 1.6301 2.0725 2.5149 2.9572 3.3995
3.8418 4.2840 4.7262 5.1684 5.6106 6.0528 6.4951 6.9373
7.3795 7.8218 8.2641 8.7065 9.1488 9.5913 10.0337 10.4762
10.9188 11.3614 11.8041 12.2469 12.2469 0 0.2977 0.7407
1.1839 1.6271 2.0702 2.5133 2.9562 3.3991 3.8420 4.2848
4.7276 5.1704 5.6131 6.0559 6.4986 6.9414 7.3842 7.8270
----------------------------------------- Python: --- ----------------------------------
i = n + 1
h = i
k = 0
z = 1
for j in range(n-1):
while i <= h + n - k-1:
if i == h:
if i == node_number -1:
y[i] = 0
theta[i] = 0
M[i] = Me
nu[i] = PM(M[i], gamma)
mu[i] = np.arcsin(1 / M[i]) * 180 / np.pi
mI = np.tan((mu[i - n + k] - theta[i - n + k] + mu[i]) / 2 * DTOR)
x[i] = (y[i - n + k] / mI) + x[i - n + k]
else:
y[i] = 0
theta[i] = 0
nu[i] = theta[i - n + k] + nu[i - n + k] + (1 / (np.sqrt((M[i - n + k]**2) - 1) - np.cos((theta[i - n + k])*DTOR)))
M[i] = InversePrandtlMeyer(nu[i])
mu[i] = np.arcsin(1 / M[i]) * 180 / np.pi
mI = np.tan(((mu[i - n + k] - theta[i - n + k] + mu[i]) / 2) * DTOR)
x[i] = (y[i - n + k] / mI) + x[i - n + k]
elif i == h + 1:
if i == node_number:
mI = np.tan((theta[i - n + k] + theta[i]) / 2 * DTOR)
mII = np.tan((mu[i - 1] + theta[i - 1] + theta[i] + mu[i]) / 2 * DTOR)
x[i] = (y[i - 1] - y[i - n + k] + (x[i - n + k] * mI) - (x[i - 1] * mII)) / (mI - mII)
y[i] = y[i - 1] + ((x[i] - x[i - 1]) * mII)
y1[z] = y[i - 1] + ((x[i] - x[i - 1]) * mII)
x1[z] = (y[i - 1] - y[i - n + k] + (x[i - n + k] * mI) - (x[i - 1] * mII)) / (mI - mII)
theta[i] = theta[i - 1]
nu[i] = nu[i - 1]
M[i] = M[i - 1]
mu[i] = np.arcsin(1 / M[i]) * 180 / np.pi
z = z + 1
else:
mI = np.tan((mu[i - n + k] - theta[i - n + k] + mu[i] - theta[i]) / 2 * DTOR)
mII = np.tan((mu[i] + theta[i] + mu[i - 1]) / 2 * DTOR)
x[i] = (y[i - n + k] + (x[i - 1] * mII) + (x[i - n + k] * mI)) / (mII + mI)
y[i] = (x[i] - x[i - 1]) * mII
nu[i] = (theta[i - n + k] + nu[i - n + k] + (nu[i - 1] / 2) + ((1 / (np.sqrt((M[i - n + k] ** 2) - 1) - np.cos((theta[i - n + k])*DTOR))) * ((y[i] - y[i - n + k]) / y[i - n + k]))) / 1.5
theta[i] = (nu[i] - nu[i - 1]) / 2
M[i] = InversePrandtlMeyer(nu[i])
mu[i] = np.arcsin(1 / M[i]) * 180 / np.pi
elif i == h + n - k-1:
mI = np.tan((theta[i - n + k] + theta[i]) / 2 * DTOR)
mII = np.tan((mu[i - 1] + theta[i - 1] + theta[i] + mu[i]) / 2 * DTOR)
x[i] = (y[i - 1] - y[i - n + k] + (x[i - n + k] * mI) - (x[i - 1] * mII)) / (mI - mII)
y[i] = y[i - 1] + ((x[i] - x[i - 1]) * mII)
y1[z] = y[i - 1] + ((x[i] - x[i - 1]) * mII)
x1[z] = (y[i - 1] - y[i - n + k] + (x[i - n + k] * mI) - (x[i - 1] * mII)) / (mI - mII)
theta[i] = theta[i - 1]
nu[i] = nu[i - 1]
M[i] = M[i - 1]
mu[i] = np.arcsin(1 / M[i]) * 180 / np.pi
z = z + 1
else:
mI = np.tan((mu[i - n + k] - theta[i - n + k] + mu[i] - theta[i]) / 2 * DTOR)
mII = np.tan((mu[i - 1] + theta[i - 1] + mu[i] + theta[i]) / 2 * DTOR)
x[i] = (y[i - n + k] - y[i - 1] + (x[i - n + k] * mI) + (x[i - 1] * mII)) / (mII + mI)
y[i] = y[i - 1] + ((x[i] - x[i - 1]) * mII)
theta[i] = (theta[i - n + k] + nu[i - n + k] + theta[i - 1] - nu[i - 1] + ((1 / (np.sqrt((M[i - n + k] ** 2) - 1) - np.cos((theta[i - n + k]) * DTOR))) * ((y[i] - y[i - n + k]) / y[i - n + k])) - ((1 / (np.sqrt((M[i - 1] ** 2) - 1) - np.cos((theta[i - 1])*DTOR))) * ((y[i] - y[i - 1]) / y[i - 1]))) / 2
nu[i] = (theta[i - n + k] + nu[i - n + k] - theta[i - 1] + nu[i - 1] + ((1 / (np.sqrt((M[i - n + k] ** 2) - 1) - np.cos((theta[i - n + k])*DTOR))) * ((y[i] - y[i - n + k]) / y[i - n + k])) + ((1 / (np.sqrt((M[i - 1] ** 2) - 1) - np.cos((theta[i - 1])*DTOR))) * ((y[i] - y[i - 1]) / y[i - 1]))) / 2
M[i] = InversePrandtlMeyer(nu[i])
mu[i] = np.arcsin(1 / M[i]) * 180 / np.pi
i = i + 1
k = k + 1
h = i
以及Python“ theta”值的前几行:
[ 0.43966268 0.87932536 1.31898804 1.75865072 2.1983134 2.63797608
3.07763876 3.51730144 3.95696412 4.3966268 4.83628948 5.27595216
5.71561484 6.15527752 6.5949402 7.03460288 7.47426556 7.91392824
8.35359092 8.7932536 9.23291628 9.67257896 10.11224165 10.55190433
10.99156701 11.43122969 11.87089237 12.31055505 12.75021773 13.18988041
13.18988041 0. 1.12391865 2.04904177 2.75249233 3.37815396
3.96686921 4.53558282 5.09310744 5.64490943 6.19500187 6.74690367
7.30431746 7.87186621 8.45626406 9.06871626 9.73089387 10.49343203
11.51563061 13.74816848 0.85949953 nan nan nan
nan nan nan nan nan nan
nan 0. 1.48303468 2.8885035 3.74795106 4.46269741
5.11686874 5.74107193 6.35015955 6.95312861 7.55674416 8.16737947
8.79250326 9.44272463 10.13594268 10.90840157 11.85472483 13.39395428
13.37483292 nan nan
python计算的问题之一是它们很安静,通常是错误的这可能是为什么彼此不相等的原因之一您可以在此处找到有关它的更多信息:Is floating point math broken?
以上是关于matlab高手,在for循环中,设置每个循环的最大时间,如果超过该时间,跳出本次循环,进入下一次循环。的主要内容,如果未能解决你的问题,请参考以下文章