필터 지우기
필터 지우기

For loop while saving data in matrix

조회 수: 1 (최근 30일)
Navin
Navin 2014년 2월 11일
편집: the cyclist 2014년 2월 11일
Good day i'm quite new to mathlab and i'm having a bit of problems. I'm not new to programming but i am new to matlab and for some reason i can't get past this problem. Any help would be greatly appreciated. I am trying to have a for loop which starts at 1 and decriments by 0.01 and stops at 0.0001 in this loop i need values to be stored in an array for both variable, Tind and s. S changes in the loop and would be the same values as the loop itself which is from 1 to 0.0001 with a decriment of 0.01.. After storing both of these values in a matrix i would then plot them. Below is my code. I'm a bit new so my apologies if my code is a bit nooby.
for k=[1:-0.01:0.0001]
s(k)= k
ws=1800*((2*pi)/60)
Vth=255.2
R2=.332
Rth=0.59
Xth=1.106
X2=0.464
a=1/ws
b=(3*Vth.^2*(R2/s))
c=((Rth+(R2/s)).^2+(Xth+X2).^2)
Tind(k)=a*(b/c)
end
Plot(Tind,s)
I get the following error
Attempted to access s(0.99); index must be a positive integer or logical.
Error in test (line 2) s(k)= k
Any help would be greatly appreciated. Thank You.

채택된 답변

the cyclist
the cyclist 2014년 2월 11일
편집: the cyclist 2014년 2월 11일
The main problem with your code is that you are trying to use a real-valued variable as an index, but indices have to be 1,2,3 ....
Here is some code that stays true to your looping structure:
Vth=255.2;
R2=.332;
Rth=0.59;
Xth=1.106;
X2=0.464;
ws = 1800*((2*pi)/60);
a = 1/ws;
kRange = 1:-0.01:0.0001;
numberOfK = numel(kRange);
for nk= 1:numberOfK
s(nk) = kRange(nk);
b = (3*Vth.^2*(R2/s(nk)));
c = ((Rth+(R2/s(nk))).^2+(Xth+X2).^2);
Tind(nk) = a*(b/c);
end
figure
plot(Tind,s)
Even better, though, is to take advantage of the fact that MATLAB is vectorized:
Vth=255.2;
R2=.332;
Rth=0.59;
Xth=1.106;
X2=0.464;
ws = 1800*((2*pi)/60);
a = 1/ws;
s = 1:-0.01:0.0001;
b = (3*Vth.^2*(R2./s));
c = ((Rth+(R2./s)).^2+(Xth+X2).^2);
Tind = a*(b./c);
figure
plot(Tind,s)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

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

Community Treasure Hunt

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

Start Hunting!

Translated by