Curve Fitting with Matlab

Matlab has a curve fitting toolbox (installed on machines in Hicks, but perhaps not elsewhere on campus - as of Sept. 2007), that can be used to fit any curve to data.  This page describes how to do this with data collected (i.e., from an oscilloscope).

We'll do two examples:

Consider the data from a square wave input driving an RC lowpass circuit.

plot(t,vin,t,vout);
legend('Vin','Vout');
title('RC Circuit with Square Wave Input');
ylabel('Volts');
xlabel('Time, (seconds)');

Raw Data 

Easy Fit: Finding time constant from falling edge.

Let's say we want to find the time constant of the circuit from the falling edge data.  Immediately we have a problem not only because our data rises and falls, but because none of the falling edges aligns with t=0. 

First lets get the falling edge data.  By using the magnify option on the plot (or by looking through the data manually) we can find where the drop occurs.  The diagram below shows the falling edge that is near t=-4 seconds.

Zoom on falling edge 

From this diagram it is obvious that the falling edge occurs at about t=-4.34 seconds.  By expanding about the next rising edge, we can see that we want the data from -4.34<t<-1.22 seconds.

tIndex=find((t>-4.32) & (t<-1.22));  %Get only the chosen values of time
t1=t(tIndex);		%Get new time vector	
vin1=vin(tIndex);	%Get Vin
vout1=y(tIndex);	%Get y (Vout)
plot(t1,vin1,t1,vout);
legend('Vin','Vout');
title('RC Circuit with Square Wave Input, Falling only');
ylabel('Volts');
xlabel('Time, (seconds)');

Falling Edge

Now we need to adjust the data so t=0 corresponds to the first point.  This is easily done by just subtracting the first time value from each of the other time values.  This is shown below.

t1=t1-t1(1);            %Subtract first time value, so t1 begins at 0.
plot(t1,vin1,t1,vout1);
legend('Vin','Vout');
title('RC Circuit with Square Wave Input, Falling only, start at t=0');
ylabel('Volts');
xlabel('Time, (seconds)');

Falling Edge, time adjusted

Doing the curve fit. 

We would like to fit the data to the equation:

Equation

To do this we will use Matlab's curve fitting tool, "cftool."  To do this enter "cftool" at the Matlab prompt.  You should get a window like the one below.

cftool window 

Let's start by importing the data.  Hit the "Data" button, and then choose "t1" as the "X data", and "vout1" as the "Y data."  Then hit the "Create data set" button.

vout1 vs t 

Your screen should look like the one below:

CFTool data 

Now choose "Fitting."  Set your "Fit Name" to "Exponential Fit" and your "Type of fit" to "Exponential."  Choose "a*exp(b*x)" as your fit.

Fitting window

When doing non-linear curve fitting, it is helpful to give the program as much information as possible.  It is apparent from looking at the data that "a" is about 1 and "b" is about -2.  So open the "Fit options" and enter these initial guesses.

Fit Options

Close the fit options, and start the fit, by hitting "Apply."  The final fit looks good:

Fitted Data

and we get a=0.92 , and b=-2.0 as our best-fit parameters.

Best Fit Parameters

Hard Fit: Finding time constant from rising edge.

In the same way as before, let's get the rising edge data, and make it start at t=0.

Rising Edge Raw Data 

Doing the curve fit. 

This time would like to fit the data to the equation:

Asymptotic equation

Unfortunately this isn't one of Matlab's standard fitting equations.  So... Choose "Fit Name" as "Asymptotic Fit."  Choose "Type of fit" as "Custom Equations."  Hit "New Equation," and select general equations.  Now enter the equation we want (with initial guesses for "a" and "b"):

Custom Equation 

Now, do the fit.  Again it looks quite good.

Fitted Data

Fitted Parameters 

Bad Fit

See what happens with aa bad initial guess for "b."

Generic Fit

Using the examples above, you would use a different fit for a decaying exponential and an increasing (asymptotic) exponential.  However, you know that the generic first order step response given by:

Generic 1 

 or

Generic Eq. 2

can be used for either both cases.  This might simplify your work.

Concluding Remarks

You can fit just about any equation with this tool.  However, it is more successful if  you have somewhat accurate initial guesses for you parameters.  Sometimes this is easy, sometimes not so.