4. Non-Linear Two Gaussian Functions

 

Use the file “nltwogauss.mat”, it contains data that should be fitted to a function of the form:

 

Parameters

Start

Final

65

54.9846

0.47

0.6144

5

12.2136

50

51.9718

0.25

0.2674

5

9.9135

ss

 

16.5612

ssdp

 

0.0207

 

 

 

 

 

 

 

 

 

 

 

 

 

 

%Program to solve non-linear Curve homework problem #4

%data for a function F=a1(e^(-a2(x-a3)^2)

%version 2015-09-30 D.W. Donovan

 

clear all;

load('nltwogauss.mat','-ascii');

x=nltwogauss(:,1);

fx=nltwogauss(:,2);

 

figure

hold on;

t1b='Non-Linear Curve Fitting Homework Problem #4';

t2b='Raw Data to be Fitted to F(x) = a1(exp(-a2(x-a3)^2) + a4(exp(-a5(x-a6)^2)';

name='D.W. Donovan - ';

tb=[t1b,'\newline',t2b,'\newline',name,date];

title(tb)

xlabel('x in unitless numbers')

ylabel('f(x) in unitless numbers')

 

plot(x,fx)

 

a00=[5 .25 50 5 0.47 65];

 

a=fminsearch(@nlh4pf,a00);

 

a1=a(1);

a2=a(2);

a3=a(3);

a4=a(4);

a5=a(5);

a6=a(6);

 

ffx1=a1*(exp(-a2*(x-a3).^2));

ffx2=a4*(exp(-a5*(x-a6).^2));

ffx=ffx1+ffx2;

 

ss=(fx-ffx)'*(fx-ffx);

xc=[min(x):(max(x)-min(x))/100:max(x)]';

fxc1=a1*(exp(-a2*(xc-a3).^2));

fxc2=a4*(exp(-a5*(xc-a6).^2));

fxc=fxc1+fxc2;

 

la00=['a00 = ',num2str(a00)];

la1=['a1 = ',num2str(a1)];

la2=['a2 = ',num2str(a2)];

la3=['a3 = ',num2str(a3)];

la4=['a4 = ',num2str(a4)];

la5=['a5 = ',num2str(a5)];

la6=['a6 = ',num2str(a6)];

lss=['sm Sq = ',num2str(ss)];

lssdp=['sm Sq DP = ',num2str(ss/size(fx,1))];

 

figure

hold on;

t1='Non-Linear Curve Fitting Homework Problem #4';

t2='Data Fitted to F(x) = a1(exp(-a2(x-a3)^2)+ a4(exp(-a5(x-a6)^2)';

t=[t1,'\newline',t2,'\newline',name,date];

title(t)

xlabel('x in unitless numbers')

ylabel('f(x) in unitless numbers')

 

plot(x,fx,'r*')

plot(xc,fxc,'b')

plot(min(xc),max(fxc),'w')

plot(min(xc),max(fxc),'w')

plot(min(xc),max(fxc),'w')

plot(min(xc),max(fxc),'w')

plot(min(xc),max(fxc),'w')

plot(min(xc),max(fxc),'w')

plot(min(xc),max(fxc),'w')

plot(min(xc),max(fxc),'w')

plot(min(xc),max(fxc),'w')

 

legend('raw data','fitted curve',la00,la1,la2,la3,la4,la5,la6,lss,lssdp,2);

legend('boxoff')

 

Sol={'parameter' 'Start' 'End';

'a1 = ' a00(1) a1;

'a2 = ' a00(2) a2;

'a3 = ' a00(3) a3;

'a4 = ' a00(4) a4;

'a5 = ' a00(5) a5;

'a6 = ' a00(6) a6;

'sm Sq = ' 'n/a' ss ;

'sm Sq DP = ' 'n/a' (ss/size(fx,1)) };

Sol

%{

Sol =

 

    'parameter'      'Start'     'End'   

    'a1 = '          [     5]    [ 9.9135]

    'a2 = '          [0.2500]    [ 0.2674]

    'a3 = '          [    50]    [51.9718]

    'a4 = '          [     5]    [12.2136]

    'a5 = '          [0.4700]    [ 0.6144]

    'a6 = '          [    65]    [54.9846]

    'sm Sq = '       'n/a'       [16.5612]

    'sm Sq DP = '    'n/a'       [ 0.0207]

%}

 

%Function m-file needed to solve nonlinear Curve fitting homework #4

%version 2007-10-09 D.W. Donovan

function f = fun(a)

 

load('nltwogauss.mat','-ascii');

x=nltwogauss(:,1);

fx=nltwogauss(:,2);

 

a1=a(1);

a2=a(2);

a3=a(3);

a4=a(4);

a5=a(5);

a6=a(6);

 

g1=a1*(exp(-a2*(x-a3).^2));

g2=a4*(exp(-a5*(x-a6).^2));

g=g1+g2;

 

f=(g-fx)'*(g-fx);

 

Back To Dr. Donovan's Courses Page

 

Back To Dr. Donovan's Homepage

 

Physics Dept. Homepage

 

NMU Homepage

 

Please send any comments or questions about this page to ddonovan@nmu.edu

This page last updated on October 16, 2015