Example 1: Continued

  1. Use this code to generate the sample data. You can insert it directly into the command prompt window.

    %x data
    x = 0:0.1:5-0.1;
     
    %y data
    %the rand command is used here to introduce some error into 
    %the output because real data is never perfect
    y = -x.^3 +5.*x.^2+x-15 + rand([1,50])/2; 
     
    %plot the data as a scatter plot
    figure
    scatter(x,y)
  2. Next, we need to define the polynomial 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= myPolyCurve (param,input)
    a = param(1);
    b = param(2);
    c=  param(3);
    d=  param(4);
    % this is the 3rd order polynomial equation here
    output = a.*input.^3 + b.*input.^2 + c.*input.^1 + d;
  3. Now we are ready to 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 = [-10 5 10 -15];
     
    %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(@myPolyCurve, initialConditions,x,y);

    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

  4. The newParameters variable contains the calculated values for a,b,c, and d. Use the following code to see how the curve fits the data:

    figure
    scatter(x,y) %plot the scatter plot
    hold %hold the figure
     
    %use new parameters to get new output values
    y2 = myPolyCurve(newParameters,x); 
     
    %plot the new data using the color red
    plot(x,y2,'r')

    Looks like it was a decent fit.

    First Attempt

  5. But can we make it fit even better? We sure can. We can include a lower bound, upper bound, and even change the optimization configuration to get a better fit. Of course, doing so will probably require more computing resources, so there is a tradeoff. So if you find that your data isn’t fitting as well as you thought it would, you can try changing some settings to see if it helps. So lets give that a try. Enter these lines of code into the command prompt:

    %configure the optimset for use with lsqcurvefit
    options = optimset('lsqcurvefit');
     
    %increase the number of function evaluations for more accuracy
    options.MaxFunEvals = 500;
     
    lb = [ -20 -20 -20 -20]; %define the lower bound
    ub =[20 20 20 20]; %define the upper bound
     
    %re-evaluate the curve fit with new options
    [newParameters2,error2] = lsqcurvefit(@myPolyCurve, initialConditions,x,y,lb,ub,options);
     
    %use new parameters to get new output values
    y3 = myPolyCurve(newParameters2,x);
     
    %%% plot all the data on the same figure %%%
    figure
    scatter(x,y) %plot the scatter plot
    hold %hold the figure
     
    %plot the new data using the color red
    plot(x,y2,'r') 
     
    %plot the new data using the color green
    plot(x,y3,'g')

    The red curve is from the first attempt, and the green curve is from the second atempt. Just from visual inspection, it looks like the curve fits even better now!

    Second Attempt

  6. Also, if you compare the values for error1 and error2, you will notice that error2 is smaller than error1, which suggests that the second trial will yield a better fit! If you want to see the values for these two variables, you can type error1 and error2 respectively at the command prompt.

And that’s how you use lsqcurvefit to fit your data. This is the end of the first example. Keep reading to see the next example!

Pages: 1 2 3