function [ka,k,Vd_F]=get_params()

[t_data,Cp_data]=get_data();
% Dose is given.
D=100;

% Create an anonymous function handle to the MATLAB file.
f = @(x)mycost(x);

% Initial guess - ka,k,Vd_F
x0 = [2,0.1,20];

lb=[0,0, 0];
ub=[10,10,200];

options = optimoptions('fmincon','Display','iter');
[x, fval] = fmincon(f,x0,[],[],[],[],lb,ub,[],options);

ka=x(1);
k=x(2);
Vd_F=x(3);

t=0:0.01:t_data(end);
Cp_model=(ka/(ka-k))*(D/Vd_F)*(exp(-k*t)-exp(-ka*t));
plot(t,Cp_model,t_data,Cp_data,'o')
drawnow

disp('************************************************')
display(strcat('cost function=',num2str(fval)));
disp('optimum parameters=')
display(strcat('ka=',num2str(ka)));
display(strcat('k=',num2str(k)));
display(strcat('Vd_F=',num2str(Vd_F)));
disp('************************************************')

function y=mycost(x)
    ka_loc=x(1);
    k_loc=x(2);
    Vd_F_loc=x(3);
    
    Cp_model_loc=(ka_loc/(ka_loc-k_loc))*(D/Vd_F_loc)*(exp(-k_loc*t_data)-exp(-ka_loc*t_data));

    corr=0.1;
    y=sum( (log(Cp_model_loc+corr)-log(Cp_data+corr)).^2);
    % y=sum( Cp_model_loc-Cp_data).^2;
end

end


function [t_data,Cp_data]=get_data()

M=[             0, 0
    			0.6 , 2.74 
			0.8 , 3.13  
			1 , 3.37
			1.4 , 3.55  
			1.8 , 3.5  
			2 , 3.43
			2.6 , 3.12  
			3 , 2.89
			4 , 2.33  
			7 , 1.17  
			12 , 0.37 
            ];

t_data= M(:,1);
Cp_data= M(:,2);
        
end

