%-----------------%
% Fitting of data %
%-----------------%
%% Example
clc
clear all
close all

T_max=[9.7,12.0,15.7,17.5,21.4,26.9,...
    31.2,30.7,26.0,19.0,13.4,10.1];
T_min=[2.6,3.7,5.6,7.2,10.7,15.1...
    ,18.4,18.2,15.0,10.2,6.0,3.8];

i=[1:1:12];

figure(1)
hold on
plot(i,T_max,'ro-','MarkerSize',10)
plot(i,T_min,'bx--','MarkerSize',10)
legend('T_{max}','T_{min}', ...
    'Location','NorthWest')
ylabel('temperature [{}^{o}C]','fontsize',16),...
    xlabel('month',...
    'fontsize',16)
hold off
%print -f -depsc 'temperature_fitt.eps'

%% Ex. 1.2.2 and Fig. 1
clc
clear all
close all

T_max=[9.7,12.0,15.7,17.5,21.4,26.9,...
    31.2,30.7,26.0,19.0,13.4,10.1];
T_min=[2.6,3.7,5.6,7.2,10.7,15.1...
    ,18.4,18.2,15.0,10.2,6.0,3.8];

% number of data
N=length(T_max);
%N=length(T_min)
x=(1:1:N);

% number of parameters we are going to fit the data with
Np=4;

for i=1:N
    for j=1:Np
        A(i,j)=( x(i) )^(j-1);
    end
end

%format long
A

% A is a NxNp=12x4 matrix, as it can be checked with
size(A)

% (A')*A is a 4x4 matrix, so we can evaluate the inverse;
% the set of best (linear least-square fitting) parameters is:
a_max=inv((A')*A)*(A')*(T_max')
a_min=inv((A')*A)*(A')*(T_min')

% we define the continuous variable
xc=linspace(1,12,100);
f_max=a_max(1)+a_max(2)*xc+a_max(3)*xc.^2+a_max(4)*xc.^3;
f_min=a_min(1)+a_min(2)*xc+a_min(3)*xc.^2+a_min(4)*xc.^3;

figure(1)
hold on
plot(x,T_max,'ro--','MarkerSize',10)
plot(xc,f_max,'r-','LineWidth',2)
plot(x,T_min,'bx-.','MarkerSize',10)
plot(xc,f_min,'b-')
legend('T_{max}','best fit T_{max}',...
    'T_{min}','best fit T_{min}', ...
    'Location','NorthWest')
ylabel('temperature [{}^{o}C]','fontsize',16),...
    xlabel('month',...
    'fontsize',16)
hold off
print -f -depsc 'temperature_fitt.eps'

%% Fig.2

fmax2=a_max(1)+a_max(2)*x+a_max(3)*x.^2+a_max(4)*x.^3;


figure(2)
hold on
plot(x,T_max,'ro--','MarkerSize',10)
plot(xc,f_max,'r-','LineWidth',2)
for i=1:N
    yv=linspace(T_max(i),fmax2(i),20);
    xv=ones(1,20)*x(i);
    plot(xv,yv,'g-','LineWidth',2)
end
legend('T_{max}=y_i=y(x_i)','f(x,a_1,...,a_{N_p}) = best fit T_{max}',...
    'y_i-f(x_i,a1,...,a_{N_p})', ...
    'Location','NorthWest')
ylabel('temperature [{}^{o}C]','fontsize',16),...
    xlabel('month',...
    'fontsize',16)
hold off
print -f -depsc 'distances.eps'
%% Ex. 1.2.3
clc
clear all


T_max=[9.7,12.0,15.7,17.5,21.4,26.9,...
    31.2,30.7,26.0,19.0,13.4,10.1];
T_min=[2.6,3.7,5.6,7.2,10.7,15.1...
    ,18.4,18.2,15.0,10.2,6.0,3.8];

% number of data
N=length(T_max);
%N=length(T_min)
x=(1:1:N);

% number of parameters we are going to fit the data with
Np=5;

for i=1:N
    for j=1:Np
        A(i,j)=( x(i) )^(j-1);
    end
end

A
size(A)

a_max=inv((A')*A)*(A')*(T_max')
a_min=inv((A')*A)*(A')*(T_min')

% we define the continuous variable
xc=linspace(1,12,100);
f_max=a_max(1)+a_max(2)*xc+a_max(3)*xc.^2+a_max(4)*xc.^3+a_max(5)*xc.^4;
f_min=a_min(1)+a_min(2)*xc+a_min(3)*xc.^2+a_min(4)*xc.^3+a_min(5)*xc.^4;

figure(3)
hold on
plot(x,T_max,'ro--','MarkerSize',10)
plot(xc,f_max,'r-','LineWidth',2)
plot(x,T_min,'bx-.','MarkerSize',10)
plot(xc,f_min,'b-')
legend('T_{max}','best fit T_{max}',...
    'T_{min}','best fit T_{min}', ...
    'Location','NorthWest')
ylabel('temperature [{}^{o}C]','fontsize',16),...
    xlabel('month',...
    'fontsize',16)
hold off
print -f -depsc 'temperature_fitt_2.eps'
%% Ex. 1.2.3 -- try with sin()
clc
clear all
close all

T_max=[9.7,12.0,15.7,17.5,21.4,26.9,...
    31.2,30.7,26.0,19.0,13.4,10.1];
N=length(T_max);
x=(1:1:N);

num=100
a1=linspace(0,40,num);
a2=linspace(0,1.5*pi,num);
a3=linspace(0,2*pi,num);

% evaluate the squared distances between the data and
F=zeros(num,num,num);

for i1=1:num
    for i2=1:num
        for i3=1:num            
            for i=1:N
                F(i1,i2,i3)=F(i1,i2,i3) + ( T_max(i)-...
                    a1(i1)*sin( a2(i2)*x(i)+a3(i3) ) )*...
                    ( T_max(i)-...
                    a1(i1)*sin( a2(i2)*x(i)+a3(i3) ) );
            end
        end 
    end
end

% the smallest value of F and its position
min_F=min(min(min(F)))
for i1=1:num
    for i2=1:num
        for i3=1:num
           if  F(i1,i2,i3)==min_F
               pos=[i1, i2, i3];
           end
        end
    end
end
% position
pos
               
%check
F(pos(1),pos(2),pos(3))

a1_p=a1(pos(1))
a2_p=a2(pos(2))
a3_p=a3(pos(3))

%plot
xc=linspace(1,12,100);
f=a1_p*( sin(a2_p*xc+a3_p) );

figure(4)
hold on
plot(x,T_max,'ro--','MarkerSize',10)
plot(xc,f,'r-','LineWidth',2)
legend('T_{max}','best fit of T_{max} with sin',...
    'Location','NorthWest')
ylabel('temperature [{}^{o}C]','fontsize',16),...
    xlabel('month',...
    'fontsize',16)
hold off
print -f -depsc 'temperature_fitt_sin.eps'

%%



