program exer05_02;

// A função cuja integral vai ser calculada e o gráfico feito

function f(x:real):real;
begin 
  f :=  1 + x*x;
end;  

// funão não usada
 
function G(x: real):real; 
begin
  G :=  x + (1/3.0)*x*x*x;
end;

// derivada de f - não usada

function df(x:real):real;
begin
  df := 2*x;
end;

// escreve a matriz (x,f(x)) no arquivo dados

Procedure listagem(inicio, fim, delta : real); 
Var 
  teste : integer;   
  dados : Text; 
  x 	: real;
begin
    teste := 0;   
    x := inicio;
    Assign(dados, "dados");
    rewrite(dados);
    while( x < fim) do
    begin	 
	writeln(dados, x, "  ",  f(x));
   	x := x + delta;
    end; 
    close(dados);
end;

// Escreve o arquivo de comandos do gnuplot no arquivo transfere

procedure transfere;
Var 
 transfere :  Text;
begin
 Assign(transfere, "transfere");
 Rewrite(transfere);
 //writeln(transfere, "set terminal postscript portrait enhanced ");
 //writeln(transfere, "set output 'exer05_02.eps'");
 writeln(transfere, "set pointsize 0.1 ");  
 writeln(transfere, "plot 'dados' with lines  ");
 writeln(transfere, "pause -2");
 Close(transfere);
end;

//Calcula a integral de uma função - soma de Riemann

function riemann(inicio, fim, delta: real):real;
 Var soma, x : real;
begin
 soma := 0;   
 x := inicio;
 while( x < fim) do
 begin	 
	soma 	:= soma + f(x);
   	x 	:= x + delta;
 end; 
   riemann := soma*delta;
end;


// Início do script -  o programa

Begin
  Var	delta, precisao : real;
  delta := 0.01; precisao := 0.1; 
  listagem(-3,3,0.01);
  transfere;
  Writeln('O valor aproximado da integral é ', riemann(-1,1,0.001));
  Writeln('Para ver um gráfico, chame');
  writeln('    gnuplot transfere   ');
  //call('gnuplot transfere'); // usar o comando certo 
  
end.