%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%22marimo%3E%3D0.23.9%22%2C%20%22numpy%3E%3D2.0%22%2C%20%22matplotlib%3E%3D3.9%22%2C%20%22pandas%3E%3D2.2%22%2C%20%22scipy%3E%3D1.14%22%2C%20%22sympy%3E%3D1.13%22%2C%20%22urllib3%3E%3D2.2%22%2C%20%22ipython%22%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Optimization%0A%0A%20%20%20%20%3E%20Marcos%20Duarte%2C%20Renato%20Naville%20Watanabe%3Cbr%3E%0A%20%20%20%20%3E%20%5BLaboratory%20of%20Biomechanics%20and%20Motor%20Control%5D(https%3A%2F%2Fbmclab.pesquisa.ufabc.edu.br%2F)%3Cbr%3E%0A%20%20%20%20%3E%20Federal%20University%20of%20ABC%2C%20Brazil%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20How%20to%20use%20this%20guide%0A%0A%20%20%20%20A%20net%20joint%20moment%20tells%20you%20what%20the%20muscles%20accomplish%20together%2C%20but%20often%20not%20how%20they%20share%20the%20work.%20Optimization%20adds%20a%20stated%20preference%20among%20feasible%20solutions.%20We%20first%20practice%20on%20a%20box%20and%20a%20one-dimensional%20function%2C%20then%20see%20how%20different%20preferences%20produce%20different%20muscle%20forces.%0A%0A%20%20%20%20Read%20the%20examples%20in%20order%2C%20predict%20each%20result%2C%20and%20then%20run%20the%20cell.%20Try%20the%20distributed%20Exercises%20before%20opening%20their%20worked%20solutions.%20Finish%20with%20the%20checkpoint%20questions%20and%20independent%20Problems%3B%20their%20panels%20contain%20final%20answers%20only.%0A%0A%20%20%20%20**Challenge%200.**%20Three%20muscles%20can%20balance%20one%20measured%20elbow%20moment.%20Would%20a%20unique%20numerical%20answer%20prove%20that%20the%20nervous%20system%20used%20those%20exact%20forces%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Python%20setup%0A%0A%20%20%20%20NumPy%2C%20SciPy%2C%20SymPy%2C%20pandas%2C%20Matplotlib%2C%20and%20IPython%20support%20the%20examples.%20The%20elbow%20geometry%2Fforce%20tables%20are%20public%20repository%20assets%20loaded%20by%20%60repo_file%60.%20Solver%20results%20depend%20on%20their%20units%2C%20bounds%2C%20and%20stated%20objective%3B%20Internet%20access%20is%20needed%20only%20if%20those%20files%20are%20not%20present%20locally.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%3Cdiv%20style%3D%22text-align%3A%20right%22%3E%0A%20%20%20%20%3Ci%3EIf%20there%20occur%20some%20changes%20in%20nature%2C%20the%20amount%20of%20action%20necessary%20for%20this%20change%20must%20be%20as%20small%20as%20possible.%3C%2Fi%3E%0A%20%20%20%20%3Cbr%3EMaupertuis%20(sec%20XVIII)%0A%20%20%20%20%3C%2Fdiv%3E%0A%0A%20%20%20%20**Optimization%20is%20the%20process%20of%20finding%20the%20best%20value%20from%20possible%20alternatives%20with%20regards%20to%20a%20certain%20criteria**%20(%5BWikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMathematical_optimization)).%0A%0A%20%20%20%20Typically%2C%20such%20best%20value%20is%20the%20value%20that%20maximizes%20or%20minimizes%20the%20criteria.%20In%20this%20context%2C%20to%20solve%20a%20(mathematical)%20optimization%20problem%20is%20to%20find%20the%20maximum%20or%20minimum%20(which%20need%20not%20be%20a%20stationary%20point%20when%20it%20lies%20on%20a%20boundary)%20of%20a%20function%20(and%20we%20can%20use%20maximum%20or%20minimum%20interchangeably%20because%20the%20maximum%20of%20a%20function%20is%20the%20minimum%20of%20the%20negative%20of%20that%20function).%3Cbr%3E%0A%20%20%20%20To%20solve%20an%20optimization%20problem%2C%20we%20first%20have%20to%20model%20the%20problem%20and%20define%20the%20objective%2C%20the%20variables%2C%20and%20the%20constraints%20of%20the%20problem.%20In%20optimization%2C%20these%20terms%20are%20usually%20defined%20as%3A%0A%0A%20%20%20%201.%20Objective%20function%20(or%20also%2C%20cost%2C%20loss%2C%20utility%2C%20or%20fitness%20function)%3A%20a%20function%20describing%20what%20we%20want%20to%20optimize.%0A%20%20%20%202.%20Design%20variable(s)%3A%20variables%20that%20will%20be%20manipulated%20to%20optimize%20the%20cost%20function.%0A%20%20%20%203.%20Constraint%20functions%3A%20a%20set%20of%20constraints%2C%20equalities%20or%20inequalities%20that%20constrains%20the%20possible%20solutions%20to%20possible%20values%20of%20the%20design%20variables%20(candidate%20solutions%20or%20feasible%20solutions%20or%20feasible%20set).%0A%0A%20%20%20%20A%20feasible%20solution%20that%20minimizes%20(or%20maximizes)%20the%20objective%20function%20is%20called%20an%20optimal%20solution.%0A%0A%20%20%20%20The%20optimization%20problem%20is%20the%20calculation%20of%20the%20minimum%20or%20maximum%20values%20of%20an%20objective%20function%20over%20a%20set%20of%20**unknown**%20possible%20values%20of%20the%20design%20variables.%3Cbr%3E%0A%20%20%20%20Even%20in%20case%20of%20a%20finite%20number%20of%20possible%20values%20of%20the%20objective%20function%20and%20design%20variables%20(e.g.%2C%20after%20discretization%20and%20a%20manual%20or%20a%20grid%20search)%2C%20in%20general%20the%20evaluation%20of%20the%20objective%20function%20is%20computationally%20expensive%20and%20should%20be%20avoided.%3Cbr%3E%0A%20%20%20%20Of%20note%2C%20even%20if%20there%20is%20no%20other%20option%2C%20random%20search%20can%20outperform%20grid%20search%20in%20some%20high-dimensional%20parameter%20searches%3B%20this%20is%20not%20a%20universal%20guarantee.%20See%20%5BBergstra%2C%20Bengio%20(2012)%5D(http%3A%2F%2Fjmlr.csail.mit.edu%2Fpapers%2Fvolume13%2Fbergstra12a%2Fbergstra12a.pdf).%0A%0A%20%20%20%20A%20typical%20problem%20of%20optimization%3A%20%5BKnapsack%20problem%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FKnapsack_problem).%0A%0A%20%20%20%20Read%20more%20about%20that%20in%20%5BIntroduction%20to%20Optimization%5D(https%3A%2F%2Fneos-guide.org%2Fguide%2F)%20from%20the%20%20%5BNEOS%20Guide%5D(http%3A%2F%2Fneos-guide.org%2F).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Some%20jargon%20in%20mathematical%20optimization%0A%0A%20%20%20%20-%20**Linear%20versus%20nonlinear%20optimization**%3A%20linear%20optimization%20refers%20to%20when%20the%20objective%20function%20and%20the%20constraints%20are%20linear%20mathematical%20functions.%20For%20linear%20objectives%20over%20a%20convex%20feasible%20set%2C%20every%20local%20minimum%20is%20global.%20If%20a%20finite%20optimum%20exists%20on%20a%20polytope%2C%20at%20least%20one%20vertex%20attains%20it%3B%20unbounded%20or%20infeasible%20problems%20need%20separate%20treatment.%20See%20%5BWikipedia%201%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FLinear_programming)%20and%20%5BWikipedia%202%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FNonlinear_programming).%0A%20%20%20%20-%20**Constrained%20versus%20unconstrained%20optimization**%3A%20unconstrained%20optimization%20has%20no%20constraints%3B%20constrained%20optimization%20restricts%20the%20feasible%20set.%0A%20%20%20%20-%20**Convex%20optimization**%3A%20the%20field%20of%20optimization%20that%20deals%20with%20finding%20the%20minimum%20of%20convex%20functions%20(or%20the%20maximum%20of%20concave%20functions)%20over%20a%20convex%20constraint%20set.%20The%20convexity%20of%20a%20function%20facilitates%20the%20optimization%20because%20a%20local%20minimum%20must%20be%20a%20global%20minimum%20and%20first-order%20conditions%20(the%20first%20derivatives)%20are%20sufficient%20conditions%20for%20finding%20the%20optimal%20solution.%20Note%20that%20although%20convex%20optimization%20is%20a%20particular%20case%20of%20nonlinear%20optimization%2C%20it%20is%20a%20relatively%20simple%20optimization%20problem%2C%20with%20robust%20and%20mature%20methods%20of%20solution.%20See%20%5BWikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FConvex_optimization).%0A%20%20%20%20-%20**Multivariate%20optimization**%3A%20optimization%20of%20a%20function%20of%20several%20variables.%0A%20%20%20%20-%20**Multimodal%20optimization**%3A%20optimization%20of%20a%20function%20with%20several%20local%20minima%20to%20find%20the%20multiple%20(locally)%20optimal%20solutions%2C%20as%20opposed%20to%20a%20single%20best%20solution.%0A%20%20%20%20-%20**Multi-objective%20optimization**%3A%20optimization%20involving%20more%20than%20one%20objective%20function%20to%20be%20optimized%20simultaneously.%0A%20%20%20%20-%20**Optimal%20control**%3A%20finding%20a%20control%20law%20for%20a%20given%20system%20such%20that%20a%20certain%20optimality%20criterion%20is%20achieved.%20See%20%5BWikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FOptimal_control).%0A%20%20%20%20-%20**Quadratic%20programming**%3A%20optimization%20of%20a%20quadratic%20function%20subject%20to%20linear%20constraints.%20See%20%5BWikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FQuadratic_programming).%0A%20%20%20%20-%20**Simplex%20algorithm**%3A%20linear%20optimization%20algorithm%20that%20begins%20at%20a%20starting%20vertex%20and%20moves%20along%20the%20edges%20of%20the%20polytope%20(the%20feasible%20region)%20until%20it%20reaches%20the%20vertex%20of%20the%20optimum%20solution.%20See%20%5BWikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSimplex_algorithm).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%201.**%20For%20the%20box%20problem%2C%20identify%20the%20design%20variable%2C%20objective%2C%20and%20geometric%20constraint%20before%20differentiating.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%201%0A%20%20%20%20The%20cut%20size%20x%20is%20the%20variable.%20Volume%20is%20%24V%3Dx(a-2x)%5E2%24.%20A%20nondegenerate%20box%20requires%20%240%3Cx%3Ca%2F2%24.%20Checking%20stationary%20points%20without%20checking%20this%20domain%20can%20produce%20an%20impossible%20box.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Maxima%20and%20minima%0A%0A%20%20%20%20In%20mathematics%2C%20the%20maximum%20and%20minimum%20of%20a%20function%20are%20the%20largest%20and%20smallest%20values%20that%20the%20function%20takes%20at%20a%20point%20either%20within%20a%20neighborhood%20(local)%20or%20on%20the%20function%20entire%20domain%20%20(global)%20(%5BWikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMaxima_and_minima)).%0A%0A%20%20%20%20For%20a%20function%20of%20one%20variable%2C%20if%20the%20maximum%20or%20minimum%20of%20a%20function%20is%20not%20at%20the%20limits%20of%20the%20domain%20and%20if%20at%20least%20the%20first%20and%20second%20derivatives%20of%20the%20function%20exist%2C%20a%20maximum%20and%20minimum%20can%20be%20found%20as%20the%20point%20where%20the%20first%20derivative%20of%20the%20function%20is%20zero.%20If%20the%20second%20derivative%20on%20that%20point%20is%20positive%2C%20then%20it's%20a%20minimum%2C%20if%20it%20is%20negative%2C%20it's%20a%20maximum.%0A%0A%20%20%20%20%3Cdiv%20class%3D'center-align'%3E%3Cfigure%20aria-describedby%3D%22figure-caption-1%22%3E%3Cimg%20src%3D'https%3A%2F%2Fgithub.com%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fimages%2Fmaxmin.png%3Fraw%3D1'%20width%3D350%20alt%3D'minima%20and%20maxima%20of%20a%20function'%2F%3E%20%20%3C%2Ffigure%3E%3C%2Fdiv%3E%0A%0A%20%20%20%20%3Cspan%20id%3D%22figure-caption-1%22%20class%3D%22figure-caption%22%20style%3D%22display%3Ablock%3Btext-align%3Acenter%3Bfont-style%3Aitalic%22%3EFigure.%20Maxima%20and%20minima%20of%20a%20function%20of%20one%20variable.%3C%2Fspan%3E%0A%0A%20%20%20%20-%20Note%20that%20the%20requirement%20that%20the%20second%20derivative%20on%20the%20extremum%20to%20be%20positive%20for%20a%20minimum%20or%20negative%20for%20a%20maximum%20is%20sufficient%20but%20not%20a%20necessary%20condition.%20For%20instance%2C%20the%20function%20%24f(x)%3Dx%5E4%24%20has%20an%20extremum%20in%20%24x%3D0%24%20since%20%24f'(x)%3D4x%5E3%24%20and%20%24f'(0)%3D0%24%2C%20but%20its%20second%20derivative%20at%20%24x%3D0%24%20is%20also%20zero%3A%20%24f''(x)%3D12x%5E2%3B%5C%3A%20f''(0)%3D0%24.%20If%20the%20first%20nonzero%20derivative%20after%20the%20first%20is%20of%20even%20order%2C%20a%20positive%20sign%20gives%20a%20local%20minimum%20and%20a%20negative%20sign%20gives%20a%20local%20maximum%3B%20an%20odd%20order%20gives%20neither%3A%20%24f''''(0)%3D24%24%3B%20the%20extremum%20is%20a%20minimum.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Let's%20now%20apply%20optimization%20to%20solve%20a%20problem%20with%20a%20univariate%20function.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20import%20Python%20libraries%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20%23%20'%25matplotlib%20inline'%20command%20supported%20automatically%20in%20marimo%0A%20%20%20%20import%20matplotlib%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20sympy%20as%20sym%0A%20%20%20%20from%20sympy.plotting%20import%20plot%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20IPython.display%20import%20display%0A%20%20%20%20from%20IPython.core.display%20import%20Math%0A%0A%20%20%20%20return%20Math%2C%20display%2C%20np%2C%20pd%2C%20plot%2C%20plt%2C%20sym%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Example%201%3A%20Maximum%20volume%20of%20a%20cardboard%20box%0A%0A%20%20%20%20We%20want%20to%20make%20a%20box%20from%20a%20square%20cardboard%20with%20side%20%24a%24%20such%20that%20its%20volume%20should%20be%20maximum.%3Cbr%3E%0A%20%20%20%20What%20is%20the%20optimal%20distance%20where%20the%20square%20cardboard%20should%20be%20cut%20and%20folded%20to%20make%20a%20box%20with%20maximum%20volume%3F%0A%0A%20%20%20%20%3Cdiv%20class%3D'center-align'%3E%3Cfigure%20aria-describedby%3D%22figure-caption-2%22%3E%3Cimg%20src%3D'https%3A%2F%2Fgithub.com%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fimages%2Fbox.png%3Fraw%3D1'%20width%3D450%20alt%3D'box%20optimization'%2F%3E%20%20%3C%2Ffigure%3E%3C%2Fdiv%3E%0A%0A%20%20%20%20%3Cspan%20id%3D%22figure-caption-2%22%20class%3D%22figure-caption%22%20style%3D%22display%3Ablock%3Btext-align%3Acenter%3Bfont-style%3Aitalic%22%3EFigure.%20A%20box%20to%20be%20made%20from%20a%20cardboard%20such%20that%20its%20volume%20should%20be%20maximum.%20Where%20we%20should%20cut%3F%3C%2Fspan%3E%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20If%20the%20distance%20where%20to%20cut%20and%20fold%20the%20cardboard%20is%20%24b%24%2C%20see%20figure%20above%2C%20the%20volume%20of%20the%20box%20will%20be%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bl%20l%7D%0A%20%20%20%20V(b)%20%3D%20b(a-2b)(a-2b)%20%5C%5C%0A%20%20%20%20%5C%5C%0A%20%20%20%20V(b)%20%3D%20a%5E2b%20-%204ab%5E2%20%2B%204b%5E3%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20In%20the%20context%20of%20optimization%3A%3Cbr%3E%0A%20%20%20%20**The%20expression%20for%20%24V%24%20is%20the%20cost%20function%2C%20%24b%24%20is%20the%20design%20variable%2C%20and%20the%20constraint%20is%20that%20feasible%20values%20of%20%24b%24%20are%20in%20the%20interval%20%24%5D0%2C%20%5Cdfrac%7Ba%7D%7B2%7D%5B%24%2C%20i.e.%2C%20%24b%3E0%24%20and%20%24b%3C%5Cdfrac%7Ba%7D%7B2%7D%24.**%0A%0A%20%20%20%20The%20first%20and%20second%20derivatives%20of%20%24V%24%20w.r.t.%20%24b%24%20are%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bl%20l%7D%0A%20%20%20%20%5Cdfrac%7B%5Cmathrm%7Bd%7DV%7D%7B%5Cmathrm%7Bd%7Db%7D%20%3D%20a%5E2%20-%208ab%20%2B%2012b%5E2%20%5C%5C%0A%20%20%20%20%5C%5C%0A%20%20%20%20%5Cdfrac%7B%5Cmathrm%7Bd%7D%5E2%20V%7D%7B%5Cmathrm%7Bd%7Db%5E2%7D%20%3D%20-%208a%20%2B%2024b%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20We%20have%20to%20find%20the%20values%20for%20%24b%24%20where%20the%20first%20derivative%20of%20%24V%24%20is%20zero%20(the%20extrema)%20and%20then%20use%20the%20expression%20for%20the%20second%20derivative%20of%20%24V%24%20to%20find%20whether%20each%20of%20these%20extrema%20is%20a%20minimum%20(positive%20value)%20or%20a%20maximum%20(negative%20value).%3Cbr%3E%0A%20%20%20%20Let's%20use%20Sympy%20for%20that%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Math%2C%20display%2C%20sym)%3A%0A%20%20%20%20a%2C%20b%20%3D%20sym.symbols('a%20b')%0A%20%20%20%20V%20%3D%20b*(a%20-%202*b)*(a%20-%202*b)%0A%20%20%20%20Vdiff%20%3D%20sym.expand(sym.diff(V%2C%20b))%0A%20%20%20%20roots%20%3D%20sym.solve(Vdiff%2C%20b)%0A%20%20%20%20display(Math(sym.latex('Roots%3A')%20%2B%20sym.latex(roots)))%0A%20%20%20%20return%20V%2C%20a%2C%20b%2C%20roots%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Discarding%20the%20solution%20%24b%3D%5Cdfrac%7Ba%7D%7B2%7D%24%20(where%20%24V%3D0%24%2C%20which%20is%20a%20minimum)%2C%20%24b%3D%5Cdfrac%7Ba%7D%7B6%7D%24%20results%20in%20the%20maximum%20volume.%3Cbr%3E%0A%20%20%20%20We%20can%20check%20that%20by%20plotting%20the%20volume%20of%20the%20cardboard%20box%20for%20%24a%3D1%24%20and%20%24b%3A%20%5B0%2C%5C%3A0.5%5D%24%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Math%2C%20V%2C%20a%2C%20b%2C%20display%2C%20plot%2C%20roots%2C%20sym)%3A%0A%20%20%20%20plot(V.subs(%7Ba%3A%201%7D)%2C%20(b%2C%200%2C%20.5)%2C%20xlabel%3D'b'%2C%20ylabel%3D'V')%0A%20%20%20%20display(Math(sym.latex('V_%7Ba%3D1%7D%5E%7Bmax%7D(b%3D%25s)%3D%25s'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%25(roots%5B0%5D.evalf(n%3D4%2C%20subs%3D%7Ba%3A%201%7D)%2C%20V.evalf(n%3D3%2C%20subs%3D%7Ba%3A%201%2C%20b%3A%20roots%5B0%5D%7D)))))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20-%20Note%20that%20although%20the%20problem%20above%20is%20a%20case%20of%20nonlinear%20constrained%20optimization%2C%20because%20the%20objective%20function%20is%20univariate%2C%20well-conditioned%20and%20the%20constraints%20are%20linear%20inequalities%2C%20the%20optimization%20is%20simple.%20Unfortunately%2C%20this%20is%20seldom%20the%20case.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%202.**%20A%20square%20sheet%20has%20side%20a.%20Show%20that%20the%20useful%20stationary%20cut%20is%20%24a%2F6%24%20and%20distinguish%20it%20from%20%24a%2F2%24.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%202%0A%20%20%20%20%24V%5Cprime%3D(a-2x)(a-6x)%24%2C%20so%20stationary%20values%20are%20%24a%2F6%24%20and%20%24a%2F2%24.%20The%20second%20collapses%20the%20base%20and%20has%20zero%20volume.%20At%20%24a%2F6%24%2C%20%24V%3D2a%5E3%2F27%24%2C%20the%20maximum%20inside%20the%20feasible%20interval.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Curve%20fitting%20as%20an%20optimization%20problem%0A%0A%20%20%20%20Curve%20fitting%20is%20the%20process%20of%20fitting%20a%20model%2C%20expressed%20in%20terms%20of%20a%20mathematical%20function%2C%20that%20depends%20on%20adjustable%20parameters%20to%20a%20series%20of%20data%20points%20and%20once%20adjusted%2C%20that%20curve%20has%20the%20best%20fit%20to%20the%20data%20points.%0A%0A%20%20%20%20The%20general%20approach%20to%20the%20fitting%20procedure%20involves%20the%20definition%20of%20a%20merit%20function%20that%20measures%20the%20agreement%20between%20data%20and%20model.%20The%20model%20parameters%20are%20then%20adjusted%20to%20yield%20the%20best-fit%20parameters%20as%20a%20problem%20of%20minimization%20(an%20optimization%20problem%2C%20where%20the%20merit%20function%20is%20the%20cost%20function).%0A%0A%20%20%20%20A%20classical%20solution%2C%20termed%20least-squares%20fitting%2C%20is%20to%20find%20the%20best%20fit%20by%20minimizing%20the%20sum%20of%20the%20squared%20differences%20between%20data%20points%20and%20the%20model%20function%20(the%20sum%20of%20squared%20residuals%20as%20the%20merit%20function).%0A%0A%20%20%20%20For%20more%20on%20curve%20fitting%20see%20the%20video%20below%20and%20the%20notebook%20%5BCurve%20fitting%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fcurve_fitting.py).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20IPython.display%20import%20YouTubeVideo%0A%20%20%20%20YouTubeVideo('Rxp7o7_RxII'%2C%20width%3D480%2C%20height%3D360%2C%20rel%3D0)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Gradient%20descent%0A%0A%20%20%20%20Gradient%20descent%20is%20a%20first-order%20iterative%20optimization%20algorithm%20for%20finding%20the%20minimum%20of%20a%20function%20(%5BWikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FGradient_descent)).%3Cbr%3E%0A%20%20%20%20In%20the%20gradient%20descent%20algorithm%2C%20a%20local%20minimum%20of%20a%20function%20is%20found%20starting%20from%20an%20initial%20point%20and%20taking%20steps%20proportional%20to%20the%20negative%20of%20the%20derivative%20of%20the%20function%20(gradient)%20at%20the%20current%20point%20and%20we%20evaluate%20if%20the%20current%20point%20is%20lower%20than%20then%20the%20previous%20point%20until%20a%20local%20minimum%20in%20reached%20(hopefully).%0A%0A%20%20%20%20It%20follows%20that%2C%20if%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x_%7Bn%2B1%7D%20%3D%20x_n%20-%20%5Cgamma%20%5Cnabla%20f(x)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20for%20%24%5Cgamma%24%20small%20enough%2C%20then%20%24f(x_%7Bn%7D)%20%5Cgeq%20f(x_%7Bn%2B1%7D)%24.%0A%0A%20%20%20%20This%20process%20is%20repeated%20iteratively%20until%20the%20step%20size%20(which%20is%20proportional%20to%20the%20gradient!)%20is%20below%20a%20required%20precision%20(hopefully%20the%20sequence%20%24x_%7Bn%7D%24%20converges%20to%20the%20desired%20local%20minimum).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Example%202%3A%20Minimum%20of%20a%20function%20by%20gradient%20descent%0A%0A%20%20%20%20From%20https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FGradient_descent%3A%3Cbr%3E%0A%20%20%20%20Calculate%20the%20minimum%20of%20%24f(x)%3Dx%5E4-3x%5E3%2B2%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20cur_x%20%3D%206%0A%20%20%20%20gamma%20%3D%200.01%0A%20%20%20%20precision%20%3D%201e-05%0A%20%20%20%20step_size%20%3D%201%0A%20%20%20%20max_iters%20%3D%2010000%0A%20%20%20%20iters%20%3D%200%0A%20%20%20%20f%20%3D%20lambda%20x%3A%20x%20**%204%20-%203%20*%20x%20**%203%20%2B%202%0A%20%20%20%20_df%20%3D%20lambda%20x%3A%204%20*%20x%20**%203%20-%209%20*%20x%20**%202%0A%20%20%20%20while%20(step_size%20%3E%20precision)%20%26%20(iters%20%3C%20max_iters)%3A%0A%20%20%20%20%20%20%20%20prev_x%20%3D%20cur_x%0A%20%20%20%20%20%20%20%20cur_x%20%3D%20cur_x%20-%20gamma%20*%20_df(prev_x)%0A%20%20%20%20%20%20%20%20step_size%20%3D%20abs(cur_x%20-%20prev_x)%0A%20%20%20%20%20%20%20%20iters%20%3D%20iters%20%2B%201%0A%20%20%20%20print('True%20local%20minimum%20at%20%7B%7D%20with%20function%20value%20%7B%7D.'.format(9%20%2F%204%2C%20f(9%20%2F%204)))%0A%20%20%20%20print('Local%20minimum%20by%20gradient%20descent%20at%20%7B%7D%20with%20function%20value%20%7B%7D.'.format(cur_x%2C%20f(cur_x)))%0A%20%20%20%20return%20f%2C%20max_iters%2C%20precision%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20related%20nonlinear%20conjugate-gradient%20algorithm%20is%20implemented%20in%20the%20%5Bscipy.optimize.minimize%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.optimize.minimize.html%23scipy.optimize.minimize)%20function%2C%20see%20%5Bminimize(method%3D'CG')%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Foptimize.minimize-cg.html)%2C%20which%20deals%20with%20minimization%20of%20a%20scalar%20function%20of%20one%20or%20more%20variables%20using%20the%20conjugate%20gradient%20algorithm.%3Cbr%3E%0A%20%20%20%20Let's%20run%20the%20example%20above%20but%20using%20the%20scipy%20function%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f%2C%20max_iters%2C%20np%2C%20precision)%3A%0A%20%20%20%20from%20scipy.optimize%20import%20minimize%0A%0A%20%20%20%20minimize(f%2C%206%2C%20args%3D()%2C%20method%3D'CG'%2C%20jac%3DNone%2C%20tol%3Dprecision%2C%20callback%3DNone%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20options%3D%7B'gtol'%3A%201e-05%2C%20'norm'%3A%20np.inf%2C%20'eps'%3A%201.4901161193847656e-08%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'maxiter'%3A%20max_iters%2C%20'disp'%3A%20True%2C%20'return_all'%3A%20False%7D)%0A%20%20%20%20return%20(minimize%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%203.**%20For%20%24f(x)%3Dx%5E2%24%2C%20use%20gradient%20descent%20%24x_%7Bk%2B1%7D%3Dx_k-%5Cgamma%20f%5Cprime(x_k)%24.%20What%20range%20of%20fixed%20positive%20step%20sizes%20converges%20to%20zero%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%203%0A%20%20%20%20The%20update%20is%20%24x_%7Bk%2B1%7D%3D(1-2%5Cgamma)x_k%24.%20Convergence%20requires%20%24%7C1-2%5Cgamma%7C%3C1%24%2C%20hence%20%240%3C%5Cgamma%3C1%24.%20A%20decreasing%20objective%20is%20not%20guaranteed%20for%20an%20arbitrary%20large%20step.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Multivariate%20optimization%0A%0A%20%20%20%20When%20there%20is%20more%20than%20one%20design%20variable%20(the%20cost%20function%20depends%20on%20more%20than%20one%20variable)%2C%20it's%20a%20multivariate%20optimization.%20The%20general%20idea%20of%20finding%20minimum%20and%20maximum%20values%20where%20the%20derivatives%20are%20zero%20still%20holds%20for%20a%20multivariate%20function.%20The%20second%20derivative%20of%20a%20multivariate%20function%20can%20be%20described%20by%20the%20Hessian%20matrix%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BH%7D%20%3D%20%5Cbegin%7Bbmatrix%7D%7B%5Cdfrac%20%20%7B%5Cpartial%20%5E%7B2%7Df%7D%7B%5Cpartial%20x_%7B1%7D%5E%7B2%7D%7D%7D%26%7B%5Cdfrac%20%20%7B%5Cpartial%20%5E%7B2%7Df%7D%7B%5Cpartial%20x_%7B1%7D%5C%2C%5Cpartial%20x_%7B2%7D%7D%7D%26%5Ccdots%20%26%7B%5Cdfrac%20%20%7B%5Cpartial%20%5E%7B2%7Df%7D%7B%5Cpartial%20x_%7B1%7D%5C%2C%5Cpartial%20x_%7Bn%7D%7D%7D%5C%5C%5B2.2ex%5D%7B%5Cdfrac%20%20%7B%5Cpartial%20%5E%7B2%7Df%7D%7B%5Cpartial%20x_%7B2%7D%5C%2C%5Cpartial%20x_%7B1%7D%7D%7D%26%7B%5Cdfrac%20%20%7B%5Cpartial%20%5E%7B2%7Df%7D%7B%5Cpartial%20x_%7B2%7D%5E%7B2%7D%7D%7D%26%5Ccdots%20%26%7B%5Cdfrac%20%20%7B%5Cpartial%20%5E%7B2%7Df%7D%7B%5Cpartial%20x_%7B2%7D%5C%2C%5Cpartial%20x_%7Bn%7D%7D%7D%5C%5C%5B2.2ex%5D%5Cvdots%20%26%5Cvdots%20%26%5Cddots%20%26%5Cvdots%20%5C%5C%5B2.2ex%5D%7B%5Cdfrac%20%20%7B%5Cpartial%20%5E%7B2%7Df%7D%7B%5Cpartial%20x_%7Bn%7D%5C%2C%5Cpartial%20x_%7B1%7D%7D%7D%26%7B%5Cdfrac%20%20%7B%5Cpartial%20%5E%7B2%7Df%7D%7B%5Cpartial%20x_%7Bn%7D%5C%2C%5Cpartial%20x_%7B2%7D%7D%7D%26%5Ccdots%20%26%7B%5Cdfrac%20%20%7B%5Cpartial%20%5E%7B2%7Df%7D%7B%5Cpartial%20x_%7Bn%7D%5E%7B2%7D%7D%7D%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Let's%20see%20now%20a%20classical%20problem%20in%20biomechanics%20where%20optimization%20is%20useful%20and%20there%20is%20more%20than%20one%20design%20variable.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20distribution%20problem%20in%20biomechanics%0A%0A%20%20%20%20Using%20the%20inverse%20dynamics%20approach%20in%20biomechanics%2C%20we%20can%20determine%20the%20net%20force%20and%20torque%20acting%20on%20a%20joint%20if%20we%20know%20the%20external%20forces%20on%20the%20segments%20and%20the%20kinematics%20and%20inertial%20properties%20of%20the%20segments.%20But%20with%20this%20approach%20we%20are%20unable%20to%20determine%20the%20individual%20muscles%20forces%20that%20%20created%20such%20torque%2C%20as%20expressed%20in%20the%20following%20equation%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20M_%7Btotal%7D%20%3D%20M_1%20%2B%20M_2%20%2B%20%5Cdots%20%2B%20M_n%20%3D%20r_1F_1%20%2B%20r_2F_2%20%2B%20%5Cdots%20%2B%20r_nF_n%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24r_i%24%20is%20the%20moment%20arm%20of%20the%20force%20%24F_i%24%20that%20generates%20a%20torque%20%24M_i%24%2C%20a%20parcel%20of%20the%20(known)%20total%20torque%20%24M_%7Btotal%7D%24.%0A%0A%20%20%20%20Even%20if%20we%20know%20the%20moment%20arm%20of%20each%20muscle%20(e.g.%2C%20from%20cadaveric%20data%20or%20from%20image%20analysis)%2C%20the%20equation%20above%20has%20%24n%24%20unknowns.%20Because%20there%20is%20more%20than%20one%20muscle%20that%20potentially%20created%20such%20torque%2C%20there%20are%20more%20unknowns%20than%20equations%2C%20and%20the%20problem%20is%20undetermined.%20So%2C%20the%20problem%20is%20how%20to%20find%20how%20the%20torque%20is%20distributed%20among%20the%20muscles%20of%20that%20joint.%0A%0A%20%20%20%20One%20solution%20is%20to%20consider%20that%20we%20(biological%20systems)%20optimize%20our%20effort%20in%20order%20to%20minimize%20energy%20expenditure%2C%20stresses%20on%20our%20tissues%2C%20fatigue%2C%20etc.%20The%20principle%20of%20least%20action%2C%20stated%20in%20the%20opening%20of%20this%20text%2C%20is%20an%20allusion%20that%20optimization%20might%20be%20ubiquitous%20in%20nature.%20With%20this%20rationale%2C%20let's%20solve%20the%20distribution%20problem%20in%20biomechanics%20using%20optimization%20and%20find%20the%20minimum%20force%20of%20each%20muscle%20necessary%20to%20complete%20a%20given%20task.%0A%0A%20%20%20%20The%20following%20cost%20functions%20have%20been%20proposed%20to%20solve%20the%20distribution%20problem%20in%20biomechanics%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bl%20l%7D%0A%20%20%20%20%5Cdisplaystyle%5Csum_%7Bi%3D1%7D%5EN%20F_i%20%5Cquad%20%26%5Ctext%7Be.g.%2C%20Seireg%20and%20Arkivar%20(1973)%7D%0A%20%20%20%20%5C%5C%0A%20%20%20%20%5Cdisplaystyle%5Csum_%7Bi%3D1%7D%5EN%20F_i%5E2%20%5Cquad%20%26%0A%20%20%20%20%5C%5C%0A%20%20%20%20%5Cdisplaystyle%5Csum_%7Bi%3D1%7D%5EN%20%5Cleft(%5Cdfrac%7BF_i%7D%7Bpcsa_i%7D%5Cright)%5E2%20%5Cquad%20%26%5Ctext%7Be.g.%2C%20Crowninshield%20and%20Brand%20(1981)%7D%0A%20%20%20%20%5C%5C%0A%20%20%20%20%5Cdisplaystyle%5Csum_%7Bi%3D1%7D%5EN%20%5Cleft(%5Cdfrac%7BF_i%7D%7BM_%7Bmax%2Ci%7D%7D%5Cright)%5E3%20%5Cquad%20%26%5Ctext%7Be.g.%2C%20Herzog%20(1987)%7D%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24pcsa_i%24%20is%20the%20physiological%20cross-sectional%20area%20of%20muscle%20%24i%24%20and%20%24M_%7Bmax%2Ci%7D%24%20is%20the%20maximum%20torque%20muscle%20%24i%24%20can%20produce.%3Cbr%3E%0A%20%20%20%20Each%20muscle%20force%20%24F_i%24%20is%20a%20design%20variable%20and%20the%20following%20constraints%20must%20be%20satisfied%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bl%20l%7D%0A%20%20%20%200%20%5Cleq%20F_i%20%5Cleq%20F_%7Bmax%7D%0A%20%20%20%20%5C%5C%0A%20%20%20%20%5Cdisplaystyle%5Csum_%7Bi%3D1%7D%5EN%20r_i%20%5Ctimes%20F_i%20%3D%20M%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Let's%20apply%20this%20concept%20to%20solve%20a%20distribution%20problem%20in%20biomechanics.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%204.**%20Two%20muscles%20have%20moment%20arms%200.02%20and%200.04%20m.%20If%20either%20alone%20must%20supply%204%20Nm%2C%20what%20force%20is%20needed%3F%20Which%20does%20minimizing%20total%20force%20favor%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%204%0A%20%20%20%20The%20required%20forces%20are%20200%20and%20100%20N.%20Minimizing%20their%20sum%20favors%20the%20larger%20moment%20arm%20when%20its%20capacity%20permits.%20That%20preference%20is%20a%20modeling%20choice%3B%20it%20is%20not%20a%20direct%20observation%20of%20recruitment.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Muscle%20force%20estimation%0A%0A%20%20%20%20Consider%20the%20following%20main%20flexors%20of%20the%20elbow%20joint%20(see%20figure%20below)%3A%20biceps%20long%20head%2C%20biceps%20short%20head%2C%20and%20brachialis.%20Suppose%20that%20the%20elbow%20net%20joint%20torque%20determined%20using%20inverse%20dynamics%20is%2020%20Nm%20(flexor).%20How%20much%20each%20of%20these%20muscles%20contributed%20to%20the%20net%20torque%3F%0A%0A%20%20%20%20%3Cdiv%20class%3D'center-align'%3E%3Cfigure%20aria-describedby%3D%22figure-caption-3%22%3E%3Cimg%20src%3D'https%3A%2F%2Fgithub.com%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fimages%2Felbowflexors.png%3Fraw%3D1'%20alt%3D'Elbow%20flexors'%2F%3E%20%20%3C%2Ffigure%3E%3C%2Fdiv%3E%0A%0A%20%20%20%20%3Cspan%20id%3D%22figure-caption-3%22%20class%3D%22figure-caption%22%20style%3D%22display%3Ablock%3Btext-align%3Acenter%3Bfont-style%3Aitalic%22%3EFigure.%20A%20view%20in%20OpenSim%20of%20the%20arm26%20model%20showing%20three%20elbow%20flexors%20(Biceps%20long%20and%20short%20heads%20and%20Brachialis).%3C%2Fspan%3E%0A%0A%20%20%20%20For%20the%20optimization%2C%20we%20will%20need%20experimental%20data%20for%20the%20moment%20arm%2C%20maximum%20moment%2C%20and%20*pcsa*%20of%20each%20muscle.%20Let's%20import%20these%20data%20from%20the%20OpenSim%20arm26%20model%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20from%20notebook_resources%20import%20repo_file%0A%20%20%20%20except%20ModuleNotFoundError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20if%20_error.name%20!%3D%20%22notebook_resources%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%0A%20%20%20%20%20%20%20%20raise%20ModuleNotFoundError(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22This%20notebook%20needs%20notebook_resources.py.%20Download%20it%20from%3A%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22https%3A%2F%2Fraw.githubusercontent.com%2FBMClab%2FBMC%2Fmaster%2Ffunctions%2Fnotebook_resources.py%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Save%20it%20in%20the%20same%20folder%20as%20this%20notebook%2C%20then%20rerun%20this%20cell.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22notebook_resources%22%2C%0A%20%20%20%20%20%20%20%20)%20from%20None%0A%20%20%20%20return%20(repo_file%2C)%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20repo_file)%3A%0A%20%20%20%20%23%20time%20elbow_flexion%20BIClong%20BICshort%20BRA%0A%20%20%20%20r_ef%20%3D%20np.loadtxt(repo_file('data%2Fr_elbowflexors.mot')%2C%20skiprows%3D7)%0A%20%20%20%20f_ef%20%3D%20np.loadtxt(repo_file('data%2Ff_elbowflexors.mot')%2C%20skiprows%3D7)%0A%20%20%20%20return%20f_ef%2C%20r_ef%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20maximum%20isometric%20force%20of%20these%20muscles%20are%20defined%20in%20the%20arm26%20model%20as%3A%20Biceps%20long%20head%3A%20624.3%20N%2C%20Biceps%20short%20head%3A%20435.56%20N%2C%20and%20Brachialis%3A%20987.26%20N.%20Let's%20compute%20the%20mamimum%20torques%20that%20each%20muscle%20could%20produce%20considering%20a%20static%20situation%20at%20the%20different%20elbow%20flexion%20angles%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f_ef%2C%20r_ef)%3A%0A%20%20%20%20m_ef%20%3D%20r_ef*1%0A%20%20%20%20m_ef%5B%3A%2C%202%3A%5D%20%3D%20r_ef%5B%3A%2C%202%3A%5D*f_ef%5B%3A%2C%202%3A%5D%0A%20%20%20%20return%20(m_ef%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20And%20let's%20visualize%20these%20data%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f_ef%2C%20m_ef%2C%20np%2C%20plt%2C%20r_ef)%3A%0A%20%20%20%20labels%20%3D%20%5B'Biceps%20long%20head'%2C%20'Biceps%20short%20head'%2C%20'Brachialis'%5D%0A%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots(nrows%3D1%2C%20ncols%3D3%2C%20sharex%3DTrue%2C%20figsize%3D(10%2C%204))%0A%20%20%20%20ax%5B0%5D.plot(r_ef%5B%3A%2C%201%5D%2C%20r_ef%5B%3A%2C%202%3A%5D)%0A%20%20%20%20%23ax%5B0%5D.set_xlabel('Elbow%20angle%24(%5C%2C%5Eo)%24')%0A%20%20%20%20ax%5B0%5D.set_title('Moment%20arm%20(m)')%0A%20%20%20%20ax%5B1%5D.plot(f_ef%5B%3A%2C%201%5D%2C%20f_ef%5B%3A%2C%202%3A%5D)%0A%20%20%20%20ax%5B1%5D.set_xlabel(r'Elbow%20angle%24(%5C%2C%5Eo)%24'%2C%20fontsize%3D16)%0A%20%20%20%20ax%5B1%5D.set_title('Maximum%20force%20(N)')%0A%20%20%20%20ax%5B2%5D.plot(m_ef%5B%3A%2C%201%5D%2C%20m_ef%5B%3A%2C%202%3A%5D)%0A%20%20%20%20%23ax%5B2%5D.set_xlabel('Elbow%20angle%24(%5C%2C%5Eo)%24')%0A%20%20%20%20ax%5B2%5D.set_title('Maximum%20torque%20(Nm)')%0A%20%20%20%20ax%5B2%5D.legend(labels%2C%20loc%3D'best'%2C%20framealpha%3D.5)%0A%20%20%20%20ax%5B2%5D.set_xlim(np.min(r_ef%5B%3A%2C%201%5D)%2C%20np.max(r_ef%5B%3A%2C%201%5D))%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20These%20data%20don't%20have%20the%20*pcsa*%20value%20of%20each%20muscle.%20We%20will%20estimate%20the%20*pcsa*%20considering%20that%20the%20amount%20of%20maximum%20muscle%20force%20generated%20per%20area%20is%20constant%20and%20equal%20to%2050N%2Fcm%24%5E2%24.%20Consequently%2C%20the%20*pcsa*%20(in%20cm%24%5E2%24)%20for%20each%20muscle%20is%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20a_ef%20%3D%20np.array(%5B624.3%2C%20435.56%2C%20987.26%5D)%2F50%20%20%23%2050%20N%2Fcm2%0A%20%20%20%20print(a_ef)%0A%20%20%20%20return%20(a_ef%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Static%20versus%20dynamic%20optimization%0A%0A%20%20%20%20In%20the%20context%20of%20biomechanics%2C%20we%20can%20solve%20the%20distribution%20problem%20separately%20for%20each%20angle%20(instant)%20of%20the%20elbow%3B%20we%20will%20refer%20to%20that%20as%20static%20optimization.%20However%2C%20there%20is%20no%20guarantee%20that%20when%20we%20analyze%20all%20these%20solutions%20across%20the%20range%20of%20angles%2C%20they%20will%20be%20the%20best%20solution%20overall.%20One%20reason%20is%20that%20static%20optimization%20ignores%20the%20time%20history%20of%20the%20muscle%20force.%20Dynamic%20optimization%20refers%20to%20the%20optimization%20over%20a%20period%20of%20time.%20For%20such%2C%20we%20will%20need%20to%20input%20a%20cost%20function%20spanning%20the%20entire%20period%20of%20time%20at%20once.%20Dynamic%20optimization%20usually%20has%20a%20higher%20computational%20cost%20than%20static%20optimization.%20The%20term%20'Dynamic%20optimization'%20used%20in%20Biomechanics%20is%20not%20related%20to%20the%20broader%20method%20known%20as%20'%5Bdynamic%20programming%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDynamic_programming)'%20in%20mathematical%20optimization%20or%20computer%20programming.%0A%0A%20%20%20%20%3Cdiv%20class%3D'center-align'%3E%3Cfigure%20aria-describedby%3D%22figure-caption-4%22%3E%3Cimg%20src%3D'https%3A%2F%2Fgithub.com%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fimages%2Foptim_stat_dyn.png%3Fraw%3D1'%20width%3D550%20alt%3D'static%20versus%20dynamic%20optimization'%20vspace%3D%2210%22%2F%3E%20%20%3C%2Ffigure%3E%3C%2Fdiv%3E%0A%0A%20%20%20%20%3Cspan%20id%3D%22figure-caption-4%22%20class%3D%22figure-caption%22%20style%3D%22display%3Ablock%3Btext-align%3Acenter%3Bfont-style%3Aitalic%22%3EFigure.%20Static%20and%20dynamic%20optimization%20approaches%20in%20Biomechanics.%20Figure%20taken%20from%20Robertson%20et%20al.%20(2013).%3C%2Fspan%3E%0A%0A%20%20%20%20The%20title%20'Static%20and%20dynamic%20optimization%20solutions%20for%20gait%20are%20practically%20equivalent'%20of%20an%20article%20published%20by%20Anderson%20and%20Pandy%20(2001)%2C%20where%20they%20studied%20normal%20walking%20(full%20gait%20cycle%20equals%20to%201.12%20s)%2C%20summarizes%20the%20results%20of%20such%20comparison.%20The%20authors%20reported%20that%20the%20dynamic%20solution%20required%201000%20times%20more%20computation%20time%20than%20the%20static%20solution.%0A%0A%20%20%20%20For%20now%2C%20we%20will%20solve%20the%20present%20problem%20using%20static%20optimization.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Solution%20of%20the%20optimization%20problem%0A%0A%20%20%20%20For%20the%20present%20case%2C%20we%20are%20dealing%20with%20a%20problem%20of%20minimization%2C%20multidimensional%20(function%20of%20several%20variables)%2C%20nonlinear%2C%20constrained%2C%20with%20linear%20force%20bounds%20and%20moment%20balance.%20For%20positive%20normalization%20constants%2C%20the%20displayed%20linear%2C%20squared%2C%20and%20nonnegative%20cubic%20objectives%20are%20convex%20on%20this%20feasible%20set.%20Numerical%20optimization%20is%20hardly%20a%20simple%20task.%20There%20are%20many%20different%20algorithms%20and%20public%20and%20commercial%20software%20for%20performing%20optimization.%20For%20instance%2C%20look%20at%20%5BNEOS%20Server%5D(http%3A%2F%2Fwww.neos-server.org%2Fneos%2F)%2C%20a%20free%20internet-based%20service%20for%20solving%20numerical%20optimization%20problems.%3Cbr%3E%0A%20%20%20%20We%20will%20solve%20the%20present%20problem%20using%20the%20%5Bscipy.optimize%5D(http%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Foptimize.html%23module-scipy.optimize)%20package%20which%20provides%20several%20optimization%20algorithms.%20We%20will%20use%20the%20function%20%60minimize%60%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20scipy.optimize.minimize(fun%2C%20x0%2C%20args%3D()%2C%20method%3DNone%2C%20jac%3DNone%2C%20hess%3DNone%2C%20hessp%3DNone%2C%20bounds%3DNone%2C%20constraints%3D()%2C%20tol%3DNone%2C%20callback%3DNone%2C%20options%3DNone)%0A%20%20%20%20%5C%22%5C%22%5C%22Minimization%20of%20scalar%20function%20of%20one%20or%20more%20variables.%5C%22%5C%22%5C%22%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Now%2C%20let's%20write%20Python%20functions%20for%20each%20cost%20function%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20def%20cf_f1(x)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Cost%20function%3A%20sum%20of%20forces.%22%22%22%20%20%0A%20%20%20%20%20%20%20%20return%20x%5B0%5D%20%2B%20x%5B1%5D%20%2B%20x%5B2%5D%0A%0A%20%20%20%20def%20cf_f2(x)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Cost%20function%3A%20sum%20of%20forces%20squared.%22%22%22%0A%20%20%20%20%20%20%20%20return%20x%5B0%5D**2%20%2B%20x%5B1%5D**2%20%2B%20x%5B2%5D**2%0A%0A%20%20%20%20def%20cf_fpcsa2(x%2C%20a)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Cost%20function%3A%20sum%20of%20squared%20muscle%20stresses.%22%22%22%0A%20%20%20%20%20%20%20%20return%20(x%5B0%5D%2Fa%5B0%5D)**2%20%2B%20(x%5B1%5D%2Fa%5B1%5D)**2%20%2B%20(x%5B2%5D%2Fa%5B2%5D)**2%0A%0A%20%20%20%20def%20cf_fmmax3(x%2C%20m)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Cost%20function%3A%20sum%20of%20cubic%20forces%20normalized%20by%20moments.%22%22%22%0A%20%20%20%20%20%20%20%20return%20(x%5B0%5D%2Fm%5B0%5D)**3%20%2B%20(x%5B1%5D%2Fm%5B1%5D)**3%20%2B%20(x%5B2%5D%2Fm%5B2%5D)**3%0A%0A%20%20%20%20return%20cf_f1%2C%20cf_f2%2C%20cf_fmmax3%2C%20cf_fpcsa2%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Let's%20also%20define%20the%20Jacobian%20for%20each%20cost%20function%20(which%20is%20an%20optional%20parameter%20for%20the%20optimization)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20def%20cf_f1d(x)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Derivative%20of%20cost%20function%3A%20sum%20of%20forces.%22%22%22%0A%20%20%20%20%20%20%20%20dfdx0%20%3D%201.0%0A%20%20%20%20%20%20%20%20dfdx1%20%3D%201.0%0A%20%20%20%20%20%20%20%20dfdx2%20%3D%201.0%0A%20%20%20%20%20%20%20%20return%20np.array(%5Bdfdx0%2C%20dfdx1%2C%20dfdx2%5D)%0A%0A%20%20%20%20def%20cf_f2d(x)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Derivative%20of%20cost%20function%3A%20sum%20of%20forces%20squared.%22%22%22%0A%20%20%20%20%20%20%20%20dfdx0%20%3D%202*x%5B0%5D%0A%20%20%20%20%20%20%20%20dfdx1%20%3D%202*x%5B1%5D%0A%20%20%20%20%20%20%20%20dfdx2%20%3D%202*x%5B2%5D%0A%20%20%20%20%20%20%20%20return%20np.array(%5Bdfdx0%2C%20dfdx1%2C%20dfdx2%5D)%0A%0A%20%20%20%20def%20cf_fpcsa2d(x%2C%20a)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Derivative%20of%20cost%20function%3A%20sum%20of%20squared%20muscle%20stresses.%22%22%22%0A%20%20%20%20%20%20%20%20dfdx0%20%3D%202*x%5B0%5D%2Fa%5B0%5D**2%0A%20%20%20%20%20%20%20%20dfdx1%20%3D%202*x%5B1%5D%2Fa%5B1%5D**2%0A%20%20%20%20%20%20%20%20dfdx2%20%3D%202*x%5B2%5D%2Fa%5B2%5D**2%0A%20%20%20%20%20%20%20%20return%20np.array(%5Bdfdx0%2C%20dfdx1%2C%20dfdx2%5D)%0A%0A%20%20%20%20def%20cf_fmmax3d(x%2C%20m)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Derivative%20of%20cost%20function%3A%20sum%20of%20cubic%20forces%20normalized%20by%20moments.%22%22%22%0A%20%20%20%20%20%20%20%20dfdx0%20%3D%203*x%5B0%5D**2%2Fm%5B0%5D**3%0A%20%20%20%20%20%20%20%20dfdx1%20%3D%203*x%5B1%5D**2%2Fm%5B1%5D**3%0A%20%20%20%20%20%20%20%20dfdx2%20%3D%203*x%5B2%5D**2%2Fm%5B2%5D**3%0A%20%20%20%20%20%20%20%20return%20np.array(%5Bdfdx0%2C%20dfdx1%2C%20dfdx2%5D)%0A%0A%20%20%20%20return%20cf_f1d%2C%20cf_f2d%2C%20cf_fmmax3d%2C%20cf_fpcsa2d%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Let's%20define%20initial%20values%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(a_ef%2C%20f_ef%2C%20m_ef%2C%20np%2C%20r_ef)%3A%0A%20%20%20%20M%20%3D%2020%0A%20%20%20%20iang%20%3D%2069%0A%20%20%20%20r%20%3D%20r_ef%5Biang%2C%202%3A%5D%0A%20%20%20%20f0%20%3D%20f_ef%5Biang%2C%202%3A%5D%0A%20%20%20%20a_1%20%3D%20a_ef%0A%20%20%20%20m%20%3D%20m_ef%5Biang%2C%202%3A%5D%0A%20%20%20%20x0%20%3D%20f_ef%5Biang%2C%202%3A%5D%20%2F%2010%0A%20%20%20%20print('M%20%3D'%2C%20M)%0A%20%20%20%20print('x0%20%3D'%2C%20x0)%0A%20%20%20%20print('r%20*%20x0%20%3D'%2C%20np.sum(r%20*%20x0))%0A%20%20%20%20return%20M%2C%20a_1%2C%20f0%2C%20m%2C%20r%2C%20x0%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Inequality%20constraints%20(such%20as%20boundaries%20in%20our%20problem)%20can%20be%20entered%20with%20the%20parameter%20%60bounds%60%20to%20the%20%60minimize%60%20function%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f0)%3A%0A%20%20%20%20bnds%20%3D%20((0%2C%20f0%5B0%5D)%2C%20(0%2C%20f0%5B1%5D)%2C%20(0%2C%20f0%5B2%5D))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Equality%20constraints%20(such%20as%20the%20sum%20of%20torques%20should%20equals%20the%20desired%20torque%20in%20our%20problem)%2C%20as%20well%20as%20inequality%20constraints%2C%20can%20be%20entered%20with%20the%20parameter%20%60constraints%60%20to%20the%20%60minimize%60%20function%20(and%20we%20can%20also%20opt%20to%20enter%20the%20Jacobian%20of%20these%20constraints)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(M%2C%20f0%2C%20np%2C%20r)%3A%0A%20%20%20%20%23%20use%20this%20in%20combination%20with%20the%20parameter%20bounds%3A%0A%20%20%20%20cons%20%3D%20(%7B'type'%3A%20'eq'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20'fun'%20%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20np.array(%5Br%5B0%5D*x%5B0%5D%20%2B%20r%5B1%5D*x%5B1%5D%20%2B%20r%5B2%5D*x%5B2%5D%20-%20M%5D)%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20'jac'%20%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20np.array(%5Br%5B0%5D%2C%20r%5B1%5D%2C%20r%5B2%5D%5D)%2C%20'args'%3A%20(r%2C%20f0%2C%20M)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(M%2C%20f0%2C%20np%2C%20r)%3A%0A%20%20%20%20cons_1%20%3D%20(%7B'type'%3A%20'eq'%2C%20'fun'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20np.array(%5Br%5B0%5D%20*%20x%5B0%5D%20%2B%20r%5B1%5D%20*%20x%5B1%5D%20%2B%20r%5B2%5D%20*%20x%5B2%5D%20-%20M%5D)%2C%20'jac'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20np.array(%5Br%5B0%5D%2C%20r%5B1%5D%2C%20r%5B2%5D%5D)%2C%20'args'%3A%20(r%2C%20f0%2C%20M)%7D%2C%20%7B'type'%3A%20'ineq'%2C%20'fun'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20f0%5B0%5D%20-%20x%5B0%5D%2C%20'jac'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20np.array(%5B-1.0%2C%200%2C%200%5D)%2C%20'args'%3A%20(r%2C%20f0%2C%20M)%7D%2C%20%7B'type'%3A%20'ineq'%2C%20'fun'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20f0%5B1%5D%20-%20x%5B1%5D%2C%20'jac'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20np.array(%5B0%2C%20-1.0%2C%200%5D)%2C%20'args'%3A%20(r%2C%20f0%2C%20M)%7D%2C%20%7B'type'%3A%20'ineq'%2C%20'fun'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20f0%5B2%5D%20-%20x%5B2%5D%2C%20'jac'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20np.array(%5B0%2C%200%2C%20-1.0%5D)%2C%20'args'%3A%20(r%2C%20f0%2C%20M)%7D%2C%20%7B'type'%3A%20'ineq'%2C%20'fun'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20x%5B0%5D%2C%20'jac'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20np.array(%5B1.0%2C%200%2C%200%5D)%2C%20'args'%3A%20(r%2C%20f0%2C%20M)%7D%2C%20%7B'type'%3A%20'ineq'%2C%20'fun'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20x%5B1%5D%2C%20'jac'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20np.array(%5B0%2C%201.0%2C%200%5D)%2C%20'args'%3A%20(r%2C%20f0%2C%20M)%7D%2C%20%7B'type'%3A%20'ineq'%2C%20'fun'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20x%5B2%5D%2C%20'jac'%3A%20lambda%20x%2C%20r%2C%20f0%2C%20M%3A%20np.array(%5B0%2C%200%2C%201.0%5D)%2C%20'args'%3A%20(r%2C%20f0%2C%20M)%7D)%0A%20%20%20%20return%20(cons_1%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Although%20more%20verbose%2C%20if%20all%20the%20Jacobians%20of%20the%20constraints%20are%20also%20informed%2C%20this%20alternative%20seems%20better%20than%20informing%20bounds%20for%20the%20optimization%20process%20(less%20error%20in%20the%20final%20result%20and%20less%20iterations).%0A%0A%20%20%20%20We%20use%20SLSQP%20(Sequential%20Least%20Squares%20Programming)%2C%20which%20supports%20equality%20constraints%20and%20bounds.%20It%20is%20one%20choice%2C%20not%20the%20only%20constrained%20solver%20in%20%60minimize%60%3B%20%60trust-constr%60%20is%20another.%0A%0A%20%20%20%20Finally%2C%20let's%20run%20the%20optimization%20for%20the%20four%20different%20cost%20functions%20and%20find%20the%20optimal%20muscle%20forces%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(cf_f1%2C%20cf_f1d%2C%20cons_1%2C%20minimize%2C%20x0)%3A%0A%20%20%20%20f1r%20%3D%20minimize(fun%3Dcf_f1%2C%20x0%3Dx0%2C%20args%3D()%2C%20jac%3Dcf_f1d%2C%20constraints%3Dcons_1%2C%20method%3D'SLSQP'%2C%20options%3D%7B'disp'%3A%20True%7D)%0A%20%20%20%20return%20(f1r%2C)%0A%0A%0A%40app.cell%0Adef%20_(cf_f2%2C%20cf_f2d%2C%20cons_1%2C%20minimize%2C%20x0)%3A%0A%20%20%20%20f2r%20%3D%20minimize(fun%3Dcf_f2%2C%20x0%3Dx0%2C%20args%3D()%2C%20jac%3Dcf_f2d%2C%20constraints%3Dcons_1%2C%20method%3D'SLSQP'%2C%20options%3D%7B'disp'%3A%20True%7D)%0A%20%20%20%20return%20(f2r%2C)%0A%0A%0A%40app.cell%0Adef%20_(a_1%2C%20cf_fpcsa2%2C%20cf_fpcsa2d%2C%20cons_1%2C%20minimize%2C%20x0)%3A%0A%20%20%20%20fpcsa2r%20%3D%20minimize(fun%3Dcf_fpcsa2%2C%20x0%3Dx0%2C%20args%3D(a_1%2C)%2C%20jac%3Dcf_fpcsa2d%2C%20constraints%3Dcons_1%2C%20method%3D'SLSQP'%2C%20options%3D%7B'disp'%3A%20True%7D)%0A%20%20%20%20return%20(fpcsa2r%2C)%0A%0A%0A%40app.cell%0Adef%20_(cf_fmmax3%2C%20cf_fmmax3d%2C%20cons_1%2C%20m%2C%20minimize%2C%20x0)%3A%0A%20%20%20%20fmmax3r%20%3D%20minimize(fun%3Dcf_fmmax3%2C%20x0%3Dx0%2C%20args%3D(m%2C)%2C%20jac%3Dcf_fmmax3d%2C%20constraints%3Dcons_1%2C%20method%3D'SLSQP'%2C%20options%3D%7B'disp'%3A%20True%7D)%0A%20%20%20%20return%20(fmmax3r%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Let's%20compare%20the%20results%20for%20the%20different%20cost%20functions%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(M%2C%20a_1%2C%20f0%2C%20f1r%2C%20f2r%2C%20fmmax3r%2C%20fpcsa2r%2C%20m%2C%20mo%2C%20np%2C%20pd%2C%20r)%3A%0A%20%20%20%20dat%20%3D%20np.vstack((np.around(r%20*%20100%2C%201)%2C%20np.around(a_1%2C%201)%2C%20np.around(f0%2C%200)%2C%20np.around(m%2C%201)))%0A%20%20%20%20opt%20%3D%20np.around(np.vstack((f1r.x%2C%20f2r.x%2C%20fpcsa2r.x%2C%20fmmax3r.x))%2C%201)%0A%20%20%20%20er%20%3D%20%5B'-'%2C%20'-'%2C%20'-'%2C%20'-'%2C%20np.sum(r%20*%20f1r.x)%20-%20M%2C%20np.sum(r%20*%20f2r.x)%20-%20M%2C%20np.sum(r%20*%20fpcsa2r.x)%20-%20M%2C%20np.sum(r%20*%20fmmax3r.x)%20-%20M%5D%0A%20%20%20%20data%20%3D%20np.vstack((np.vstack((dat%2C%20opt)).T%2C%20er)).T%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20r'%24%5Ctext%7BMoment%20arm%7D%5C%3B%5Bcm%5D%24'%2C%0A%20%20%20%20%20%20%20%20r'%24pcsa%5C%3B%5Bcm%5E2%5D%24'%2C%0A%20%20%20%20%20%20%20%20r'%24F_%7Bmax%7D%5C%3B%5BN%5D%24'%2C%0A%20%20%20%20%20%20%20%20r'%24M_%7Bmax%7D%5C%3B%5BNm%5D%24'%2C%0A%20%20%20%20%20%20%20%20r'%24%5Csum%20F_i%24'%2C%0A%20%20%20%20%20%20%20%20r'%24%5Csum%20F_i%5E2%24'%2C%0A%20%20%20%20%20%20%20%20r'%24%5Csum(F_i%2Fpcsa_i)%5E2%24'%2C%0A%20%20%20%20%20%20%20%20r'%24%5Csum(F_i%2FM_%7Bmax%2Ci%7D)%5E3%24'%2C%0A%20%20%20%20%5D%0A%20%20%20%20cols%20%3D%20%5B'Biceps%20long%20head'%2C%20'Biceps%20short%20head'%2C%20'Brachialis'%2C%20'Error%20in%20M'%5D%0A%20%20%20%20_df%20%3D%20pd.DataFrame(data%2C%20index%3Drows%2C%20columns%3Dcols)%0A%20%20%20%20print('%5CnComparison%20of%20different%20cost%20functions%20for%20solving%20the%20distribution%20problem')%0A%20%20%20%20%23%20Format%20a%20regular%20column%3A%20the%20default%20DataFrame%20index%20is%20displayed%20as%20text.%0A%20%20%20%20mo.ui.table(%0A%20%20%20%20%20%20%20%20_df.reset_index(names%3D'Quantity%20%2F%20cost%20function')%2C%0A%20%20%20%20%20%20%20%20format_mapping%3D%7B'Quantity%20%2F%20cost%20function'%3A%20mo.md%7D%2C%0A%20%20%20%20%20%20%20%20selection%3DNone%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Check%20feasibility%20before%20interpretation%0A%0A%20%20%20%20After%20optimization%2C%20inspect%20%60success%60%2C%20the%20termination%20message%2C%20the%20moment%20residual%2C%20and%20every%20force%20bound.%20A%20returned%20array%20is%20not%20by%20itself%20a%20feasible%20solution.%20Compare%20starts%20and%20solver%20tolerances%2C%20and%20keep%20the%20objective%E2%80%99s%20units%20explicit.%20The%20retained%20cubic%20force-over-maximum-moment%20criterion%20has%20dimensional%20normalization%3B%20it%20should%20not%20be%20mistaken%20for%20dimensionless%20muscle%20activation.%0A%0A%20%20%20%20A%20minimum%20of%20a%20chosen%20cost%20is%20a%20prediction%20conditional%20on%20that%20cost.%20The%20historical%20least-action%20quotation%20is%20motivation%2C%20not%20evidence%20that%20the%20nervous%20system%20minimizes%20any%20particular%20recruitment%20objective.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Comments%0A%0A%20%20%20%20The%20results%20show%20that%20the%20estimations%20for%20the%20muscle%20forces%20depend%20on%20the%20cost%20function%20used%20in%20the%20optimization.%20Which%20one%20is%20correct%3F%20This%20is%20a%20difficult%20question%20and%20it's%20dependent%20on%20the%20goal%20of%20the%20actual%20task%20being%20modeled.%20Glitsch%20and%20Baumann%20(1997)%20investigated%20the%20effect%20of%20different%20cost%20functions%20on%20the%20optimization%20of%20walking%20and%20running%20and%20the%20predicted%20muscles%20forces%20were%20compared%20with%20the%20electromyographic%20activity%20of%20the%20corresponding%20muscles%20of%20the%20lower%20limb.%20They%20found%20that%2C%20among%20the%20analyzed%20cost%20functions%2C%20the%20minimization%20of%20the%20sum%20of%20squared%20muscle%20stresses%20resulted%20in%20the%20best%20similarity%20with%20the%20actual%20electromyographic%20activity.%0A%0A%20%20%20%20In%20general%2C%20one%20should%20always%20test%20different%20algorithms%20and%20different%20initial%20values%20before%20settling%20for%20the%20solution%20found.%20Downey%20(2011)%2C%20Kitchin%20(2013)%2C%20and%20Kiusalaas%20(2013)%20present%20more%20examples%20on%20numerical%20optimization.%20The%20%5BNEOS%20Guide%5D(http%3A%2F%2Fneos-guide.org%2F)%20is%20a%20valuable%20source%20of%20information%20on%20this%20topic%20and%20the%20%5BSciPy%20optimization%20guide%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Ftutorial%2Foptimize.html)%20documents%20maintained%20solver%20interfaces.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Exercises%3A%20apply%20and%20audit%20the%20models%0A%0A%20%20%20%20**Exercise%205%20(original%20analytical-extrema%20task).**%20Find%20and%20classify%20the%20stationary%20points%20of%20%24f(x)%3Dx%5E3-7.5x%5E2%2B18x-10%24.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%205%0A%20%20%20%20The%20derivative%20is%20%243x%5E2-15x%2B18%3D3(x-2)(x-3)%24.%20The%20second%20derivative%20is%20%246x-15%24%2C%20negative%20at%202%20and%20positive%20at%203.%20Thus%20%24(2%2C4)%24%20is%20a%20local%20maximum%20and%20%24(3%2C3.5)%24%20a%20local%20minimum.%20The%20cubic%20is%20unbounded%20on%20the%20real%20line%2C%20so%20neither%20is%20a%20global%20extremum%20there.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20**Exercise%206%20(original%20gradient-descent%20task).**%20Find%20the%20local%20minimum%20of%20the%20same%20function%20by%20gradient%20descent%2C%20and%20state%20a%20suitable%20starting%20point%20and%20step.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%206%0A%20%20%20%20Use%20%24x_%7Bk%2B1%7D%3Dx_k-0.05(3x_k%5E2-15x_k%2B18)%24%2C%20starting%20at%20%24x_0%3D4%24.%20The%20iterates%20approach%20%24x%3D3%24%20and%20%24f%3D3.5%24.%20Starting%20exactly%20at%202%20stays%20at%20a%20stationary%20maximum%3B%20starting%20far%20to%20its%20left%20can%20run%20toward%20the%20unbounded%20negative%20tail.%20Check%20the%20derivative%20and%20neighborhood%2C%20not%20only%20the%20update%20size.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20**Exercise%207%20(original%20elbow-sensitivity%20task).**%20Repeat%20the%20elbow-force%20fits%20from%20different%20initial%20forces%20and%20at%20different%20elbow%20angles.%20Explain%20what%20a%20trustworthy%20comparison%20should%20show.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%207%0A%20%20%20%20At%20each%20angle%20first%20load%20that%20angle%E2%80%99s%20moment%20arms%20and%20capacities%2C%20then%20check%20whether%20the%20required%20moment%20is%20feasible.%20Keep%20objective%20and%20angle%20fixed%20while%20varying%20initial%20forces.%20These%20convex%20objectives%20should%20converge%20to%20the%20same%20optimum%20value%20when%20solved%20accurately%3B%20a%20linear%20objective%20can%20have%20more%20than%20one%20equally%20optimal%20solution.%20Compare%20moment%20residuals%20and%20bounds%2C%20then%20vary%20angle.%20Changes%20across%20angles%20can%20reflect%20changed%20leverage%20or%20active%20capacity%20bounds%3B%20changes%20across%20starts%20at%20a%20fixed%20angle%20can%20expose%20solver%20accuracy%20or%20nonuniqueness.%20No%20single%20force%20vector%20answers%20every%20chosen%20angle.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20**Exercise%208%20(original%20three-flexor%20task).**%20A%2010%20Nm%20elbow%20moment%20is%20supplied%20by%20brachialis%2C%20brachioradialis%2C%20and%20biceps%20brachii%2C%20with%20maximum%20forces%20(1000%2C%20250%2C%20700)%20N%2C%20moment%20arms%20(2%2C%205%2C%204)%20cm%2C%20and%20PCSA%20(33%2C%208%2C%2023)%20cm%C2%B2.%20Estimate%20forces%20using%20different%20cost%20functions%20and%20test%20sensitivity%20to%20initial%20values.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%208%0A%20%20%20%20Convert%20moment%20arms%20to%20metres%20and%20impose%20%240.02F_1%2B0.05F_2%2B0.04F_3%3D10%24%20with%20the%20stated%20force%20bounds.%20For%20minimum%20total%20force%2C%20the%20largest%20moment%20arm%20can%20supply%20the%20whole%20moment%3A%20%24(0%2C200%2C0)%24%20N.%20For%20minimum%20squared%20force%2C%20stationarity%20gives%20%24F_i%3D%5Clambda%20r_i%24%2C%20and%20the%20constraint%20sets%20%24%5Clambda%3D10%2F%5Csum%20r_i%5E2%24%3A%20%24(44.444%2C111.111%2C88.889)%24%20N.%20For%20squared%20stress%2C%20%24F_i%3D%5Clambda%20r_i%20A_i%5E2%24%20and%20the%20same%20constraint%20gives%20%24(151.040%2C22.191%2C146.741)%24%20N.%20All%20satisfy%20the%20capacity%20bounds.%0A%0A%20%20%20%20For%20the%20retained%20cubic%20force%2Fmaximum-moment%20criterion%2C%20use%20%24M_%7Bmax%2Ci%7D%3Dr_iF_%7B0%2Ci%7D%24%3A%20stationarity%20gives%20%24F_i%5Cpropto%5Csqrt%7Br_iM_%7Bmax%2Ci%7D%5E3%7D%24%2C%20yielding%20%24(65.459%2C51.139%2C153.346)%24%20N.%20Repeat%20with%20zero%2C%20half-capacity%2C%20and%20the%20feasible%20vector%20%24(0%2C200%2C0)%24%20as%20starts%3B%20compare%20the%20objective%20and%20the%20moment%20residual%2C%20not%20just%20printed%20digits.%20These%20calculations%20show%20how%20changing%20the%20objective%20changes%20the%20predicted%20sharing%20even%20when%20the%20net%20moment%20is%20identical.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Checkpoint%20questions%0A%0A%20%20%20%201.%20Is%20every%20stationary%20point%20a%20minimum%3F%0A%20%20%20%202.%20What%20distinguishes%20a%20local%20optimum%20from%20a%20global%20one%3F%0A%20%20%20%203.%20Why%20is%20muscle%20redundancy%20underdetermined%3F%0A%20%20%20%204.%20What%20must%20be%20checked%20after%20the%20solver%20returns%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Checkpoint%20answers%0A%20%20%20%201.%20No%3B%20it%20can%20be%20a%20maximum%20or%20saddle%2Finflection%20point.%0A%20%20%20%202.%20The%20comparison%20domain%3A%20a%20neighborhood%20versus%20the%20whole%20feasible%20set.%0A%20%20%20%203.%20There%20are%20more%20unknown%20muscle%20forces%20than%20independent%20balance%20equations.%0A%20%20%20%204.%20Status%2C%20feasibility%2C%20bounds%2C%20residuals%2C%20and%20sensitivity%20to%20numerical%20settings.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Problems%0A%0A%20%20%20%201.%20Give%20the%20optimal%20cut%20and%20volume%20for%20a%2030%20cm%20square%20cardboard.%0A%20%20%20%202.%20Give%20the%20local%20maximum%20and%20minimum%20locations%20of%20the%20cubic%20in%20Exercise%205.%0A%20%20%20%203.%20Two%20muscles%20with%20arms%200.02%20and%200.04%20m%20have%20capacities%2050%20N%20each.%20Can%20they%20supply%204%20Nm%20together%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Final%20answers%0A%20%20%20%201.%20Cut%205%20cm%3B%20volume%202000%20cm%C2%B3.%0A%20%20%20%202.%20Maximum%20at%20x%3D2%3B%20minimum%20at%20x%3D3.%0A%20%20%20%203.%20No%3B%20their%20maximum%20combined%20moment%20is%203%20Nm.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Go%20deeper%0A%0A%20%20%20%20%5BNEOS%20optimization%20guide%5D(https%3A%2F%2Fneos-guide.org%2Fguide%2F)%20%E2%80%94%20classify%20the%20objective%20and%20constraints%20before%20choosing%20a%20solver.%20%5BSciPy%20optimization%20tutorial%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Ftutorial%2Foptimize.html)%20%E2%80%94%20current%20algorithms%20and%20result%20diagnostics.%0A%0A%20%20%20%20%23%23%23%20Video%20lectures%20on%20the%20Internet%0A%0A%20%20%20%20%5BGradient%20descent%2C%203Blue1Brown%5D(https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DIHZwWFHWa-w)%20%E2%80%94%20visualize%20a%20cost%20landscape%20and%20how%20the%20gradient%20directs%20a%20local%20search%3B%20the%20mathematical%20idea%20also%20applies%20to%20these%20mechanical%20examples.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20References%0A%0A%20%20%20%20-%20Bergstra%20B%2C%20Bengio%20Y%20(2012)%20%5BRandom%20Search%20for%20Hyper-Parameter%20Optimization%5D(http%3A%2F%2Fjmlr.csail.mit.edu%2Fpapers%2Fvolume13%2Fbergstra12a%2Fbergstra12a.pdf).%20Journal%20of%20Machine%20Learning%20Research%2C%2013%2C%20281-305.%0A%0A%20%20%20%20-%20Crowninshield%20RD%2C%20Brand%20RA%20(1981)%20%5BA%20physiologically%20based%20criterion%20of%20muscle%20force%20prediction%20in%20locomotion%5D(https%3A%2F%2Fpubmed.ncbi.nlm.nih.gov%2F7334039).%20Journal%20of%20Biomechanics%2C%2014%2C%20793%E2%80%93801.%0A%0A%20%20%20%20-%20Downey%20AB%20(2014)%20%5BPhysical%20Modeling%20in%20MATLAB%5D(http%3A%2F%2Fgreenteapress.com%2Fwp%2Fphysical-modeling-in-matlab-2e%2F).%202nd%20edition.%20Green%20Tea%20Press.%0A%0A%20%20%20%20-%20Herzog%20W%20(1987)%20%5BIndividual%20muscle%20force%20estimations%20using%20a%20non-linear%20optimal%20design%5D(https%3A%2F%2Fpubmed.ncbi.nlm.nih.gov%2F3682873).%20J%20Neurosci%20Methods%2C%2021%2C%20167-179.%0A%0A%20%20%20%20-%20Glitsch%20U%2C%20Baumann%20W%20(1997)%20%5BThe%20three-dimensional%20determination%20of%20internal%20loads%20in%20the%20lower%20extremity%5D(https%3A%2F%2Fpubmed.ncbi.nlm.nih.gov%2F9456380).%20Journal%20of%20Biomechanics%2C%2030%2C%201123%E2%80%931131.%0A%0A%20%20%20%20-%20Kitchin%20J%20(2013)%20%5Bpycse%20-%20Python%20Computations%20in%20Science%20and%20Engineering%5D(http%3A%2F%2Fkitchingroup.cheme.cmu.edu%2Fpycse%2F).%0A%0A%20%20%20%20-%20Kiusalaas%20(2013)%20%5BNumerical%20methods%20in%20engineering%20with%20Python%203%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3DaJkXoxxoCoUC).%203rd%20edition.%20Cambridge%20University%20Press.%0A%0A%20%20%20%20-%20Nigg%20BM%20and%20Herzog%20W%20(2006)%20%5BBiomechanics%20of%20the%20Musculo-skeletal%20System%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3DhOIeAQAAIAAJ%26dq%3Deditions%3AISBN0470017678).%203rd%20Edition.%20Wiley.%0A%0A%20%20%20%20-%20Robertson%20G%2C%20Caldwell%20G%2C%20Hamill%20J%2C%20Kamen%20G%20(2013)%20%5BResearch%20Methods%20in%20Biomechanics%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3DgRn8AAAAQBAJ).%202nd%20Edition.%20Human%20Kinetics.%0A%0A%20%20%20%20-%20Seireg%20A%2C%20Arvikar%20RJ%20(1973)%20%5BA%20mathematical%20model%20for%20evaluation%20of%20forces%20in%20lower%20extremeties%20of%20the%20musculo-skeletal%20system%5D(https%3A%2F%2Fpubmed.ncbi.nlm.nih.gov%2F4706941).%20Journal%20of%20Biomechanics%2C%206%2C%20%20313%E2%80%93322%2C%20IN19%E2%80%93IN20%2C%20323%E2%80%93326.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
2e4e12bdfb2aad482b78e4d03a8e8856