{ From user Lonnie, Model Iterate_demonstratio at Thu, Nov 29, 2007 11:06 AM~~ } Softwareversion 4.0.1 { System Variables with non-default values: } Time := 0..10 {!40000|Att_previndexvalue Time: [0,1,2]} Typechecking := 1 Checking := 1 Saveoptions := 2 Savevalues := 0 Model Iterate_demonstratio Title: Iterate Demonstration Description: Analyica User Group Webinar, 29 Nov 2007 Author: Lonnie Chrisman, Ph.D.~ Lumina Decision Systems Date: Thu, Nov 29, 2007 8:58 AM Saveauthor: Lonnie Savedate: Thu, Nov 29, 2007 11:06 AM Defaultsize: 48,24 Diagstate: 1,1,0,568,297,17 Windstate: 2,40,20,476,224 Fontstyle: Arial, 15 Fileinfo: 0,Model Iterate_demonstratio,2,2,0,0,W:\TestModels\Iterate D~~ emonstration.ANA Module Balance_sheet Title: Balance Sheet Defaultsize: 48,24 Nodelocation: 96,48,1 Nodesize: 48,24 Diagstate: 1,159,55,604,390,17 Variable Non_debt_liabilities Title: Non-debt liabilities Definition: 680 Nodelocation: 200,48,1 Nodesize: 48,24 Variable Debt Title: Debt Definition: Iterate( ~ 0,~ debt + Balance_check, ~ abs(Balance_check) < 0.01,~ 100,~ true~ ) Nodelocation: 200,112,1 Nodesize: 48,24 Variable Interest_payment Title: Interest Payment Definition: Debt * Interest_rate Nodelocation: 200,184,1 Nodesize: 48,24 Constant Interest_rate Title: Interest rate Definition: 10% Nodelocation: 80,184,1 Nodesize: 48,24 Variable Retained_earnings Title: Retained earnings Definition: 200 - Interest_payment Nodelocation: 200,256,1 Nodesize: 48,24 Variable All_other_assets Title: All other assets Definition: 800 Nodelocation: 200,328,1 Nodesize: 48,24 Variable Total_assets Title: Total Assets Definition: Retained_earnings + All_other_assets Nodelocation: 352,288,1 Nodesize: 48,24 Variable Total_liabilities Title: Total Liabilities Definition: Non_debt_liabilities + Debt Nodelocation: 352,80,1 Nodesize: 48,24 Variable Balance_check Title: Balance Check Definition: Total_assets - Total_liabilities Nodelocation: 496,184,1 Nodesize: 48,24 Valuestate: 2,40,219,416,303,0,MIDM Numberformat: 2,F,4,2,0,0,4,0,$,0,"ABBREV",0 Close Balance_sheet Module Population_growth Title: Population Growth Defaultsize: 48,24 Nodelocation: 336,48,1 Nodesize: 48,24 Diagstate: 1,55,111,745,356,17 Index Age Title: Age Definition: ['Young','Adult'] Nodelocation: 80,48,1 Nodesize: 48,24 {!40000|Att_previndexvalue: ['Young','Adult']} Index Next_age Title: Next Age Definition: copyindex(Age) Nodelocation: 200,48,1 Nodesize: 48,24 {!40000|Att_previndexvalue: ['Young','Adult']} Variable L Title: Leslie Matrix Definition: Table(Age,Next_age)(~ 0,1,~ 1,1~ ) Nodelocation: 200,120,1 Nodesize: 48,24 Reformdef: [Next_age,Age] Variable Initial_population Title: Initial Population Definition: Table(Age)(1,1) Nodelocation: 88,192,1 Nodesize: 48,24 Decision Population Title: Population Definition: Pop_iteration[Pop_items='Population'] Nodelocation: 336,192,1 Nodesize: 48,24 Valuestate: 2,494,39,416,303,0,MIDM Variable Next_population Title: Next Population Definition: sum( Population * L, age )[ Next_age=age] Nodelocation: 456,192,1 Nodesize: 48,24 Variable Age_ratio Title: Age ratio Definition: Population[Age='Adult'] / Population[Age='Young'] Nodelocation: 336,256,1 Nodesize: 48,24 Valuestate: 2,574,26,416,303,0,MIDM Reformval: [Self,Age] Numberformat: 2,D,14,2,0,0,14,0,$,0,"ABBREV",0 {!40000|Att_previndexvalue: [(Age='Young')]} Variable Next_age_ratio Title: Next Age ratio Definition: Next_population[Age='Adult'] / Next_population[Age='Young'~~ ] Nodelocation: 456,256,1 Nodesize: 48,24 Reformval: [Self,Age] {!40000|Att_previndexvalue: [(Age='Young')]} Decision Ll_population Title: LL population Definition: Pop_iteration[Pop_items='Collection'] Nodelocation: 336,120,1 Nodesize: 48,24 Variable Pop_iteration Title: Pop iteration Definition: Iterate(~ Array(Pop_items,[Initial_population,NULL]),~ Array(Pop_items,[Next_population, for a:=Age do Ll_push( Ll_populatio~~ n[Age=a], Population[Age=a])] ),~ abs(Age_ratio-Next_age_ratio)<1e-14) Nodelocation: 208,248,1 Nodesize: 48,24 Valuestate: 2,319,145,416,303,0,MIDM Reformval: [Age,Pop_items] Index Pop_items Title: Pop items Definition: ['Population','Collection'] Nodelocation: 88,248,1 Nodesize: 48,24 Library Linked_list_library Title: Linked List Library Description: Contains routines for manipulating a linked list.~ The simplest linked list is just NULL -- a linked list with zero elem~~ ents.~ Any other linked list is a reference to a record indexed by Linked_Li~~ st with each cell containing a reference, to the element, and to the ~~ remainder.~ ~ Use the LL_Push to build the list.~ After a list is built, the easiest way to use and view it is to conve~~ rt it back to an array using LL_to_RArray.~ ~ For example, assume you have a function IsPrime(n).~ This collects all the primes between 2 and n:~ ~ var LL := null;~ for i:=n..2 do begin~ if IsPrime(i) then LL := LL_Push(LL,i) /* Build the list */~ end;~ LL_to_Array( LL, "primes" ) /* Convert to an array for easy use */ Author: Lonnie Chrisman Date: Mon, Aug 18, 2003 11:53 PM Saveauthor: Lonnie Savedate: Wed, Sep 05, 2007 3:41 PM Defaultsize: 48,24 Nodelocation: 480,48,1 Nodesize: 48,32 Nodeinfo: 1,1,1,1,1,1,0,0,0,0 Diagstate: 1,354,40,527,421,17 Windstate: 2,102,90,669,534 Index Linked_list Title: Linked List Description: Record definition of a linked list. Item contains a refe~~ rence to the element. Next contains the remainder of the linked list~~ , which is either NULL or a reference to something indexed by Linked_~~ List. Definition: ['Item','Next'] Nodelocation: 176,32,1 Nodesize: 48,24 Function Ll_push(LL,x) Title: LL Push Description: Pushes x on the front of the linked list LL and returns t~~ he new LL. Definition: \Array(Linked_List,[\x,LL]) Nodelocation: 176,96,1 Nodesize: 48,24 Windstate: 2,20,466,476,224 Paramnames: LL,x Function Ll_to_array(LL,indexName:atomic) Title: LL To Array Description: Converts a linked list to an array. ~ The array is indexed by a single local index having the name indicate~~ d by the second parameter. Definition: var len := LL_Length(LL);~ if len=0 then~ NULL~ else begin~ index J / indexName :=1..LL_Length(LL);~ for i:=J Do (~ Var x:=LL_First(LL);~ LL := LL_Remove_First(LL);~ x~ )~ end Nodelocation: 288,160,1 Nodesize: 52,24 Windstate: 2,426,58,593,411 Paramnames: LL,indexName Function Ll_length(LL:atomic) Title: LL Length Description: Returnst the # of elements in a linked list. Definition: var len:=0;~ while (IsReference(LL)) begin~ LL := subscript(#LL,Linked_List,"Next");~ len := len + 1~ end;~ len Nodelocation: 176,160,1 Nodesize: 48,24 Windstate: 2,142,87,476,224 Paramnames: LL Function Ll_first(LL) Title: LL First Description: Returns the first element of a non-empty linked list. Definition: #Subscript(#LL,Linked_List,"Item") Nodelocation: 176,224,1 Nodesize: 48,24 Paramnames: LL Function Ll_remove_first(LL) Title: LL Remove First Description: Returns a linked list resulting from the removal of the f~~ irst element of the list. Definition: Subscript(#LL,Linked_List,"Next") Nodelocation: 288,224,1 Nodesize: 48,24 Paramnames: LL Function Ll_to_rarray(LL,indexName:atomic) Title: LL to RArray Description: Converts a linked list to an array but with elements in r~~ everse order (last in LL first in array).~ This is often useful when collecting things during an iteration, wher~~ e pushing them on the front is most convenient, so that the linked li~~ st is actually in reverse order. When done, this retrieves the colle~~ ction as an Analytica array in the forward iteration order, as if eac~~ h item had been appended during the iteration. Definition: var len := LL_Length(LL);~ index J / indexName :=1..len;~ Slice( for i:=J Do (~ Var x:=LL_First(LL);~ LL := LL_Remove_First(LL);~ x~ ), J, len-J+1) Nodelocation: 288,96,1 Nodesize: 48,24 Windstate: 2,102,90,631,415 Paramnames: LL,indexName Function Ll_nth(LL ; n:atomic positive) Title: LL Nth Description: Returns the Nth element in a linked list. ~ Note that this operation is not efficient -- if you are going to do i~~ t many times, you may be better off converting to an array first. El~~ ement n=1 is the first element. If n does not correspond to an eleme~~ nt, NULL is returned. Definition: while Isreference(LL) and n>1 Do begin~ n:=n-1; ~ LL:=LL_Remove_First(LL)~ end;~ if Isreference(LL) then LL_First(LL)~ else NULL Nodelocation: 176,288,1 Nodesize: 48,24 Windstate: 2,445,158,472,331 Paramnames: LL,n Module Ll_example_usage Title: Example usage Description: This example shows a linked list of 'fruits'. It current~~ ly exercises all the functions of the 'linked list library'. Author: Fred & Lonnie Date: Mon, Feb 28, 2005 2:48 PM Saveauthor: Lonnie Savedate: Wed, Mar 16, 2005 10:00 AM Defaultsize: 48,24 Nodelocation: 408,80,1 Nodesize: 48,24 Nodeinfo: 1,1,1,1,1,1,0,0,0,0 Diagstate: 1,377,93,664,512,17 Windstate: 2,44,283,476,224 Diagramcolor: 52427,65535,62258 Variable First_tweaked_call Title: First Tweaked Definition: Ll_first( The_ll ) Nodelocation: 336,232,1 Nodesize: 72,16 Valuestate: 2,40,50,416,303,0,MIDM Variable Fruit_0 Title: Fruit 0 Definition: 'pineapple' Nodelocation: 488,272,1 Nodesize: 48,24 Defnstate: 2,43,180,416,303,0,MIDM Nodecolor: 65535,57341,1 Variable Fruit_1 Title: Fruit 1 Definition: 'apple' Nodelocation: 104,240,1 Nodesize: 48,24 Defnstate: 2,49,215,416,303,0,MIDM Valuestate: 2,56,66,416,303,0,MIDM Nodecolor: 65535,1,1 Variable Fruit_2 Title: Fruit 2 Definition: 'banana' Nodelocation: 104,176,1 Nodesize: 48,24 Defnstate: 2,368,120,416,303,0,MIDM Valuestate: 2,275,145,416,303,0,MIDM Nodecolor: 65535,65531,1 Variable Fruit_3 Title: Fruit 3 Definition: 'cranberry' Nodelocation: 104,112,1 Nodesize: 48,24 Nodecolor: 52427,1,1 Variable Fruit_4 Title: Fruit 4 Definition: 'durian' Nodelocation: 104,48,1 Nodesize: 48,24 Defnstate: 2,215,222,416,303,0,MIDM Valuestate: 2,72,82,416,303,0,MIDM Nodecolor: 32770,65535,1 Variable Length_call Title: Length Definition: Ll_length( The_ll ) Nodelocation: 336,152,1 Nodesize: 72,16 Valuestate: 2,56,66,416,303,0,MIDM Variable Ll_to_array1 Title: LL to Array Definition: Ll_to_array( The_ll, "Fruits" ) Nodelocation: 336,72,1 Nodesize: 72,16 Valuestate: 2,69,66,416,303,0,MIDM Variable Ll_to_rarray_call Title: LL to RArray Definition: Ll_to_rarray( The_ll, "Fruits") Nodelocation: 336,112,1 Nodesize: 72,16 Valuestate: 2,314,224,416,303,0,MIDM Variable The_ll Title: The LL Definition: var LL := LL_Push(null,Fruit_4);~ LL := LL_Push(LL,Fruit_3);~ LL := LL_Push(LL,Fruit_2);~ LL_Push(LL,Fruit_1) Nodelocation: 104,304,1 Nodesize: 48,24 Valuestate: 2,40,50,416,303,0,MIDM Variable Push_function_call Title: Push function call Definition: Ll_push( The_ll, Fruit_0 ) Nodelocation: 336,272,1 Nodesize: 72,16 Valuestate: 2,213,154,416,303,0,MIDM Variable Remove_call Title: Remove Definition: Ll_remove_first( The_ll ) Nodelocation: 336,312,1 Nodesize: 68,16 Valuestate: 2,88,98,416,303,0,MIDM Variable Ll_nth_call Title: LL Nth Definition: Ll_nth( The_ll, 3) Nodelocation: 336,192,1 Nodesize: 68,16 Valuestate: 2,40,50,416,303,0,MIDM Function Isprime(n : atomic numeric) Title: IsPrime Description: Returns true if n is a prime. Definition: if n<2 then false~ else if n=2 then true ~ else if Mod(n,2)=0 then false~ else begin~ var k:=3;~ var last := sqrt(n);~ var factorFound := false;~ while ( not factorFound and k<=last ) (~ if Mod(n,k)=0 then factorFound:=true;~ k := k+2;~ );~ not factorFound~ end Nodelocation: 104,392,1 Nodesize: 48,24 Paramnames: n Function First_n_primes(N : atomic positive) Title: First N Primes(N) Description: Returns a list of the first N primes.~ This is an example demonstrating how the linked list library might be~~ used. Definition: var nfound := 1;~ var LL := LL_Push(null,2);~ var i:=3;~ /* Builds a linked list of the primes found so far */~ while (nfoundxmax then xmax else x Nodelocation: 536,32,1 Nodesize: 48,24 Paramnames: xmin,x,xmax Close Supply_demand Module Simulation_of_dynami Title: Simulation of Dynamic Defaultsize: 48,24 Nodelocation: 456,48,1 Nodesize: 48,24 Diagstate: 1,135,25,720,501,17 Decision K Title: Spring constant Definition: 0.5 Nodelocation: 72,104,1 Nodesize: 56,24 Decision M Title: Mass Definition: 0.5 Nodelocation: 336,112,1 Nodesize: 56,24 Decision Damping Title: Damping constant Definition: 0.5 Nodelocation: 592,192,1 Nodesize: 56,24 Decision X0 Title: X0 Definition: 0.5 Nodelocation: 224,416,1 Nodesize: 48,24 Decision V0 Title: V0 Definition: -2 Nodelocation: 456,416,1 Nodesize: 48,24 Constant Dt Title: dt Definition: 0.02 Nodelocation: 240,48,1 Nodesize: 48,24 Index T Title: T Definition: Sequence( 0, 10, Dt ) Nodelocation: 464,48,1 Nodesize: 48,24 {!40000|Att_previndexvalue: [0,0.01,0.02,0.03,0.04,0.05,0.06,0.07000000000000001~~ ,0.08,0.09,0.1,0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18,0.19,0.2,0.21,~~ 0.22,0.23,0.24,0.25,0.26,0.27,0.28,0.29,0.3,0.31,0.32,0.33,0.34,0.35,~~ 0.36,0.37,0.38,0.39,0.4,0.41,0.42,0.43,0.44,0.45,0.46,0.47,0.48,0.49,~~ 0.5,0.51,0.52,0.53,0.54,0.55,0.5600000000000001,0.57,0.58,0.59,0.6,0.61~~ ,0.62,0.63,0.64,0.65,0.66,0.67,0.6800000000000001,0.69,0.7,0.71,0.72,~~ 0.73,0.74,0.75,0.76,0.77,0.78,0.79,0.8,0.8100000000000001,0.82,0.83,~~ 0.84,0.85,0.86,0.87,0.88,0.89,0.9,0.91,0.92,0.9300000000000001,0.94,0.95~~ ,0.96,0.97,0.98,0.99,1,1.01,1.02,1.03,1.04,1.05,1.06,1.07,1.08,1.09,1.1~~ ,1.11,1.12,1.13,1.14,1.15,1.16,1.17,1.18,1.19,1.2,1.21,1.22,1.23,1.24~~ ,1.25,1.26,1.27,1.28,1.29,1.3,1.31,1.32,1.33,1.34,1.35,1.36,1.37,1.38~~ ,1.39,1.4,1.41,1.42,1.43,1.44,1.45,1.46,1.47,1.48,1.49,1.5,1.51,1.52~~ ,1.53,1.54,1.55,1.56,1.57,1.58,1.59,1.6,1.61,1.62,1.63,1.64,1.65,1.66~~ ,1.67,1.68,1.69,1.7,1.71,1.72,1.73,1.74,1.75,1.76,1.77,1.78,1.79,1.8,~~ 1.81,1.82,1.83,1.84,1.85,1.86,1.87,1.88,1.89,1.9,1.91,1.92,1.93,1.94,~~ 1.95,1.96,1.97,1.98,1.99,2,2.01,2.02,2.03,2.04,2.05,2.06,2.07,2.08,2.09~~ ,2.1,2.11,2.12,2.13,2.14,2.15,2.16,2.17,2.18,2.19,2.2,2.21,2.22,2.23,~~ 2.24,2.25,2.26,2.27,2.28,2.29,2.3,2.31,2.32,2.33,2.34,2.35,2.36,2.37~~ ,2.38,2.39,2.4,2.41,2.42,2.43,2.44,2.45,2.46,2.47,2.48,2.49,2.5,2.51,~~ 2.52,2.53,2.54,2.55,2.56,2.57,2.58,2.59,2.6,2.61,2.62,2.63,2.64,2.65,~~ 2.66,2.67,2.68,2.69,2.7,2.71,2.72,2.73,2.74,2.75,2.76,2.77,2.78,2.79,~~ 2.8,2.81,2.82,2.83,2.84,2.85,2.86,2.87,2.88,2.89,2.9,2.91,2.92,2.93,2.94~~ ,2.95,2.96,2.97,2.98,2.99,3,3.01,3.02,3.03,3.04,3.05,3.06,3.07,3.08,~~ 3.09,3.1,3.11,3.12,3.13,3.14,3.15,3.16,3.17,3.18,3.19,3.2,3.21,3.22,3.23~~ ,3.24,3.25,3.26,3.27,3.28,3.29,3.3,3.31,3.32,3.33,3.34,3.35,3.36,3.37~~ ,3.38,3.39,3.4,3.41,3.42,3.43,3.44,3.45,3.46,3.47,3.48,3.49,3.5,3.51,~~ 3.52,3.53,3.54,3.55,3.56,3.57,3.58,3.59,3.6,3.61,3.62,3.63,3.64,3.65,~~ 3.66,3.67,3.68,3.69,3.7,3.71,3.72,3.73,3.74,3.75,3.76,3.77,3.78,3.79,~~ 3.8,3.81,3.82,3.83,3.84,3.85,3.86,3.87,3.88,3.89,3.9,3.91,3.92,3.93,~~ 3.94,3.95,3.96,3.97,3.98,3.99,4,4.01,4.02,4.03,4.04,4.05,4.06,4.07,4.08~~ ,4.09,4.1,4.11,4.12,4.13,4.14,4.15,4.16,4.17,4.18,4.19,4.2,4.21,4.22,~~ 4.23,4.24,4.25,4.26,4.27,4.28,4.29,4.3,4.31,4.32,4.33,4.34,4.35,4.36,~~ 4.37,4.38,4.39,4.4,4.41,4.42,4.43,4.44,4.45,4.46,4.47,4.48,4.49,4.5,4.51~~ ,4.52,4.53,4.54,4.55,4.56,4.57,4.58,4.59,4.6,4.61,4.62,4.63,4.64,4.65~~ ,4.66,4.67,4.68,4.69,4.7,4.71,4.72,4.73,4.74,4.75,4.76,4.77,4.78,4.79~~ ,4.8,4.81,4.82,4.83,4.84,4.85,4.86,4.87,4.88,4.89,4.9,4.91,4.92,4.93,~~ 4.94,4.95,4.96,4.97,4.98,4.99,5]} Variable Spring_force Title: Spring force Definition: -K * X Nodelocation: 224,192,1 Nodesize: 48,24 Variable X Title: X Definition: X_history[@T=CurT] Nodelocation: 224,312,1 Nodesize: 48,24 Variable V Title: V Definition: V_history[@T=Curt] Nodelocation: 456,312,1 Nodesize: 48,24 Variable A Title: A Definition: (Spring_force+Damping_force) / M Nodelocation: 336,192,1 Nodesize: 48,24 Variable Damping_force Title: Damping force Definition: -Damping * V Nodelocation: 456,192,1 Nodesize: 48,24 Variable The_iteration Title: The iteration Definition: Iterate(~ ( X_History := if @T=1 then X0 else null ;~ V_History := if @T=1 then V0 else null;~ curT := 1), ~ ~ ( var nextX := X + V*dt;~ var nextV := V + A*dt;~ curT := curT+1;~ X_History := if @T=curT then nextX else X_History;~ V_History := if @T=curT then nextV else V_History),~ ~ curT >= size(T) ) Nodelocation: 336,376,1 Nodesize: 48,24 Windstate: 2,102,90,476,363 Variable X_history Title: X history Definition: ComputedBy( The_iteration ) Nodelocation: 112,312,1 Nodesize: 48,24 Valuestate: 2,284,110,585,382,1,MIDM Variable V_history Title: V history Definition: ComputedBy(The_iteration) Nodelocation: 576,312,1 Nodesize: 48,24 Variable Curt Title: curT Definition: ComputedBy(The_iteration) Nodelocation: 464,104,1 Nodesize: 48,24 Close Simulation_of_dynami Close Iterate_demonstratio