Help me with indexing please?

3 次查看(过去 30 天)
Zach Dunagan
Zach Dunagan 2017-10-21
编辑: Stephen23 2017-10-22
Trying to convert Python to Matlab. Below is the Python code along with the output.
import numpy as np
import matplotlib.pyplot as plt
import copy
# setup panel coordinates and control point coordinates
AoA=10*np.pi/180
Uinf=8.8
rho=1.204
chord=0.12
span=chord*2
thickness=0.006
minRad=thickness/2
majRad=chord/4
numPanels=128
xp=np.zeros((numPanels+1,1))
yp=np.zeros((numPanels+1,1))
# panel coordinates for a finite thickness plate with elliptical leading edges (nodes)
xStep=chord/(numPanels/2)
xp[0]=chord/2
yp[0]=0
i=1
while i<numPanels/8:
xp[i]=chord/2-xStep*i
yp[i]=-minRad/majRad*(majRad**2-(majRad-xStep*i)**2)**(1/2)
i=i+1
xp =
0.06
0.058125
0.05625
0.054375
0.0525
0.050625
0.04875
0.046875
0.045
0.043125
0.04125
0.039375
0.0375
0.035625
0.03375
0.031875
yp =
0
-0.00104396
-0.00145237
-0.00174888
-0.00198431
-0.00217855
-0.00234187
-0.00248039
-0.00259808
-0.00269766
-0.00278107
-0.00284975
-0.00290474
-0.00294679
-0.00297647
-0.00299413
Here is my Matlab Code with the output.
% clears variables and windows
clc, clear all
% user defined variables
AoA = 10*pi/180;
Uinf = 8.8;
rho = 1.204;
chord = 0.12;
span = chord*2;
thickness = 0.006;
minRad = thickness/2;
majRad = chord/4;
numPanels = 128;
xp = zeros(numPanels, 1);
yp = zeros(numPanels, 1);
% panel coordinates for a finite thickness plate with elliptical leading
% edge (nodes)
xStep = chord/(numPanels/2);
k = 1;
while k < numPanels/9
xp(k) = chord/2*k;
yp(k) = 0*k;
xp(k) = chord/2 - xStep*k;
yp(k) = -minRad/majRad*(majRad^2-(majRad-xStep*k)^2)^(1/2);
k = k + 1;
end
xp =
0.0581250000000000
0.0562500000000000
0.0543750000000000
0.0525000000000000
0.0506250000000000
0.0487500000000000
0.0468750000000000
0.0450000000000000
0.0431250000000000
0.0412500000000000
0.0393750000000000
0.0375000000000000
0.0356250000000000
0.0337500000000000
yp =
-0.00104395581803063
-0.00145236875482778
-0.00174888357245415
-0.00198431348329844
-0.00217855313224167
-0.00234187424939940
-0.00248039185412305
-0.00259807621135332
-0.00269765523186341
-0.00278107443266087
-0.00284975327879450
-0.00290473750965556
-0.00294679380853157
-0.00297647022494767
I'd like the output of Matlab to match the output of Python.

回答(2 个)

Stephen23
Stephen23 2017-10-21
编辑:Stephen23 2017-10-21
There is no point writing MATLAB code as if it were Python (or C++) with lots of loops. MATLAB is a high-level language, so forget about those ugly loops and learn how to write neat and simple vectorized code:
AoA = 10*pi/180;
Uinf = 8.8;
rho = 1.204;
chord = 0.12;
span = chord*2;
thickness = 0.006;
minRad = thickness/2;
majRad = chord/4;
numPanels = 128;
% calculate:
xStep = chord/(numPanels/2);
vec = xStep*(0:numPanels/8-1);
xp = chord/2 - vec;
yp = -minRad/majRad * sqrt(majRad^2-(majRad-vec).^2);
and checking that it gives the same output as the Python code:
>> xp(:)
ans =
0.06
0.058125
0.05625
0.054375
0.0525
0.050625
0.04875
0.046875
0.045
0.043125
0.04125
0.039375
0.0375
0.035625
0.03375
0.031875
>> yp(:)
ans =
0
-0.00104395581803063
-0.00145236875482778
-0.00174888357245415
-0.00198431348329844
-0.00217855313224167
-0.0023418742493994
-0.00248039185412305
-0.00259807621135332
-0.00269765523186341
-0.00278107443266087
-0.0028497532787945
-0.00290473750965556
-0.00294679380853157
-0.00297647022494766
-0.00299413489175087
By the way, one of the main reasons your code does not work is because you did not take into account the differences between zero-based and one-based indexing when you calculated with the index.
  3 个评论
Stephen23
Stephen23 2017-10-21
编辑:Stephen23 2017-10-22
@Zach Dunagan: well that is easy: look at every location that you use the indexing (this is easy in the MATLAB editor because of the variable highlighting), and you will realize that some of them need to have 1 subtracted from them (hint: everywhere that you multiply the index with something).
But MATLAB has quite different best-practices than Python. Using a loop is not good use of MATLAB for solving this task, because vectorized code is much simpler, is more efficient, and avoids the whole issue of indexing entirely. Also it is best to avoid while loops if the number of iterations can be clearly defined before the loop: in your case a for loop would be a more standard use of MATLAB.
If you really want to learn how to write MATLAB code then forget the loop. Note that my answer, simpler and neater without any loop, already gives the answer that you are supposedly looking for.
Zach Dunagan
Zach Dunagan 2017-10-21
Thank you for your suggestions.

请先登录,再进行评论。


KL
KL 2017-10-21
编辑:KL 2017-10-21
Change the part after you define xStep with the following code
xp_0 = chord/2;
yp_0 = 0;
k = 1;
while k < numPanels/9
xp(k) = chord/2 - xStep*k;
yp(k) = -minRad/majRad*(majRad^2-(majRad-xStep*k)^2)^(1/2);
k = k + 1;
end
xp = [xp_0; xp]
yp = [yp_0; yp]
I have just created xp_0 and yp_0 before the loop, ran the loop from k=1 with just 2 equations (just like in python) and finally put the _0 values on top of the resultant arrays.
and the better way is to avoid the loop,
xp_0 = chord/2;
yp_0 = 0;
k = 1:floor(numPanels/9);
xp(k) = chord/2 - xStep.*k;
yp(k) = -minRad/majRad*sqrt(majRad^2-(majRad-xStep.*k).^2);
xp = [xp_0; xp]
yp = [yp_0; yp]

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by