Effacer les filtres
Effacer les filtres

For loop while saving data in matrix

1 vue (au cours des 30 derniers jours)
Navin
Navin le 11 Fév 2014
Modifié(e) : the cyclist le 11 Fév 2014
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.

Réponse acceptée

the cyclist
the cyclist le 11 Fév 2014
Modifié(e) : the cyclist le 11 Fév 2014
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)

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by