Example 2: An Asymptotic Function

  1. The sample data for this example is shown in the figure below. Once again, the goal is to find the best curve that will fit these points.

    Sample Data for Limiter

  2. The data is characterized by the following equation:

    Limiter Equation

    A little background information on the equation: this is an equation to describe the behavior of a limiter. A limiter is a circuit that allows signals below a set value to pass unaffected. After this set level has been reached or supassed, the signal becomes saturated.

  3. Use the following code to generate the sample data. Paste this code into the command prompt.

    p=4;
    L=2;
     
    %this is the input array
    Vin = 0:0.1:5-.1;
     
    %the rand command is used here to introduce some error into 
    %the output because real data is never perfect
    Vout  = Vin.*(1+abs(Vin./L).^p).^(-1/p)+rand([1,50])/100;
     
    %plot the data
    figure
    scatter(Vin,Vout);
  4. Next, we need to define the limiter equation before we can use lsqcurvefit. Open a new m-file and paste in the following. After pasting in the code, save the m-file and remember which directory you saved it under.

    function output = limiter(param,input)
    p = param(1);
    L = param(2);
    output  = input.*(1+abs(input./L).^p).^(-1/p);
  5. Now, we can use the lsqcurvefit command. Remember to set the Matlab current directory to wherever you saved the previous m-file. Cut and paste this code into the command prompt.

    %the better your initial condition guesses are, the faster
    %the lsqcurvefit command will converge onto a solution
    initialConditions = [4,2];
     
    %newParameters is an array containing the optimal values that will
    %generate a curve that will best fit your data
     
    %error is the sum of the error squared.  the lower this number is, the better
    [newParameters,error] = lsqcurvefit(@limiter, initialConditions,Vin,Vout);

    You will probably see the following message when you execute these commands, which is normal:

    Optimization terminated: first-order optimality less than OPTIONS.TolFun,
    and no negative/zero curvature detected in trust region model

  6. The newParameters variable contains the calculated values for p and L. Use the following code to see how the curve fits the data using these newly calculated parameters:

    figure
    scatter(Vin,Vout) %plot the scatter plot
    hold %hold the figure
     
    %use new parameters to get new output values
    Vout2 = limiter(newParameters,Vin);
     
    %plot the curve using the color red
    plot(Vin,Vout2,'r')

    Looks like it was a pretty good fit!

    First Attempt

  7. We can try making it a better fit by using the method shown in the previous example. I will leave this up to you to finish. Good luck!

Pages: 1 2 3