Matlab for循环,Python中的不同值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Matlab for循环,Python中的不同值相关的知识,希望对你有一定的参考价值。

我在matlab中有代码,我正尝试将其“重写”为Python。问题在于for循环的值不同。不幸的是我找不到解决方案。

问题在每个数组中都有值:nu元素中的thetamuM i

在Python中,DTORRTOD是弧度,反之亦然。

对于两个代码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循环,Python中的不同值的主要内容,如果未能解决你的问题,请参考以下文章

for循环在matlab中不起作用

matlab中不同维度的cell合并,不用for循环应该怎么做

Python相当于在Matlab中分配给一个数组(在for循环内)

Matlab中for循环中的图例

matlab 如何将for每循环一次产生的矩阵合并成一个大矩阵

matlab用for循环求矩阵乘法