For loop while saving data in matrix

2 次查看(过去 30 天)
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 中查找有关 Data Type Identification 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by