Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.
cancel
Showing results for 
Search instead for 
Did you mean: 
AmCh
Creator
Creator

Field generation within while loop

Hello everyone,

In a while loop I am supposed to calculate a value in each iteration.

As an example:

set b=2;

set a=0;

set n=100;

set delta=(b-a)/n;

do while t<b

let W=W+$(t)*$(t);

let t=t+delta;

Loop

 

For post-processing, I would like to store these values in a field to be able to display them later in a diagram. For the chart I would need dimension and expression.

Dimension=values of t

Expression=values of W

Can someone please help me?

Thank you very much in advance.

With kind regards



1 Solution

Accepted Solutions
edwin
Master II
Master II

try this:

set b=2;
set a=0;
set n=100;
set delta=(b-a)/n;

let W=0;
let t=0;

data:
load null() as t AutoGenerate(0);

do while t<b
let W=W+t*t;
let t=t+$(delta);
concatenate (data) load $(t) as t, $(W) as W AutoGenerate(1);
Loop

View solution in original post

9 Replies
edwin
Master II
Master II

assuming the loop is already working and that the t , delta, and w already exists as variables:

noconcatenate TABLE: load null() as t autogenerate(0);
do while t<b
let W=$(W)+$(t)*$(t);
let t=$(t)+$(delta);
concatenate(TABLE) load $(t) as t, $(W) as W autogenerate(1);
Loop

 when you use the variable i think you need to encapsulate it with $().  but you need to test it

edwin
Master II
Master II

this look the same as a different post

AmCh
Creator
Creator
Author

Thank you for your prompt response.

What do you mean with that t, delta and w already exist as variable?

t is an increment variable within the loop. It should not be pre-defined.

delta is already pre-defined as : set delta=(b-a)/n.

w should be created within the loop. It should not be pre-defined.

Another problem here that I couldn't increment with a non decimal value. (e.g. delta=0.5)

edwin
Master II
Master II

if your increment is by .5 from 0 to 4 for example, you can loop from 0 to 8 and just divide your counter by 2 to simulate a .5 increment

AmCh
Creator
Creator
Author

Thank you. It worked with the increment. But it didn't with the main problem; storing of variables within the while loop.
Can you help me further.
Thank you in advance.

edwin
Master II
Master II

when you say it didnt work, what didnt work?  was there an error message?  can you give a hint?

edwin
Master II
Master II

try this:

set b=2;
set a=0;
set n=100;
set delta=(b-a)/n;

let W=0;
let t=0;

data:
load null() as t AutoGenerate(0);

do while t<b
let W=W+t*t;
let t=t+$(delta);
concatenate (data) load $(t) as t, $(W) as W AutoGenerate(1);
Loop

AmCh
Creator
Creator
Author

Hi edwin,

thank you for your reply. Unfortunately this error messages appears:

'Field names must be unique within table'

Only two fields should be generated (t and W)
I want to generate rows in respective fields.

 

Any help please?
Thanks in advance.

edwin
Master II
Master II

@AmCh i ran the script and there weren't any errors.  this means you modified it and i get that.  if you still need help maybe you can share your script?