0

My program doesn't hold the value in a variable as I want it. I want to attach the calculated value as a list in a variable. What is wrong here?

t = 10
While[t >= 1,
   sw = 30^2*(StandardDeviation[x]/Sqrt[11 - t])^2 + 10^2*(StandardDeviation[y]/Sqrt[t])^2; 
  t = t - 1];
xzczd
  • 65,995
  • 9
  • 163
  • 468
hcp
  • 203
  • 1
  • 4

2 Answers2

3

J.M. and AccidentalFourierTransform have already shown you the standard answer in the comment, but if you still insist on making your loop work, you can use the following:

Clear@sw
sw = {}; (sw = a_) ^:= (sw := #) &[sw~Join~{a}]

t = 10
While[t >= 1, 
 sw = 30^2 (StandardDeviation[x]/Sqrt[11 - t])^2 + 10^2 (StandardDeviation[y]/Sqrt[t])^2;
  t = t - 1]

sw == Table[
  30^2 (StandardDeviation[x]/Sqrt[11 - t])^2 + 10^2 (StandardDeviation[y]/Sqrt[t])^2, {t,
    10, 1, -1}]
(* True *)
xzczd
  • 65,995
  • 9
  • 163
  • 468
2

try this then..

t = 10;
s = {};
While[t >= 1, 
sw = 30^2*(StandardDeviation[x]/Sqrt[11 - t])^2 + 
10^2*(StandardDeviation[y]/Sqrt[t])^2; AppendTo[s, sw];
t--];
s
ZaMoC
  • 6,697
  • 11
  • 31