{"id":4929,"date":"2015-11-17T12:39:02","date_gmt":"2015-11-17T17:39:02","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=4929"},"modified":"2016-02-10T11:27:34","modified_gmt":"2016-02-10T16:27:34","slug":"the-martian-part-3-optimizing-the-trajectory","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2015\/11\/17\/the-martian-part-3-optimizing-the-trajectory\/","title":{"rendered":"The Martian &#8211; Part 3 &#8211; Optimizing the Trajectory"},"content":{"rendered":"<p>After <a href=\"https:\/\/blogs.mathworks.com\/seth\/2015\/11\/03\/the-martian-can-you-save-mark-watney-part-1\">finding the launch date of the mission<\/a> and <a href=\"https:\/\/blogs.mathworks.com\/seth\/2015\/11\/11\/the-martian-part-2-simulating-the-spacecraft-trajectory\">putting together a simulation of the spacecraft trajectory<\/a>, it's time to see how we can optimize this trajectory.<\/p>\r\n\r\n<p>Let's bring him home!<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q4\/bringHimHome.png\" alt=\"Bring him home!\" \/><p>\r\n\r\n<p><strong>Optimizing the Trajectory<\/strong><\/p>\r\n\r\n<p>First I have to confess that I am not expert in optimization. That being said, I was pretty impressed by how easy it was to get it to work using our tools.<\/p>\r\n\r\n<p>Since this problem is actual rocket science and I am not a rocket scientist, I decided to not begin with the full <a href=\"https:\/\/youtu.be\/lcyfDRYKDJM\">Rich Purnell Maneuver<\/a>. Instead I started with a problem of more reasonable scale: the original planned trajectory for the Ares 3 mission, which was to get to Mars in 124 days, and come back to Earth exactly one year after the launch date.<\/p>\r\n\r\n<p>The simulation I created, which I showed last week, is set up so that it takes as input a vector of thrust angles, and logs the position of the ship.<\/p> \r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q4\/modelSetup.png\" alt=\"The model\" \/><p>\r\n\r\n<p>To determine how good the trajectory is, I have two criteria: I want to be at a specific position and velocity relative to Mars on day 124, and at a specific position and velocity relative to Earth on day 365. So my cost function code looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q4\/costFunction.png\" alt=\"Cost Function\" \/><p>\r\n\r\n<p>Notice that I decided to scale the inputs and outputs so that they are in the amplitude range of 1. Someone once told me that default settings for optimization algorithms work better using that scale.<\/p>\r\n\r\n<p>Then all I need to do is pass this cost function to <a href=\"https:\/\/www.mathworks.com\/help\/optim\/ug\/fmincon.html\">fmincon<\/a>, from the <a href=\"https:\/\/www.mathworks.com\/products\/optimization\/\">Optimization Toolbox<\/a>. I took advantage of the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/get-started-with-fast-restart.html\">Fast Restart<\/a> feature to speed up my workflow; for this specific model the initialization takes more time than the simulation itself, so Fast Restart significantly reduces the processing time it takes for the optimization to converge:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q4\/runOpt.png\" alt=\"Running the optimization\" \/><p>\r\n\r\n<p>After about 10 minutes, I marvelously obtained a nice trajectory that does the fly-by Mars on day 124 and comes back to Earth after a year:\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q4\/FinalTrajectory.gif\" alt=\"Result from the optimization\" \/><p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p><\/p>\r\n\r\n<p>If you are interested, you can download the whole optimization problem <a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q4\/TheMartianOptimizedTrajectory.zip\">here<\/a>. I am still very curious... if someone can simulate the full <a href=\"https:\/\/youtu.be\/lcyfDRYKDJM\">Rich Purnell Maneuver<\/a>, I will keep my promise of providing nice MathWorks swag!<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q4\/FinalTrajectory.gif\" onError=\"this.style.display ='none';\" \/><\/div><p>After finding the launch date of the mission and putting together a simulation of the spacecraft trajectory, it's time to see how we can optimize this trajectory.\r\n\r\nLet's bring him... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2015\/11\/17\/the-martian-part-3-optimizing-the-trajectory\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[50,71,181],"tags":[383,438],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4929"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=4929"}],"version-history":[{"count":10,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4929\/revisions"}],"predecessor-version":[{"id":5097,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4929\/revisions\/5097"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=4929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=4929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=4929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}