Actually the main code of the program is like this:
program main
use global_variable ! Define the global parameters that will be used in this project
use functions ! Define objective functions
use auxfuncs ! basis functions to approximate our goal function
use powell_optim
implicit none
real(8) :: interval, lambda(Ibis), func_value(Ibis)
integer :: j, counter2
spot_plot = linspace(Blower,Bupper,fPlot)
spot_plot2 = linspace(Blower,Bupper,JJ)
x_grid = linspace(Blower,Bupper,Ibis)
lambda =0
j = 1
interval = Bupper - Blower
h = interval / JJ
! Chebyshev are defines over [-1,1], we convert [0,5] to [-1,1] first
x_newgrid = 2*(x_grid-Blower) / interval -1
spot_plotnew = 2*(spot_plot-Blower) / interval -1
!choose which method to use to approximating
!first calculate the value of Ibis points for object
do j=1,Ibis
func_value(j) = func(x_grid(j))
end do
!******************************************!
! MSE method
guess_results = 0
xmatrix = 0
do j=1,Ibis
xmatrix(j,j)=1
end do
call powell(guess_results,xmatrix,ftoll,iterr,frett)
do j=1,fPlot
func_approxi_plot2(j) = sum(guess_results*func_basis(spot_plot(j)))
end do
!%%%%%%%%%%%%%%%%%%%%%%!
if (types2 ==1) then
! calculate the weighting vector for the basis
do j=1,Ibis
func_approxi(j,:)=func_basis(x_grid(j))
end do
call inverse(func_approxi,invi,Ibis)
do j=1,Ibis
lambda(j) = sum(func_value*invi(j,:))
end do
!calculate the data for plotting
do j=1,fPlot
func_value_plot(j) = func(spot_plot(j))
func_approxi_plot(j) = sum(lambda*func_basis(spot_plot(j)))
end do
!%%%%%%%%%%%%%%%%%%%%%%%!
else if (types2 == 2) then
do j=1,Ibis
func_approxi(j,:)=func_basis(x_newgrid(j))
end do
call inverse(func_approxi,invi,Ibis)
do j=1,Ibis
lambda(j) = sum(func_value*invi(j,:))
end do
!%%%%%%%%%%%%%%%%%%%%%%%!
else
lambda = func_value
do j=1,fPlot
func_value_plot(j) = func(spot_plot(j))
func_approxi_plot(j) = sum(lambda*func_basis(spot_plot(j)))
end do
end if
!%%%%%% save data %%%%%%%%!
open(unit=9,file='lambda_MSE.txt')
write(9, '(1G14.6)') guess_results
close(9)
open(unit=9,file='lambda.txt')
write(9, '(1G14.6)') lambda
close(9)
open(unit=9,file='functionvalue.txt')
write(9, '(1G14.6)') func_value_plot
close(9)
open(unit=9,file='axis.txt')
write(9,'(1G14.6)') spot_plot
close(9)
open(unit=9,file='function_approximation_value.txt')
write(9,'(1G14.6)') func_approxi_plot
close(9)
open(unit=9,file='function_approximation_value_MSE.txt')
write(9,'(1G14.6)') func_approxi_plot2
close(9)
! if (flag_normtest == 1) then
! open(unit=14,file='normality_tests_expansion')
! do i = 1, fPlot
! write(10,100) normaltest_normal(i,:)
! write(11,100) normaltest_t(i,:)
! write(12,100) normaltest_kotz(i,:)
! write(13,100) normaltest_mixture(i,:)
! write(14,100) normaltest_expansion(i,:)
! end do
! close(14)
! end if
end program main