1
$\begingroup$

I did a loop (first of my life) for an iterative calculation. Is it possible to get all step values as a matrix. That means the "xValue" variable should be like {215.108,219.688,...} and not only the last calculed value of the loop.

The code :

Clear["Global`*"]

"Variables"

\[Phi] = 20.0;
n = 3;
\[Phi]' = 0.0;
n' = 0;
\[Phi]e = 12.0;
b = 220.0;
h = 500.0;
cnom = 30.0;
fcd = -20.0;
Es = 205000.0;
Ec = 34000.0;
Nd = -200.0;
Md = 99.60;
As = n*(\[Phi]/2.0)^2*Pi;
As' = n'*(\[Phi]'/2.0)^2*Pi;
Ac = b*h - As - As';
d = h - cnom - \[Phi]e - \[Phi]/2;
Z1 = 0.9*d;

(*SigmaS1=(Nd*1000)/As+(Md*10^6)/(As*Z1)
\[CurlyEpsilon]S1=SigmaS1/Es*)

"Solver function"

Sol[Z_] := NSolve[{((Nd*1000)/As + (Md*10^6)/(As*Z))/Es*Es*As - 
      0.5*b*x*Ec*(x*((Nd*1000)/As + (Md*10^6)/(As*Z))/Es)/(d - x) == 
     Nd*1000, x > 0}, x, Reals]; 

"Loop"

{xp, Zn, StopCondition, counter} = {x /. Sol[Z1][[1]], d - (x /. Sol[Z1][[1]])/3, 100, 0};
While[
StopCondition > 0.1/100,
{StopCondition = Abs[Zn - (d - (x /. Sol[Zn][[1]])/3)]; 
 Zn = d - (x /. Sol[Zn][[1]])/3; 
 counter = counter + 1; 
{iter = Print[StringForm["Calcul N\[Degree]``", counter]]; 
 xValue = 3*(d - Zn), Print[3*(d - Zn)]}
}
]


xValue

Calcul N[Degree]1

215.108

Calcul N[Degree]

219.688

Calcul N[Degree]3

218.764

Calcul N[Degree]4

218.949

Calcul N[Degree]5

218.912

Calcul N[Degree]6

218.92

Calcul N[Degree]7

218.918

Out[3044]= 218.918 (Here should be all the previous numbers as a matrix)

$\endgroup$
2
  • $\begingroup$ Suggest using NestWhile $\endgroup$
    – mikado
    Mar 27, 2020 at 17:18
  • $\begingroup$ Or NestWhileList. $\endgroup$ Mar 27, 2020 at 17:51

1 Answer 1

1
$\begingroup$

variable should be like {215.108,219.688,...}

You could do

xValue = First@Last@Reap@While[
     StopCondition > 
      0.1/100, {StopCondition = Abs[Zn - (d - (x /. Sol[Zn][[1]])/3)];
      Zn = d - (x /. Sol[Zn][[1]])/3;
      counter = counter + 1;
      {iter = Print[StringForm["Calcul N\[Degree]``", counter]];
       Sow[3*(d - Zn), Print[3*(d - Zn)]]}
      }
     ]

gives

 {215.108, 219.688, 218.764, 218.949, 218.912, 218.92, 218.918}
$\endgroup$
1
  • $\begingroup$ Exactly what I needed ! Big thanks ! $\endgroup$ Mar 27, 2020 at 17:55

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.