help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Curve fitting of data from text file


From: Allen.Windhorn
Subject: RE: Curve fitting of data from text file
Date: Wed, 24 Mar 2010 14:50:44 -0500

 

-----Original Message-----
From: socware [mailto:address@hidden 

I want to do curve fitting and get the value of the coefficients which
gives
me minimum error for the equation:

I_d=a*(V_g-b)^2

leasqr is a lovely tool but may be overkill for what you are doing.
polyfit is probably a better choice for truly linear problems (which
yours is).  For a dummy set of data with X from 0 to 14 and Y from 0 to
25 I did:

> polyfit(X.^2, Y, 1)
ans =

   0.121179  -0.030751

which says Y = 0.121179*X^2-0.030751.  You can drop the last term 
without appreciable error.  We don't use a 2nd degree polynomial
because it introduces a linear term that distorts the X^2 term.

However, to get what you really want you can go back to the 
principles of least square fit, which say to minimize the sum of 
squared differences between your estimate and the data.  This 
means you set the derivative of that squared error to zero.

err2 = sum(a*Xi^2-Yi)^2, where a is a parameter, or by expansion,
err2 = sum(a^2*Xi^4-2*a*Xi^2*Yi+Yi^2), taking derivative w.r.t. a,
d(err2)/da = 2*a*sum(Xi^4)-2*sum(Xi^2Yi), setting to zero and
solving for a:
a = sum(Xi^*2Yi)/sum(Xi^4)

Doing this in Octave:

> X2 = X.^2; %This should make the data a straight line
> X4 = X2.^2; %4th power for denominator term
> X2Y = X2.*Y; %Numerator term
> X4s = sum(X4) %Take the sums of the arrays
X4s = 2.8890e+006
> X2Ys = sum(X2Y)
X2Ys = 3.4933e+005
> a = X2Ys/X4s %Compute the best fit parameter
a =  0.12092
> Yc = a.*(X.^2); %Estimated values to check the fit
> plot(X, Y, "r", X, Yc, "g") %View the fit

The a = 0.12092 is pretty close to the 0.12117 we got above.

Once you start adding correction terms (leakage, saturation, etc) 
you may have to go back to polyfit or leasqr.

Regards,
Allen



reply via email to

[Prev in Thread] Current Thread [Next in Thread]