{"id":5181,"date":"2013-12-06T13:21:26","date_gmt":"2013-12-06T12:21:26","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5181"},"modified":"2013-12-06T16:11:15","modified_gmt":"2013-12-06T15:11:15","slug":"simple-nonlinear-least-squares-curve-fitting-in-julia","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5181","title":{"rendered":"Simple nonlinear least squares curve fitting in Julia"},"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 <a href=\"http:\/\/julialang.org\/\">Julia<\/a> version. For other versions,see the list below<\/p>\n<ul>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=5218\">Simple nonlinear least squares curve fitting in Maple<\/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>Julia solution using Optim.jl<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/JuliaOpt\/Optim.jl\/blob\/master\/README.md\">Optim.jl<\/a> is a free Julia package that contains a suite of optimisation routines written in pure Julia. \u00a0If you haven&#8217;t done so already, you&#8217;ll need to install the Optim package<\/p>\n<pre>Pkg.add(\"Optim\")<\/pre>\n<p>The solution is almost identical to the example given in the curve fitting demo of the Optim.jl readme file:<\/p>\n<pre>using Optim\r\n\r\nmodel(xdata,p) = p[1]*cos(p[2]*xdata)+p[2]*sin(p[1]*xdata)\r\n\r\nxdata = [-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]\r\n\r\nbeta, r, J = curve_fit(model, xdata, ydata, [1.0, 0.2])\r\n# beta = best fit parameters\r\n# r = vector of residuals\r\n# J = estimated Jacobian at solution\r\n\r\n@printf(\"Best fit parameters are: %f and %f\",beta[1],beta[2])\r\n@printf(\"The sum of squares of residuals is %f\",sum(r.^2.0))<\/pre>\n<p>The result is<\/p>\n<pre>Best fit parameters are: 1.881851 and 0.700230\r\n@printf(\"The sum of squares of residuals is %f\",sum(r.^2.0))<\/pre>\n<p><strong>Notes<\/strong><\/p>\n<p>I used Julia version 0.2 on 64bit Windows 7 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":[59,7],"tags":[],"class_list":["post-5181","post","type-post","status-publish","format-standard","hentry","category-julia","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1lz","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5181","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=5181"}],"version-history":[{"count":9,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5181\/revisions"}],"predecessor-version":[{"id":5297,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5181\/revisions\/5297"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}