function xdot = dfpek(t,x)
global Pm f H E Y th ngg
Pe=zeros(1, ngg);
for ii = 1:ngg
for jj = 1:ngg
Pe(ii) = Pe(ii) + E(ii)*E(jj)*Y(ii, jj)*cos(th(ii, jj)-x(ii)+x(jj));
end, end
for k=1:ngg
xdot(k)=x(k+ngg);
xdot(k+ngg)=(pi*f)/H(k)*(Pm(k)-Pe(k));
end
xdot=xdot';

답변 (2개)

Stephen23
Stephen23 2016년 2월 17일
편집: Stephen23 2016년 2월 17일

0 개 추천

Solution
Put this before your third for-loop:
xdot = NaN(1,2*ngg);
This is called array preallocation. Do this in all of your code.
Other Advice
Indent your code properly. The way you are indenting your code is non-standard and makes it very hard to follow. Putting lots of end's on one line is not cunning or smart. This is a pain to read:
for ii = 1:ngg
for jj = 1:ngg
Pe(ii) = Pe(ii) + E(ii)*E(jj)*Y(ii, jj)*cos(th(ii, jj)-x(ii)+x(jj));
end, end
Instead you should use MATLAB's default code indentation, so that the for and end are aligned:
for ii = 1:ngg
for jj = 1:ngg
Pe(ii) = Pe(ii) + E(ii)*E(jj)*Y(ii, jj)*cos(th(ii, jj)-x(ii)+x(jj));
end
end
Avoid using global variables. Learn to pass variables reliably:

댓글 수: 1

Image Analyst
Image Analyst 2016년 2월 17일
If your code's indentation is all messed up, type Control-a (to select all text in the window) then type control-i (to standardize/fix the indenting).

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

Walter Roberson
Walter Roberson 2016년 2월 17일

0 개 추천

When your global variable "ngg" is less than 1 or is empty, your loops will not execute at all and your xdot() will not be defined.

카테고리

도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2016년 2월 17일

답변:

2016년 2월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by