! ch13_nonlinear_fit.f95 ! Companion code for Chapter 13: Optimization and Data Fitting. ! ! Fit y(t) = A*exp(-t/tau) by Nelder-Mead simplex minimization. ! Optimize A and log(tau) to keep tau positive; do not take logarithms of y. ! Independent y errors and negligible time errors are assumed. ! Edit data, uncertainties, and starting estimates in the driver. ! Reports point estimates, not parameter uncertainties. Console output only. ! Try several starting estimates and assess physical model adequacy. ! ! Compile (Fortran 95): ! gfortran -std=f95 -Wall -Wextra -O2 ch13_nonlinear_fit.f95 -o ch13_nonlinear_fit ! Run: ./ch13_nonlinear_fit (Windows: ch13_nonlinear_fit.exe) module nonlinear_fit_methods implicit none private integer, parameter :: dp=selected_real_kind(15,307) public :: dp, nelder_mead, initialize_random, normal_random contains subroutine nelder_mead(f,p,tol_x,tol_f,max_iter,max_eval,value,iterations,evaluations,status) interface function f(p) result(v) integer, parameter :: dp=selected_real_kind(15,307) real(dp), intent(in) :: p(:) real(dp) :: v end function f end interface real(dp), intent(inout) :: p(:) real(dp), intent(in) :: tol_x,tol_f integer, intent(in) :: max_iter,max_eval real(dp), intent(out) :: value integer, intent(out) :: iterations,evaluations character(len=*), intent(out) :: status real(dp) :: simplex(size(p),size(p)+1),v(size(p)+1) real(dp) :: center(size(p)),xr(size(p)),xe(size(p)),xc(size(p)),temp(size(p)) real(dp) :: fr,fe,fc,tv integer :: n,i,j logical :: shrink n=size(p) if (n<1 .or. max_iter<0 .or. max_eval=1) if (v(j)<=tv) exit simplex(:,j+1)=simplex(:,j) v(j+1)=v(j) j=j-1 end do simplex(:,j+1)=temp v(j+1)=tv end do if (v(1)>=huge(1.0_dp)) then status='Stopped: all simplex vertices have invalid objective values.' exit end if if (maxval(abs(simplex(:,2:n+1)-spread(simplex(:,1),2,n)))<=tol_x & .and. maxval(abs(v(2:n+1)-v(1)))<=tol_f) then status='Simplex parameter and objective tolerances reached.' exit end if if (iterations>=max_iter) exit ! Reserve enough calls for reflection, contraction, and a full shrink. if (evaluations>max_eval-(n+2)) then status='Function-evaluation budget reached.' exit end if center=sum(simplex(:,1:n),dim=2)/real(n,dp) xr=center+(center-simplex(:,n+1)) fr=f(xr) evaluations=evaluations+1 shrink=.false. if (fr0.0_dp) exit end do call random_number(u2) z=sqrt(-2.0_dp*log(u1))*cos(2.0_dp*acos(-1.0_dp)*u2) end function normal_random end module nonlinear_fit_methods module exponential_data use nonlinear_fit_methods, only: dp implicit none integer, parameter :: n=30 real(dp) :: t(n),y(n),scale(n) contains function cost(p) result(value) real(dp), intent(in) :: p(:) real(dp) :: value,tau,residuals(n) value=huge(1.0_dp) if (.not. all(abs(p)<=huge(1.0_dp))) return if (p(2)>=log(huge(1.0_dp)) .or. p(2)<=log(tiny(1.0_dp))) return tau=exp(p(2)) residuals=(y-p(1)*exp(-t/tau))/scale ! Reject unusable trial values before summing their squares. if (.not. all(abs(residuals)=huge(1.0_dp)) stop 'No usable fit found.' A=p(1) tau=exp(p(2)) residuals=y-A*exp(-t/tau) sse=sum(residuals**2) write(*,'(/,a)') 'Nonlinear exponential fit: y = A*exp(-t/tau)' write(*,'(a,es23.15)') 'Amplitude A: ',A write(*,'(a,es23.15)') 'Decay time tau: ',tau write(*,'(a,es14.6)') 'Residual sum of squares: ',sse write(*,'(a,es14.6)') 'Residual standard deviation: ',sqrt(sse/real(n-2,dp)) write(*,'(a,i0)') 'Degrees of freedom: ',n-2 if (known_uncertainties) then write(*,'(a,es14.6)') 'Chi-squared: ',value write(*,'(a,es14.6)') 'Reduced chi-squared: ',value/real(n-2,dp) end if write(*,'(a,i0)') 'Iterations: ',iterations write(*,'(a,i0)') 'Objective calls: ',evaluations write(*,'(a,a)') 'Status: ',trim(status) end program ch13_nonlinear_fit