Help finding error in Matlab code

I have two sets of code, one in Python and the other in Matlab. I am converting Python to Matlab, but ran into a error that I am having a difficult time trouble shooting. There erros is...
Index exceeds matrix dimensions.
Error in unSteadyTEvortexShedding (line 167)
[xVV(i, j), yVV(i, j)] = VortexPointOnPt(xVorPos(i),
yVorPos(i), xVorPos(j), yVorPos(j), 0);
I have attached the VortexPointOnPt function file as well. The indexing is occurring at the while loop with k or the for loops?

댓글 수: 4

To help with debugging, you need to format the code in unSteadTEvortexShedding. It's hard to determine which loop is starting where because the end statements aren't aligned. I see a recycling of for loop variables, which could cause the error.
Where is the end of this if/else statement?
for i = 1:t
for j = 1:t
if i == j
xVV(i, j) = yVV(i,j) == 0;
else
...
wakePanelStr = 0.1;
xpWake1 = xp(end);
Try not to override counter variables. Example, this for loop seems to be within another for loop with the same i counter variable
for i = 1:t
for j = 1:t
...
for i = 1:numPanels %THIS "i" replaced the other "i"
[tWP(i), nWP(i)] = VortexPointOnPt(xc(i), yc(i), xcWake, ycWake, theta(i));
What are these "end" ending in your function?
end
end
end
end
end
end
end
end
end
end
end
end
Jan
Jan 2017년 11월 13일
편집: Jan 2017년 11월 14일
This will not the source of errors, but
filepath = savepath('C:/FramesMatlab')
saves the folders of the current Matlab path to a pathdef.m file in the folder 'C:/FramesMatlab'. This is meaningless.
Zach Dunagan
Zach Dunagan 2017년 11월 14일
편집: Zach Dunagan 2017년 11월 14일
Both of you make some great points. Jan, what is the Matlab equivalent to that of Python?
Donald, I got the code to run. Thank you for your suggestions. I was just not thinking clearly.
Jan
Jan 2017년 11월 14일
편집: Jan 2017년 11월 14일
@Zach: The equivalent of what in Python? All I see is a meaningless Matlab command:
filepath = savepath('C:/FramesMatlab')
But I cannot know, which Python command you want to replace. Perhaps:
pathName='C://Users//Zach Dunagan//Desktop//frames//'
Then:
filepath = 'C:/FramesMatlab';

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

답변 (2개)

Jan
Jan 2017년 11월 14일
편집: Jan 2017년 11월 14일

0 개 추천

You define xVorPos and yVorPos:
xVorPos = zeros(numFrames, 1);
yVorPos = zeros(numFrames, 1);
Then you run a loop:
for t = 1:numFrames % for each time frame
Inside this loop you check
if t > 0
repeatedly, but this is impossible, when the loop runs from 1:numFrames. Omit this tests to reduce clutter. You access:
for j =1:t
... xVorPos(j), yVorPos(j)
before the failing line successfully. Therefor I cannot imagine, why
for i = 1:t
for j = 1:t
if i == j
xVV(i, j) = yVV(i,j) == 0;
else
[xVV(i, j), yVV(i, j)] = VortexPointOnPt(xVorPos(i), yVorPos(i), ...
xVorPos(j), yVorPos(j), 0);
fails some lines later.
What is the purpose of:
xVV(i, j) = yVV(i,j) == 0;
This sets xVV(i,j) to 1, if yVV(i,j) is 0, and to 1 otherwise. Is this wanted? If so, it is strange, because you have defined
xVV = zeros(t, t);
yVV = zeros(t, t);
explicitly. Or do you want to set xVV(i,j) and yVV(i,j) both to zeros? If so, omit this, because the zeros set this value already.
There are so many strange and confusing commands in your code, that I think the most efficient way to debug it is to delete it and rewrite in in Matlab from scratch. Digging in the mud does not produce reliable and clean code. You cannot write an English article by typing it in German, use Google's auto-translator and fix the errors afterwards.

댓글 수: 1

Zach Dunagan
Zach Dunagan 2017년 11월 14일
편집: Zach Dunagan 2017년 11월 14일
I told Donald I found the bug in the previous comment.
The code I am referring to is... pathName='C://Users//Zach Dunagan//Desktop//frames//'
You said perhaps it would be... filepath = 'C:/FramesMatlab'
Thank you.

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

Walter Roberson
Walter Roberson 2017년 11월 14일

0 개 추천

As I described in a previous question, in Python, setting pathName is just setting a variable, with no side effects and no special meaning. It is just another variable name. It is also unused in the code you posted, so you can simply delete the line.

댓글 수: 7

Zach Dunagan
Zach Dunagan 2017년 11월 14일
편집: Walter Roberson 2017년 11월 14일
Walter this is what I am trying to do.
From whom I am working under. "It's a string that is the path for where to save the images. So you should have a similar string variable in your matlab code that is used when you save the images."
Can someone help me with the last set of if else statements between the Python and Matlab code?
This is what I recently changed it to...
% calculate wake panel strength
if k == 1
if t == 1
wakePanelStr = -x(end) * perimeter
else
wakePanelStr = -x(end) * perimeter - sum(vortStrength(1:t));
end
else
abs(currCircSum) > 10^(-14)
wakePanelStr = wakePanelStr - currCircSum / ((currCircSum - prevCircSum) / (wakeStrSto(k, t) - wakeStrSto(k - 1, t)));
end
I am getting an error at the end saying Error using reshape To RESHAPE the number of elements must not change. I attached a new script file because I added more code to the end.
Walter Roberson
Walter Roberson 2017년 11월 14일
"It's a string that is the path for where to save the images. "
But nothing in that code saves images.
else
abs(currCircSum) > 10^(-14)
should be
elseif abs(currCircSum) > 10^(-14)
Zach Dunagan
Zach Dunagan 2017년 11월 15일
What about the reshape error?
Walter Roberson
Walter Roberson 2017년 11월 15일
You are not passing nPanels in to PanelCoorToNTCoor() so it is not obvious to me that the second output of that routine (which I do not see the code for) will have nPanels rows.
Zach Dunagan
Zach Dunagan 2017년 11월 15일
편집: Zach Dunagan 2017년 11월 15일
I manage to figure it out.
Zach Dunagan
Zach Dunagan 2017년 11월 16일
편집: Zach Dunagan 2017년 11월 20일
I found the bug.

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

카테고리

도움말 센터File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

질문:

2017년 11월 11일

편집:

2017년 11월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by