%
% Calcolo Numerico 1 -  18 giugno 2014 MILANO ESERCIZIO 3
%
clear all
 
a=input('a=');
b=input('b=');
m=40;
g=inline('exp(x)./(x.^2+1)','x');
g1=inline('exp(x).*(x-1).^2./(x.^2+1).^2','x');
int_cs=cav_simpson(g,a,b,m);
x0=[a:0.01:b];
for k=1:length(x0)
    int_formula(k)=g(x0(k))*(b-a)+g1(x0(k))*((b-x0(k))^2-(a-x0(k))^2)/2;
end
e= int_cs-int_formula;
[eK,K]=min(abs(e));
fprintf('i_cs=%12.9f \n',int_cs);
fprintf('x0=%8.5f  \t I(x_0^{min})=%12.9f \t E(x_0^{min})=%12.6f\n',...
     x0(K),int_formula(K),eK);

 
 
%a=-1 b=2
%i_cs= 3.187990268 
%x0= 1.95000  	 I(x_0^{min})= 3.194244168 	 E(x_0^{min})=    0.006254
 
%a=-2 b=1
%i_cs= 1.876050184 
%x0= 0.16000  	 I(x_0^{min})= 1.873984168 	 E(x_0^{min})=    0.002066