Stumped again... Converting Python to Matlab

조회 수: 13 (최근 30일)
Zach Dunagan
Zach Dunagan 2017년 10월 22일
댓글: Zach Dunagan 2017년 10월 24일
Here is the bit of Python code I am trying to convert to Matlab. code:
# thetas for each panel in global frame
theta=np.zeros((numPanels,1))
for i in range(numPanels):
theta[i]=np.arctan2(yp[i+1]-yp[i],xp[i+1]-xp[i])
output:
-2.63356
-2.92712
-2.98475
-3.01668
-3.03837
-3.05471
-3.06785
-3.07891
-3.08853
-3.09713
-3.10498
-3.11228
-3.11917
-3.12577
-3.13217
-3.13846
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.14159
3.13846
3.13217
3.12577
3.11917
3.11228
3.10498
3.09713
3.08853
3.07891
3.06785
3.05471
3.03837
3.01668
2.98475
2.92712
2.63356
0.508031
0.21447
0.156842
0.124909
0.103226
0.0868853
0.0737421
0.0626828
0.053059
0.0444609
0.0366123
0.0293165
0.0224263
0.0158261
0.00942088
0.00312805
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-0.00312805
-0.00942088
-0.0158261
-0.0224263
-0.0293165
-0.0366123
-0.0444609
-0.053059
-0.0626828
-0.0737421
-0.0868853
-0.103226
-0.124909
-0.156842
-0.21447
-0.508031
These are my Matlab arrays, where xpAdd = xp[i+1] and xp = xp[i]
xpAdd =
0.0600
0.0581
0.0562
0.0544
0.0525
0.0506
0.0488
0.0469
0.0450
0.0431
0.0412
0.0394
0.0375
0.0356
0.0319
0.0300
0.0281
0.0262
0.0244
0.0225
0.0206
0.0188
0.0169
0.0150
0.0131
0.0112
0.0094
0.0075
0.0056
0.0038
0.0019
0
-0.0019
-0.0038
-0.0056
-0.0075
-0.0094
-0.0112
-0.0131
-0.0150
-0.0169
-0.0188
-0.0206
-0.0225
-0.0244
-0.0262
-0.0281
-0.0300
-0.0319
-0.0338
-0.0356
-0.0375
-0.0394
-0.0412
-0.0431
-0.0450
-0.0469
-0.0488
-0.0506
-0.0525
-0.0544
-0.0562
-0.0581
-0.0600
-0.0600
-0.0581
-0.0562
-0.0544
-0.0525
-0.0506
-0.0488
-0.0469
-0.0450
-0.0431
-0.0412
-0.0394
-0.0375
-0.0356
-0.0319
-0.0300
-0.0281
-0.0262
-0.0244
-0.0225
-0.0206
-0.0188
-0.0169
-0.0150
-0.0131
-0.0112
-0.0094
-0.0075
-0.0056
-0.0038
-0.0019
0
0.0019
0.0038
0.0056
0.0075
0.0094
0.0112
0.0131
0.0150
0.0169
0.0188
0.0206
0.0225
0.0244
0.0262
0.0281
0.0300
0.0319
0.0338
0.0356
0.0375
0.0394
0.0412
0.0431
0.0450
0.0469
0.0488
0.0506
0.0525
0.0544
0.0562
0.0581
0.0600
xp' =
0.0600
0.0581
0.0562
0.0544
0.0525
0.0506
0.0488
0.0469
0.0450
0.0431
0.0412
0.0394
0.0375
0.0356
0.0319
0.0300
0.0281
0.0262
0.0244
0.0225
0.0206
0.0188
0.0169
0.0150
0.0131
0.0112
0.0094
0.0075
0.0056
0.0038
0.0019
0
-0.0019
-0.0038
-0.0056
-0.0075
-0.0094
-0.0112
-0.0131
-0.0150
-0.0169
-0.0188
-0.0206
-0.0225
-0.0244
-0.0262
-0.0281
-0.0300
-0.0319
-0.0338
-0.0356
-0.0375
-0.0394
-0.0412
-0.0431
-0.0450
-0.0469
-0.0488
-0.0506
-0.0525
-0.0544
-0.0562
-0.0581
-0.0600
Does anyone have an idea what this is doing theta[i]=np.arctan2(yp[i+1]-yp[i],xp[i+1]-xp[i]) ? I believe it's finding the angle and x1 and x2 points.

채택된 답변

Walter Roberson
Walter Roberson 2017년 10월 22일
It is finding the angle from (xp(i),yp(i)) to (xp(i+1),yp(i+1))
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 10월 22일
편집: Walter Roberson 2017년 10월 22일
Find the angles between what and each xp(i) and yp(i) ?
The code that is there now is answering the question: "if you are at (xp(i), yp(i)) and you want to go to (xp(i+1),yp(i+1)) then what angle do you need to travel?"
If that is not what you need to calculate, then please explain in words what you do need to calculate ?
If you are just looking for the MATLAB equivalent,
theta[i]=np.arctan2(yp[i+1]-yp[i],xp[i+1]-xp[i])
translates as
theta(i) = atan2(yp(i+1)-yp(i), xp(i+1)-xp(i));
and the code can be written without a loop as:
theta = atan2( diff(yp), diff(xp) );
When I glance at the python code I think it might possibly try to go past the end of the vector, but I am not certain as I do not use python.
Walter Roberson
Walter Roberson 2017년 10월 22일
편집: Walter Roberson 2017년 10월 24일
The MATLAB equivalent code is:
% thetas for each panel in global frame
theta = zeros(numPanels, 1);
for i = 1 : numPanels
theta(i) = atan2( yp(i+1)-yp(i), xp(i+1)-xp(i) );
end
If numPanels is not at least one less than length(xp) then this code will crash trying to go past the end of xp or yp. (You said you wanted the equivalent MATLAB code, not that you wanted correct code.)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Zach Dunagan
Zach Dunagan 2017년 10월 24일
편집: Walter Roberson 2017년 10월 24일
The only value that appears is the last one in the array.
xp(66:end) = flipud(xp(1:numPanels/2));
yp(66:end) = -flipud(yp(1:numPanels/2));
% collocation points
xc = zeros(numPanels, 1);
yc = zeros(numPanels, 1);
for k = numPanels
xc(k) = (xp(k) + xp(k+1))/2;
yc(k) = (yp(k) + yp(k+1))/2;
end
I have some code that will output the values, but I am trying to learn how to use these loops.
  댓글 수: 2
Walter Roberson
Walter Roberson 2017년 10월 24일
for k = 1 : numPanels
Zach Dunagan
Zach Dunagan 2017년 10월 24일
Thank you for the quick response.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by