0
$\begingroup$

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];
$\endgroup$
  • 2
    $\begingroup$ Why would sw = Table[30^2*(StandardDeviation[x]/Sqrt[11 - t])^2 + 10^2*(StandardDeviation[y]/Sqrt[t])^2, {t, 10, 1, -1}] not work? $\endgroup$ – J. M.'s discontentment Oct 11 '18 at 18:45
  • $\begingroup$ hmm It works properly but, how would i do it as as loop? $\endgroup$ – hcp Oct 11 '18 at 18:48
  • 3
    $\begingroup$ Easy: don't.. $\endgroup$ – AccidentalFourierTransform Oct 11 '18 at 19:55
3
$\begingroup$

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 *)
| improve this answer | |
$\endgroup$
2
$\begingroup$

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
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.