{"id":5218,"date":"2013-12-06T13:21:47","date_gmt":"2013-12-06T12:21:47","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5218"},"modified":"2013-12-06T16:11:57","modified_gmt":"2013-12-06T15:11:57","slug":"simple-nonlinear-least-squares-curve-fitting-in-maple","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5218","title":{"rendered":"Simple nonlinear least squares curve fitting in Maple"},"content":{"rendered":"<p>A question I get asked a lot is &#8216;How can I do nonlinear least squares curve fitting in X?&#8217; where X might be MATLAB, Mathematica or a whole host of alternatives. \u00a0Since this is such a common query, I thought I&#8217;d write up how to do it for a very simple problem in several systems that I&#8217;m interested in<\/p>\n<p>This is the Maple version. For other versions,see the list below<\/p>\n<ul>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=5181\">Simple nonlinear least squares curve fitting in Julia<\/a><\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=5180\">Simple nonlinear least squares curve fitting in Mathematica<\/a><\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=5196\">Simple nonlinear least squares curve fitting in MATLAB<\/a><\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=5215\">Simple nonlinear least squares curve fitting in Python<\/a><\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=5254\">Simple nonlinear least squares curve fitting in R<\/a><\/li>\n<\/ul>\n<p><strong>The problem<\/strong><\/p>\n<pre>xdata = -2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9\r\nydata = 0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001<\/pre>\n<p>and you&#8217;d like to fit the function<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/walkingrandomly.com\/wp-content\/ql-cache\/quicklatex.com-17f46b025dd91e024cf2dc04211e19ba_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#70;&#40;&#112;&#95;&#49;&#44;&#112;&#95;&#50;&#44;&#120;&#41;&#32;&#61;&#32;&#112;&#95;&#49;&#32;&#92;&#99;&#111;&#115;&#40;&#112;&#95;&#50;&#32;&#120;&#41;&#43;&#112;&#95;&#50;&#32;&#92;&#115;&#105;&#110;&#40;&#112;&#95;&#49;&#32;&#120;&#41;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"297\" style=\"vertical-align: -4px;\"\/><\/p>\n<p>using nonlinear least squares. \u00a0You&#8217;re starting guesses for the parameters are p1=1 and P2=0.2<\/p>\n<p>For now, we are primarily interested in the following results:<\/p>\n<ul>\n<li>The fit parameters<\/li>\n<li>Sum of squared residuals<\/li>\n<\/ul>\n<p>Future updates of these posts will show how to get other results such as confidence intervals. Let me know what you are most interested in.<\/p>\n<p><strong>Solution in Maple<\/strong><\/p>\n<p>Maple&#8217;s user interface is quite user friendly and it uses non-linear optimization routines from The Numerical Algorithms Group under the hood. Here&#8217;s the code to get the parameter values and sum of squares of residuals<\/p>\n<pre>with(Statistics):\r\n\r\nxdata := Vector([-2, -1.64, -1.33, -.7, 0, .45, 1.2, 1.64, 2.32, 2.9], datatype = float):\r\nydata := Vector([.699369, .700462, .695354, 1.03905, 1.97389, 2.41143, 1.91091, .919576,  \r\n  -.730975, -1.42001], datatype = float):\r\nNonlinearFit(p1*cos(p2*x)+p2*sin(p1*x), xdata, ydata, x, initialvalues = [p1 = 1, p2 = .2], \r\n   output = [parametervalues, residualsumofsquares])<\/pre>\n<p>which gives the result<\/p>\n<pre>[[p1 = 1.88185090465902, p2 = 0.700229557992540], 0.05381269642]<\/pre>\n<p>Various other outputs can be specified in the output vector such as:<\/p>\n<ul>\n<li>solutionmodule<\/li>\n<li>degreesoffreedom<\/li>\n<li>leastsquaresfunction<\/li>\n<li>parametervalues<\/li>\n<li>parametervector<\/li>\n<li>residuals<\/li>\n<li>residualmeansquare<\/li>\n<li>residualstandarddeviation<\/li>\n<li>residualsumofsquares.<\/li>\n<\/ul>\n<p>The meanings of the above are mostly obvious.\u00a0 In those cases where they aren&#8217;t, you can look them up in the documentation link below.<\/p>\n<p><strong>Further documentation<\/strong><\/p>\n<p>The full documentation for Maple&#8217;s NonlinearFit command is at <a href=\"http:\/\/www.maplesoft.com\/support\/help\/Maple\/view.aspx?path=Statistics%2fNonlinearFit\">http:\/\/www.maplesoft.com\/support\/help\/Maple\/view.aspx?path=Statistics%2fNonlinearFit<\/a><\/p>\n<p><strong>Notes<\/strong><\/p>\n<p>I used Maple 17.02 on 64bit Windows to run the code in this post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A question I get asked a lot is &#8216;How can I do nonlinear least squares curve fitting in X?&#8217; where X might be MATLAB, Mathematica or a whole host of alternatives. \u00a0Since this is such a common query, I thought I&#8217;d write up how to do it for a very simple problem in several systems [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[25,4],"tags":[],"class_list":["post-5218","post","type-post","status-publish","format-standard","hentry","category-maple","category-math-software"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1ma","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5218"}],"version-history":[{"count":21,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5218\/revisions"}],"predecessor-version":[{"id":5298,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5218\/revisions\/5298"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}