<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>NMATH curve fitting Archives - CenterSpace</title>
	<atom:link href="https://www.centerspace.net/tag/nmath-curve-fitting/feed" rel="self" type="application/rss+xml" />
	<link>https://www.centerspace.net/tag/nmath-curve-fitting</link>
	<description>.NET numerical class libraries</description>
	<lastBuildDate>Tue, 01 Mar 2016 18:48:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>
<site xmlns="com-wordpress:feed-additions:1">104092929</site>	<item>
		<title>Polynomial Curve Fitting</title>
		<link>https://www.centerspace.net/polynomial-curve-fitting</link>
					<comments>https://www.centerspace.net/polynomial-curve-fitting#respond</comments>
		
		<dc:creator><![CDATA[Ken Baldwin]]></dc:creator>
		<pubDate>Thu, 10 Sep 2009 17:54:38 +0000</pubDate>
				<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[MATLAB polyfit in C#]]></category>
		<category><![CDATA[NMATH curve fitting]]></category>
		<category><![CDATA[polyfit in C#]]></category>
		<category><![CDATA[polynomial curve fitting]]></category>
		<category><![CDATA[polynomial curve fitting C#]]></category>
		<category><![CDATA[polynomial fitting MATLAB]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=188</guid>

					<description><![CDATA[<p><img src="https://www.centerspace.net/blog/wp-content/uploads/2009/09/SixthDegPolynomialFit.png" alt="Sixth degree polynomial fit using Polynomial Least Squares" class="excerpt" />Many programmers, modelers, and scientists need to convert models built in MATLAB to another compiled language.  This note shows how to convert from MATLAB's polyfit() function to NMath's PolynomialLeastSquares().  Code examples are provided. </p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/polynomial-curve-fitting">Polynomial Curve Fitting</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A customer recently asked how to reproduce the results of the MATLAB <code>polyfit</code> function in NMath. This is easily done using NMath in C#.</p>
<table title="MATLAB and NMath equivalence" >
<th> MATLAB </th>
<th> NMath </th>
<tr>
<td> Standard package </td>
<td> <code>CenterSpace.NMath.Analysis </code></td>
</tr>
<tr>
<td> <code> polyfit( x, y, degree )</code> </td>
<td> <code> PolynomialLeastSquares( degree, x, y ) </code></td>
</tr>
<tr>
<td colspan="2"> Notes: polynomial coefficients are returned in the reverse order. </p>
<td>
<tr>
<td colspan="2">
<hr>
</tr>
</table>
<p>For example, here&#8217;s some MATLAB code for fitting a polynomial of degree 6 to some data, and then evaluating the fitted polynomial at a point (<code>x = 0.25</code>).</p>
<pre lang="matlab">
 x = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3 
        1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5]

 y = [0 0.112462916018285 0.222702589210478 0.328626759459127 0.428392355046668 0.520499877813047 0.603856090847926 0.677801193837419 0.742100964707661 0.796908212422832 0.842700792949715 0.880205069574082 0.910313978229635 0.934007944940652 0.952285119762649 0.966105146475311 0.976348383344644 0.983790458590775 0.989090501635731 0.992790429235257 0.995322265018953 0.997020533343667 0.998137153702018 0.998856823402643 0.999311486103355 0.999593047982555]

p = polyfit(x,y,6)
p = 0.0084   -0.0983    0.4217   -0.7435    0.1471    1.1064    0.0004
f = polyval(p, 0.25); 
f = 0.2762
</pre>
<p>Here&#8217;s the equivalent C# NMath code:</p>
<pre lang="csharp">
#using CenterSpace.NMath.Analysis
public void FitData()
{
  DoubleVector x = new DoubleVector("[0 0.1 0.2 0.3 0.4 0.5 
    0.6 0.7 0.8 0.9 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 
    2.2 2.3 2.4 2.5]");

  DoubleVector y = new DoubleVector("[0 0.112462916018285 0.222702589210478 0.328626759459127   0.428392355046668 0.520499877813047 0.603856090847926 0.677801193837419 0.742100964707661 0.796908212422832 0.842700792949715 0.880205069574082 0.910313978229635 0.934007944940652 0.952285119762649 0.966105146475311 0.976348383344644 0.983790458590775 0.989090501635731 0.992790429235257 0.995322265018953 0.997020533343667 0.998137153702018 0.998856823402643 0.999311486103355 0.999593047982555]");

  PolynomialLeastSquares pls = new PolynomialLeastSquares( 6, x, y );
  Console.WriteLine( pls.Coefficients );
  Console.WriteLine();
  Console.WriteLine( pls.FittedPolynomial.Evaluate( 0.25 ) );
}
</pre>
<p>This creates the output. </p>
<pre class="code">
[ 0.000441173961987631 1.10644604462547 0.147104056633056 
  -0.743462849129717 0.421736169818002 -0.0982995753367523 
  0.00841937177923124 ]

0.276183548385269
</pre>
<figure id="attachment_1845" aria-describedby="caption-attachment-1845" style="width: 440px" class="wp-caption aligncenter"><img decoding="async" src="https://www.centerspace.net/blog/wp-content/uploads/2009/09/SixthDegPolynomialFit.png" alt="Sixth degree polynomial fit using Polynomial Least Squares" title="Sixth degree polynomial fit using Polynomial Least Squares" width="440" class="size-full wp-image-1845" srcset="https://www.centerspace.net/wp-content/uploads/2009/09/SixthDegPolynomialFit.png 462w, https://www.centerspace.net/wp-content/uploads/2009/09/SixthDegPolynomialFit-300x265.png 300w" sizes="(max-width: 462px) 100vw, 462px" /><figcaption id="caption-attachment-1845" class="wp-caption-text">6th degree polynomial fit using Polynomial Least Squares.</figcaption></figure>
<p>Note that the order of the coefficient vector in NMath is reversed relative that returned from MATLAB&#8217;s <code>polyfit()</code> function.  In NMath, the constant is at index <code>0</code> and the leading coefficient is at index <code>Coefficients.Length - 1</code>. If you wish, you can use the <code>Reverse()</code> method on the coefficient vector to reverse the ordering.</p>
<p>-Ken</p>
<p><strong> Notes </strong><br />
 Visualization provided in partnership with <a href="https://www.nevron.com/products-dot-net-vision.aspx">Nevron</a></p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/polynomial-curve-fitting">Polynomial Curve Fitting</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/polynomial-curve-fitting/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">188</post-id>	</item>
	</channel>
</rss>
