(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 7.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 19172, 454] NotebookOptionsPosition[ 18735, 434] NotebookOutlinePosition[ 19071, 449] CellTagsIndexPosition[ 19028, 446] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell[BoxData[ RowBox[{ RowBox[{"(*", RowBox[{ "Illustrative", " ", "example", " ", "of", " ", "Networked", " ", "Model", " ", "in", " ", "Optimisation"}], "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Remove", "[", "\"\\"", "]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"model", "=", RowBox[{"{", RowBox[{ RowBox[{"m", "\[Equal]", RowBox[{"x", "+", "y"}]}], ",", RowBox[{"n", "\[Equal]", RowBox[{ RowBox[{"3", RowBox[{"x", "^", "2"}]}], "-", RowBox[{"y", "^", "2"}]}]}]}], "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"eqns", "=", RowBox[{"{", RowBox[{ RowBox[{ RowBox[{"Sin", "[", "m", "]"}], " ", RowBox[{"Sin", "[", RowBox[{"2", "n"}], "]"}]}], ",", RowBox[{ RowBox[{ RowBox[{"m", "^", "2"}], "+", RowBox[{"n", "^", "2"}]}], "\[LessEqual]", "3"}], ",", RowBox[{ RowBox[{"-", "m"}], "\[LessEqual]", "0"}], ",", RowBox[{ RowBox[{"-", "n"}], "\[LessEqual]", "0"}]}], "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"varsidentify", "[", "eqns_", "]"}], ":=", RowBox[{"Select", "[", RowBox[{ RowBox[{"Union", "[", RowBox[{"Cases", "[", RowBox[{"eqns", ",", "x_Symbol", ",", "Infinity"}], "]"}], "]"}], ",", RowBox[{ RowBox[{ RowBox[{"!", RowBox[{"NumericQ", "[", "#", "]"}]}], "&&", RowBox[{"!", RowBox[{"MatchQ", "[", RowBox[{"#", ",", RowBox[{"True", "||", "False"}]}], "]"}]}]}], "&"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"vars", "=", RowBox[{"Complement", "[", RowBox[{ RowBox[{"varsidentify", "[", "model", "]"}], ",", RowBox[{"varsidentify", "[", "eqns", "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"lenvars", "=", RowBox[{"Length", "[", "vars", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"orig", "=", RowBox[{ RowBox[{"FindMinimum", "[", RowBox[{ RowBox[{"eqns", "/.", RowBox[{"(", RowBox[{"model", "/.", RowBox[{"{", RowBox[{"Equal", "\[Rule]", "Rule"}], "}"}]}], ")"}]}], ",", "vars"}], "]"}], "//", "Timing"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"modelext", "[", "vars1_", "]"}], ":=", RowBox[{ RowBox[{"modelext", "[", "vars1", "]"}], "=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"subst", "=", RowBox[{"Thread", "[", RowBox[{"vars", "\[Rule]", "vars1"}], "]"}]}], "}"}], ",", RowBox[{"Join", "[", RowBox[{"subst", ",", RowBox[{"Flatten", "[", RowBox[{"NSolve", "[", RowBox[{ RowBox[{"model", "/.", "subst"}], ",", RowBox[{"{", RowBox[{"m", ",", "n"}], "}"}]}], "]"}], "]"}]}], "]"}]}], "]"}]}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"eqnsext", "=", RowBox[{"eqns", "/.", RowBox[{"{", RowBox[{"LessEqual", "\[Rule]", "Subtract"}], "}"}]}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"leneqns", "=", RowBox[{"Length", "[", "eqnsext", "]"}]}], ";"}], "\n", RowBox[{ RowBox[{"varsopt", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"ToExpression", "[", RowBox[{ RowBox[{"ToString", "[", RowBox[{"vars", "[", RowBox[{"[", "i", "]"}], "]"}], "]"}], "<>", "\"\\""}], "]"}], ",", RowBox[{"{", RowBox[{"i", ",", "lenvars"}], "}"}]}], "]"}]}], ";"}], "\n", RowBox[{ RowBox[{"varsoptvals", "=", RowBox[{"Thread", "[", RowBox[{"{", RowBox[{"varsopt", ",", RowBox[{"Table", "[", RowBox[{"1", ",", RowBox[{"{", RowBox[{"i", ",", "lenvars"}], "}"}]}], "]"}]}], "}"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Scan", "[", RowBox[{ RowBox[{ RowBox[{"(", RowBox[{ RowBox[{ RowBox[{"\[Gamma]f", "[", "#", "]"}], "[", "vars1_", "]"}], ":=", RowBox[{ RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "}"}], ",", RowBox[{ RowBox[{"eqnsext", "[", RowBox[{"[", "#", "]"}], "]"}], "/.", RowBox[{"modelext", "[", "vars1", "]"}]}]}], "]"}], "/;", RowBox[{"VectorQ", "[", RowBox[{"vars1", ",", "NumberQ"}], "]"}]}]}], ")"}], "&"}], ",", RowBox[{"Range", "[", "leneqns", "]"}]}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"f0", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"i", "==", "1"}], ",", RowBox[{ RowBox[{"\[Gamma]f", "[", "i", "]"}], "[", "varsopt", "]"}], ",", RowBox[{ RowBox[{ RowBox[{"\[Gamma]f", "[", "i", "]"}], "[", "varsopt", "]"}], "\[LessEqual]", "0"}]}], "]"}], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "leneqns"}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"temp", "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"result", "=", RowBox[{ RowBox[{"Block", "[", RowBox[{ RowBox[{"{", RowBox[{"j", "=", "0"}], "}"}], ",", RowBox[{"FindMinimum", "[", RowBox[{"f0", ",", "varsoptvals", ",", "\[IndentingNewLine]", RowBox[{"StepMonitor", "\[RuleDelayed]", RowBox[{"(", RowBox[{ RowBox[{"j", "++"}], ";", RowBox[{"(*", RowBox[{ RowBox[{"Print", "[", RowBox[{"DownValues", "[", "modelext", "]"}], "]"}], ";"}], "*)"}], RowBox[{"NotebookDelete", "[", "temp", "]"}], ";", RowBox[{"temp", "=", RowBox[{"PrintTemporary", "[", "j", "]"}]}], ";", RowBox[{ RowBox[{"DownValues", "[", "modelext", "]"}], "=", RowBox[{"Last", "[", RowBox[{"DownValues", "[", "modelext", "]"}], "]"}]}]}], ")"}]}]}], "\[IndentingNewLine]", "]"}]}], "]"}], "//", "Timing"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"result", "[", RowBox[{"[", RowBox[{"2", ",", "2"}], "]"}], "]"}], "=", RowBox[{"Thread", "[", RowBox[{"vars", "\[Rule]", RowBox[{"(", RowBox[{"varsopt", "/.", RowBox[{"result", "[", RowBox[{"[", RowBox[{"2", ",", "2"}], "]"}], "]"}]}], ")"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", "result", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ "\"\<{\[CapitalDelta]Secs, \[CapitalDelta]Result, \[CapitalDelta]x, \ \[CapitalDelta]y} = \>\"", ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{ RowBox[{ RowBox[{"result", "[", RowBox[{"[", "1", "]"}], "]"}], "-", RowBox[{"orig", "[", RowBox[{"[", "1", "]"}], "]"}]}], ",", RowBox[{ RowBox[{"result", "[", RowBox[{"[", RowBox[{"2", ",", "1"}], "]"}], "]"}], "-", RowBox[{"orig", "[", RowBox[{"[", RowBox[{"2", ",", "1"}], "]"}], "]"}]}], ",", RowBox[{ RowBox[{"(", RowBox[{"x", "/.", RowBox[{"result", "[", RowBox[{"[", RowBox[{"2", ",", "2"}], "]"}], "]"}]}], ")"}], "-", RowBox[{"(", RowBox[{"x", "/.", RowBox[{"orig", "[", RowBox[{"[", RowBox[{"2", ",", "2"}], "]"}], "]"}]}], ")"}]}], ",", RowBox[{ RowBox[{"(", RowBox[{"y", "/.", RowBox[{"result", "[", RowBox[{"[", RowBox[{"2", ",", "2"}], "]"}], "]"}]}], ")"}], "-", RowBox[{"(", RowBox[{"y", "/.", RowBox[{"orig", "[", RowBox[{"[", RowBox[{"2", ",", "2"}], "]"}], "]"}]}], ")"}]}]}], "}"}]}], "]"}]}]}]], "Input", CellChangeTimes->{{3.488152828773046*^9, 3.488152839219531*^9}, { 3.488152869906364*^9, 3.488152890700267*^9}, {3.48815368030415*^9, 3.488153697485487*^9}, {3.48815376704011*^9, 3.48815379946153*^9}, { 3.488153875072453*^9, 3.488153878011444*^9}, {3.488154055340551*^9, 3.488154190688275*^9}, {3.488159732701612*^9, 3.488159773773628*^9}, { 3.488160809690794*^9, 3.488160829075785*^9}, {3.488160860158609*^9, 3.488160928925225*^9}, {3.488160964662687*^9, 3.488160965747283*^9}, { 3.488161078471402*^9, 3.488161087116296*^9}, {3.488161131447065*^9, 3.488161206674199*^9}, {3.488161236878439*^9, 3.488161263464173*^9}, { 3.488161293733321*^9, 3.488161613596637*^9}, {3.488161666283688*^9, 3.488161683790878*^9}, {3.48816172790268*^9, 3.48816173380856*^9}, { 3.488161792149901*^9, 3.488161861603634*^9}, {3.488161896212084*^9, 3.488162097380304*^9}, {3.488162138610059*^9, 3.48816218937289*^9}, { 3.488162220947345*^9, 3.488162480961873*^9}, {3.488162511556237*^9, 3.488162544668623*^9}, {3.488162585159173*^9, 3.488162769244315*^9}, { 3.488162849489762*^9, 3.488162917502938*^9}, {3.488162999813689*^9, 3.488163036267318*^9}, {3.48816306789073*^9, 3.488163084485381*^9}, { 3.488163216706006*^9, 3.488163288426447*^9}, {3.488163337065136*^9, 3.488163351159428*^9}, {3.488163404373029*^9, 3.488163434674093*^9}, { 3.488163505067366*^9, 3.488163516910789*^9}, {3.488163576254795*^9, 3.488163633628105*^9}, {3.488163752475203*^9, 3.488163760695365*^9}, { 3.488163830986162*^9, 3.488163872901542*^9}, {3.488163908265014*^9, 3.488163936766783*^9}, {3.488164013884897*^9, 3.488164039131821*^9}, { 3.488165767299913*^9, 3.488166062563949*^9}, {3.48816611379266*^9, 3.488166224411791*^9}, {3.488166260166237*^9, 3.48816629339324*^9}, { 3.488166364664813*^9, 3.488166390330184*^9}, 3.488166461189136*^9, { 3.488166501249358*^9, 3.488166555993029*^9}, {3.488166589981533*^9, 3.488166623815378*^9}, {3.488166735474114*^9, 3.488166839898821*^9}, { 3.488166897844627*^9, 3.488167035456434*^9}, {3.488167076701133*^9, 3.488167100796827*^9}, {3.488167138284591*^9, 3.488167152348075*^9}, { 3.488167218266209*^9, 3.488167227118542*^9}, {3.488167550229555*^9, 3.4881676238542*^9}, {3.488167912809048*^9, 3.488167935509601*^9}, { 3.488168001638679*^9, 3.488168150735692*^9}, {3.488168205754889*^9, 3.488168312166483*^9}, {3.48816834434721*^9, 3.488168412487352*^9}, { 3.488168447896151*^9, 3.488168500808737*^9}, {3.488168693126734*^9, 3.488168696470134*^9}, {3.488177349226063*^9, 3.48817735883372*^9}, { 3.488177422808529*^9, 3.488177426008125*^9}, {3.48817746371259*^9, 3.488177471051677*^9}, {3.488177554890125*^9, 3.488177635800288*^9}, { 3.488177682293587*^9, 3.488177712573388*^9}, {3.488177785193599*^9, 3.488177838357294*^9}, {3.488177882112368*^9, 3.488177888166883*^9}, { 3.488177935419082*^9, 3.488177962315979*^9}, {3.488178041868183*^9, 3.488178042309926*^9}, {3.488178144182285*^9, 3.488178221537583*^9}, { 3.488179392512215*^9, 3.488179420784089*^9}, {3.488179488484258*^9, 3.488179525291475*^9}, {3.488179560402274*^9, 3.488179576157701*^9}, { 3.488179612408983*^9, 3.488179627465931*^9}, {3.48818003121428*^9, 3.488180111199314*^9}, 3.488180332911243*^9, 3.488180447056493*^9, { 3.488180510477201*^9, 3.488180542769562*^9}, {3.488180670453169*^9, 3.488180761276573*^9}, 3.488180873126773*^9, {3.488180927901937*^9, 3.488180996430273*^9}, {3.488181039369623*^9, 3.488181120445569*^9}, { 3.488181175350385*^9, 3.488181396058149*^9}, {3.488181518982262*^9, 3.488181866454219*^9}, {3.488181904414436*^9, 3.488181914601302*^9}, { 3.488181948411749*^9, 3.488181973426874*^9}, {3.488182023294625*^9, 3.488182082245861*^9}, {3.488182133867897*^9, 3.488182326053703*^9}, 3.488182404595367*^9, {3.488182453910542*^9, 3.48818246626244*^9}, { 3.488182506910845*^9, 3.48818254504038*^9}, 3.488183440710873*^9, 3.488185316752452*^9, {3.488185421759569*^9, 3.488185513321268*^9}, { 3.488185550343886*^9, 3.488185590799313*^9}, {3.488186923315949*^9, 3.488187037134016*^9}, {3.488187109479909*^9, 3.488187150831567*^9}, { 3.488187319212473*^9, 3.488187350918208*^9}, {3.488187476282081*^9, 3.488187510606338*^9}, {3.488187608301988*^9, 3.488187697846164*^9}, 3.488187789050663*^9, {3.488187853472918*^9, 3.488187863148784*^9}, { 3.488188498826032*^9, 3.488188515272548*^9}, {3.488188569303011*^9, 3.488188569813759*^9}, {3.488188705720054*^9, 3.488188707757826*^9}, { 3.488188775798425*^9, 3.488188804711652*^9}, {3.488188839740065*^9, 3.488188876391613*^9}, {3.488188943051278*^9, 3.488188957631732*^9}, { 3.488188990574971*^9, 3.488188995654331*^9}, {3.488189058206444*^9, 3.488189155452753*^9}, {3.488189289348699*^9, 3.488189329574205*^9}, { 3.488189391919753*^9, 3.488189401557665*^9}, {3.488189446814927*^9, 3.488189465125058*^9}, {3.488189497405555*^9, 3.488189570716159*^9}, { 3.488189600815904*^9, 3.488189605558655*^9}, {3.488189655126892*^9, 3.488189699961304*^9}, {3.488189787288609*^9, 3.488189868507755*^9}, { 3.488189928985276*^9, 3.488189977434583*^9}, {3.488190177467489*^9, 3.488190201540476*^9}, {3.488190271158715*^9, 3.488190446618154*^9}, { 3.488190563811808*^9, 3.488190625186022*^9}, {3.488190686941419*^9, 3.488190731440255*^9}, {3.488190802911788*^9, 3.488190861239586*^9}, { 3.48819089761458*^9, 3.488190987956055*^9}, {3.488191052506571*^9, 3.488191060817257*^9}, {3.488191144026486*^9, 3.4881912938298*^9}, { 3.488191360080947*^9, 3.488191378785798*^9}, {3.488191444955288*^9, 3.488191486780383*^9}, {3.488220726312723*^9, 3.488220808462717*^9}, { 3.488220868727674*^9, 3.488220929173184*^9}, {3.488220990530686*^9, 3.488221016811134*^9}, {3.488221097650094*^9, 3.488221148471317*^9}, { 3.488221497137482*^9, 3.488221500970407*^9}, {3.48822157016196*^9, 3.488221571348107*^9}, {3.488221616263562*^9, 3.488221730185011*^9}, { 3.488221862751184*^9, 3.488221869739198*^9}, 3.488221928324274*^9, { 3.488221964429082*^9, 3.488221975971468*^9}, {3.488222059484682*^9, 3.488222060588296*^9}, {3.488222092628953*^9, 3.48822225120389*^9}, { 3.488222292048432*^9, 3.48822230310619*^9}, {3.48894628159811*^9, 3.48894632356577*^9}, {3.488946366500351*^9, 3.488946412160969*^9}, { 3.488946493644923*^9, 3.48894650871081*^9}, {3.488946540854634*^9, 3.488946544669639*^9}, {3.488946580628899*^9, 3.488946607571713*^9}, { 3.488949746569002*^9, 3.488949748852689*^9}, {3.488949867564547*^9, 3.488949869359483*^9}, {3.488949917444443*^9, 3.488949943823461*^9}, { 3.489372248375422*^9, 3.489372279347445*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{"0.1360090000000318`", ",", RowBox[{"{", RowBox[{ RowBox[{"-", "0.08863586384872957`"}], ",", RowBox[{"{", RowBox[{ RowBox[{"x", "\[Rule]", "0.7762215532800059`"}], ",", RowBox[{"y", "\[Rule]", RowBox[{"-", "0.3565423839787972`"}]}]}], "}"}]}], "}"}]}], "}"}]], "Output", CellChangeTimes->{ 3.488221731308457*^9, 3.488221871593581*^9, {3.488221966400533*^9, 3.488221977613813*^9}, 3.488222123561441*^9, 3.488222221627502*^9, { 3.488222254519371*^9, 3.488222272157168*^9}, 3.488237293743711*^9, { 3.488946278026834*^9, 3.488946286972128*^9}, 3.488946327754598*^9, 3.488946415176918*^9, 3.488946546422451*^9, {3.488946588937225*^9, 3.488946608460512*^9}, {3.488946704673808*^9, 3.488946715517743*^9}, 3.488949751776441*^9, {3.488949888915901*^9, 3.488949933492561*^9}, 3.489372293504184*^9}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"{", RowBox[{"0.2520159999999354`", ",", RowBox[{"{", RowBox[{ RowBox[{"-", "0.08863500802129771`"}], ",", RowBox[{"{", RowBox[{ RowBox[{"x", "\[Rule]", "0.7762203042876419`"}], ",", RowBox[{"y", "\[Rule]", RowBox[{"-", "0.35653713590434083`"}]}]}], "}"}]}], "}"}]}], "}"}]], "Print", CellChangeTimes->{ 3.488221731503306*^9, 3.488221871884473*^9, {3.48822196672494*^9, 3.488221977765672*^9}, 3.488222123805888*^9, 3.488222221871061*^9, { 3.488222254746252*^9, 3.488222272333678*^9}, 3.488237293923322*^9, { 3.488946278207449*^9, 3.488946287148774*^9}, 3.488946327864117*^9, 3.488946415186828*^9, 3.488946546828892*^9, {3.488946589213994*^9, 3.488946608874376*^9}, {3.488946705044031*^9, 3.488946715875981*^9}, 3.488949753265878*^9, {3.488949889527886*^9, 3.488949934140108*^9}, 3.489372294300711*^9}], Cell[BoxData[ InterpretationBox[ RowBox[{"\<\"{\[CapitalDelta]Secs, \[CapitalDelta]Result, \[CapitalDelta]x, \ \[CapitalDelta]y} = \"\>", "\[InvisibleSpace]", RowBox[{"{", RowBox[{"0.1160069999999036`", ",", "8.558274318531955`*^-7", ",", RowBox[{"-", "1.2489923639780187`*^-6"}], ",", "5.2480744563432324`*^-6"}], "}"}]}], SequenceForm[ "{\[CapitalDelta]Secs, \[CapitalDelta]Result, \[CapitalDelta]x, \ \[CapitalDelta]y} = ", {0.1160069999999036, 8.558274318531955*^-7, -1.2489923639780187`*^-6, 5.2480744563432324`*^-6}], Editable->False]], "Print", CellChangeTimes->{ 3.488221731503306*^9, 3.488221871884473*^9, {3.48822196672494*^9, 3.488221977765672*^9}, 3.488222123805888*^9, 3.488222221871061*^9, { 3.488222254746252*^9, 3.488222272333678*^9}, 3.488237293923322*^9, { 3.488946278207449*^9, 3.488946287148774*^9}, 3.488946327864117*^9, 3.488946415186828*^9, 3.488946546828892*^9, {3.488946589213994*^9, 3.488946608874376*^9}, {3.488946705044031*^9, 3.488946715875981*^9}, 3.488949753265878*^9, {3.488949889527886*^9, 3.488949934140108*^9}, 3.489372294307411*^9}] }, Open ]] }, Open ]] }, WindowSize->{1206, 655}, WindowMargins->{{0, Automatic}, {Automatic, 0}}, FrontEndVersion->"7.0 for Linux x86 (32-bit) (February 25, 2009)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[567, 22, 15154, 338, 561, "Input"], Cell[15724, 362, 911, 20, 31, "Output"], Cell[CellGroupData[{ Cell[16660, 386, 910, 20, 24, "Print"], Cell[17573, 408, 1134, 22, 28, "Print"] }, Open ]] }, Open ]] } ] *) (* End of internal cache information *)