home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / NUMPAS.ZIP / NUMINT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-06-25  |  1.4 KB  |  61 lines

  1. program numint(input,output);
  2. (* Program evaluates the definite integral of a function with upper  and *)
  3. (*  lower limits, t2 and t1, by the method of trapezoidal integration. *)
  4.  
  5. var e,f,t1,t2: real;
  6.     n: integer;
  7.  
  8. function fcn(y: real): real;
  9.   begin
  10.     fcn := y*y+y+1.0     (* Define function to be integrated *)
  11.   end;
  12.  
  13.  
  14. procedure integral(var f2: real;e0,z1,z2: real; var n1:integer);
  15. (* Calculates definite integral of fcn using trapezoidal integration *)
  16. (* for upper and lower limits z1 and z2 *)
  17.  
  18.   var k1,i: integer;
  19.       f1,f3,y1,y2,e1,h: real;
  20.  
  21.   begin
  22.     h := (z2-z1)/n1;
  23.     f3 := f2;
  24.     f2 := 0.0;
  25.     k1 := 1;
  26.     for i := 0 to n1 do
  27.       begin
  28.         f1 := 0.0;
  29.         y1 := z1+h*(i-1);
  30.         y2 := z1+h*i;
  31.         f1 := h*(fcn(y1)+fcn(y2))/2;
  32.         f2 := f2+f1;
  33.         e1 := (f2-f3)/f2;
  34.       end;
  35.    writeln(k1:5,f2:15:5,'  ',e1:10:5);
  36.    if abs(e1) > e0 then
  37.       begin
  38.          n1 := 2*n1;
  39.          k1 := k1+1;
  40.          integral(f,e,z1,z2,n1);
  41.       end
  42.    else writeln('Done');
  43. end;
  44.  
  45.  
  46. begin (** main **)
  47.   write('Initial and final times:');
  48.   readln(t1,t2);
  49.   writeln;
  50.   write('Input number of increments:');
  51.   readln(n);
  52.   writeln;
  53.   write('Input error criterion: ');
  54.   readln(e);
  55.   writeln;
  56.   writeln('Run':5,'integral':15,'Error': 15);
  57.   writeln;
  58.   f := 0.0;
  59.   integral(f,e,t1,t2,n);
  60. end.
  61.