|
|
||||||||||||||||||
|
2. Non-Linear One Gaussian Function |
||||||||||||||||||
|
|
||||||||||||||||||
|
Use the
file “nlonegauss.mat”, it contains data that should be fitted to a function
of the form: |
||||||||||||||||||
|
|
||||||||||||||||||
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
%Program
to solve non-linear Curve homework problem #2 %data
for a function F=a1(e^(-a2(x-a3)^2) %version
2015-09-30 D.W. Donovan clear all; load('nlonegauss.mat','-ascii'); x=nlonegauss(:,1); fx=nlonegauss(:,2); figure hold on; t1b='Non-Linear
Curve Fitting Homework Problem #2'; t2b='Raw Data to
be Fitted to F(x) = a1(exp(-a2(x-a3)^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= [75
0.40 -5] a=fminsearch(@nlh2pf,a00) a1=a(1); a2=a(2); a3=a(3); ffx=a1*(exp(-a2*(x-a3).^2)); ss=(fx-ffx)'*(fx-ffx); xc=[min(x):(max(x)-min(x))/100:max(x)]'; fxc=a1*(exp(-a2*(xc-a3).^2)); la00=['a00 = ',num2str(a00)]; la1=['a1 = ',num2str(a1)]; la2=['a2 = ',num2str(a2)]; la3=['a3 = ',num2str(a3)]; lss=['sm Sq = ',num2str(ss)]; lssdp=['sm Sq DP = ',num2str(ss/size(fx,1))]; figure hold on; t1='Non-Linear
Curve Fitting Homework Problem #2'; t2='Data Fitted
to F(x) = a1(exp(-a2(x-a3)^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') legend('raw data','fitted curve',la00,la1,la2,la3,lss,lssdp,2); legend('boxoff') Sol={'parameter' 'Start' 'End'; 'a1 = ' a00(1) a1; 'a2 = ' a00(2) a2; 'a3 = ' a00(3) a3; 'sm Sq = ' 'n/a' ss ; 'sm Sq DP = ' 'n/a' (ss/size(fx,1)) }; Sol %{ Sol = 'parameter' 'Start' 'End' 'a1 = ' [
75] [ 101.3588] 'a2 = ' [0.4000] [
1.2487] 'a3 = ' [
-5] [ -2.4015] 'sm Sq = ' 'n/a'
[1.6817e+03] 'sm Sq DP = ' 'n/a' [ 8.3666] %} |
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
%Function
m-file needed to solve nonlinear Curve fitting homework #2 %version
2007-10-08 D.W. Donovan function f = fun(a) load('nlonegauss.mat','-ascii'); x=nlonegauss(:,1); fx=nlonegauss(:,2); a1=a(1); a2=a(2); a3=a(3); g=a1*(exp(-a2*(x-a3).^2)); f=(g-fx)'*(g-fx); |
||||||||||||||||||
|
||||||||||||||||||
|
|