roofnrt1.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. program Roofnrte;
  2. uses
  3. typ,
  4. roo;
  5. type
  6. maxarray = array[1..128] of ArbFloat;
  7. var
  8. n: ArbInt;
  9. a: ArbFloat;
  10. ah2: ArbFloat;
  11. procedure PraktikumEx(var x, fx: ArbFloat; var deff: boolean);
  12. var
  13. xloc: maxarray absolute x;
  14. floc: maxarray absolute fx;
  15. i: ArbInt;
  16. begin
  17. floc[1] := 2 * (xloc[1] - xloc[2]) - ah2 * exp(xloc[1]);
  18. for i := 2 to n - 1 do
  19. floc[i] := -xloc[i - 1] + 2 * xloc[i] - xloc[i + 1] - ah2 * exp(xloc[i]);
  20. floc[n] := -xloc[n - 1] + 2 * xloc[n] - ah2 * exp(xloc[n]);
  21. end;
  22. const
  23. m = 9;
  24. procedure NagExample(var x, fx: ArbFloat; var deff: boolean);
  25. var
  26. xloc: array[1..m] of ArbFloat absolute x;
  27. floc: array[1..m] of ArbFloat absolute fx;
  28. k: ArbInt;
  29. begin
  30. floc[1] := 1 + (3 - 2 * xloc[1]) * xloc[1] - 2 * xloc[2];
  31. for k := 2 to m - 1 do
  32. floc[k] := 1 + (3 - 2 * xloc[k]) * xloc[k] - xloc[k - 1] - 2 * xloc[k + 1];
  33. floc[m] := 1 + (3 - 2 * xloc[m]) * xloc[m] - xloc[m - 1];
  34. end;
  35. procedure MatlabEx(var x, fx: ArbFloat; var deff: boolean);
  36. var
  37. xloc: array[1..3] of ArbFloat absolute x;
  38. floc: array[1..3] of ArbFloat absolute fx;
  39. begin
  40. floc[1] := sin(xloc[1]) + sqr(xloc[2]) + ln(xloc[3]) - 7;
  41. floc[2] := 3 * xloc[1] + exp(xloc[2] * ln(2)) - xloc[3] * sqr(xloc[3]) + 1;
  42. floc[3] := xloc[1] + xloc[2] + xloc[3] - 5;
  43. end;
  44. procedure TPNumlibEx(var x, fx: ArbFloat; var deff: boolean);
  45. begin
  46. fx := cos(x);
  47. end;
  48. procedure JdeJongEx(var x, fx: ArbFloat; var deff: boolean);
  49. begin
  50. if (x >= 0) and (x <= 1) then
  51. fx := x - 2
  52. else
  53. deff := False;
  54. end;
  55. procedure Uitvoer(var x1: ArbFloat; n, step: ArbInt);
  56. var
  57. i: ArbInt;
  58. xloc: maxarray absolute x1;
  59. begin
  60. i := 1;
  61. while (i <= n) do
  62. begin
  63. writeln(i: 5, ' ', xloc[i]: 20);
  64. Inc(i, step);
  65. end;
  66. writeln;
  67. end;
  68. var
  69. x: ^maxarray;
  70. t, residu: ArbFloat;
  71. i, term: ArbInt;
  72. begin
  73. { praktikum sommetje }
  74. n := 8;
  75. a := 0.50;
  76. repeat
  77. ah2 := a / sqr(n);
  78. GetMem(x, n * SizeOf(ArbFloat));
  79. for i := 1 to n do
  80. x^[i] := 0;
  81. writeln('Voorbeeld programma ''praktikum'', resultaten voor n= ', n: 2);
  82. writeln;
  83. roofnr(@PraktikumEx, n, x^[1], residu, 1e-4, term);
  84. if term = 1 then
  85. writeln(' Norm van de residuen', residu: 20, #13#10,
  86. ' Berekende oplossing')
  87. else
  88. writeln(' Proces afgebroken term = ', term, #13#10,
  89. ' Laatst berekende waarden');
  90. writeln;
  91. Uitvoer(x^[1], n, n div 8);
  92. FreeMem(x, n * SizeOf(ArbFloat));
  93. n := n * 2
  94. until n = 128;
  95. { Nag procedure bibliotheek voorbeeld }
  96. GetMem(x, m * SizeOf(ArbFloat));
  97. for i := 1 to m do
  98. x^[i] := -1;
  99. writeln('Voorbeeld programma ''NAG-bibliotheek'' met m= ', m: 2);
  100. writeln;
  101. roofnr(@NagExample, m, x^[1], residu, 1e-6, term);
  102. if term = 1 then
  103. writeln(' Norm van de residuen', residu: 20, #13#10,
  104. ' Berekende oplossing')
  105. else
  106. writeln(' Proces afgebroken term = ', term, #13#10,
  107. ' Laatst berekende waarden');
  108. writeln;
  109. Uitvoer(x^[1], m, 1);
  110. FreeMem(x, m * SizeOf(ArbFloat));
  111. { Matlab voorbeeld uit handleiding }
  112. n := 3;
  113. GetMem(x, n * SizeOf(ArbFloat));
  114. for i := 1 to n do
  115. x^[i] := 1;
  116. writeln('Voorbeeld programma ''MATLAB handleiding'', resultaten voor n= ', n: 2);
  117. writeln;
  118. roofnr(@MatlabEx, n, x^[1], residu, 1e-6, term);
  119. if term = 1 then
  120. writeln(' Norm van de residuen', residu: 20, #13#10,
  121. ' Berekende oplossing')
  122. else
  123. writeln(' Proces afgebroken term = ', term, #13#10,
  124. ' Laatst berekende waarden');
  125. writeln;
  126. Uitvoer(x^[1], n, 1);
  127. FreeMem(x, n * SizeOf(ArbFloat));
  128. { 1-dimensionaal voorbeeld uit TPNumlib }
  129. writeln('Voorbeeld programma ''TPNumlib'' voor ‚‚n dimensie');
  130. writeln;
  131. t := 1;
  132. roofnr(@TPNumlibEx, 1, t, residu, 1e-6, term);
  133. if term = 1 then
  134. writeln(' Norm van de residuen', residu: 20, #13#10,
  135. ' Berekende oplossing')
  136. else
  137. writeln(' Proces afgebroken term = ', term, #13#10,
  138. ' Laatst berekende waarden');
  139. writeln;
  140. Writeln(' ', t: 20);
  141. { Matlab voorbeeld uit handleiding }
  142. { dit moet fout gaan }
  143. n := 3;
  144. GetMem(x, n * SizeOf(ArbFloat));
  145. for i := 1 to n do
  146. x^[i] := 1;
  147. writeln;
  148. writeln('Voorbeeld programma ''MATLAB handleiding'', resultaten voor n= ', n: 2);
  149. writeln('Gaat niet goed want de relatieve fout is gelijk aan 0 gekozen');
  150. writeln;
  151. roofnr(@MatlabEx, n, x^[1], residu, 0, term);
  152. if term = 1 then
  153. writeln(' Norm van de residuen', residu: 20, #13#10,
  154. ' Berekende oplossing')
  155. else
  156. writeln(' Proces afgebroken term = ', term, #13#10,
  157. ' Laatst berekende waarden');
  158. writeln;
  159. Uitvoer(x^[1], n, 1);
  160. writeln;
  161. writeln('Voorbeeld programma ''MATLAB handleiding'', resultaten voor n= ', n: 2);
  162. writeln;
  163. for i := 1 to n do
  164. x^[i] := 1;
  165. roofnr(@MatlabEx, n, x^[1], residu, 1e-8, term);
  166. if term = 1 then
  167. writeln(' Norm van de residuen', residu: 20, #13#10,
  168. ' Berekende oplossing')
  169. else
  170. writeln(' Proces afgebroken term = ', term, #13#10,
  171. ' Laatst berekende waarden');
  172. writeln;
  173. Uitvoer(x^[1], n, 1);
  174. FreeMem(x, n * SizeOf(ArbFloat));
  175. { 1-dimensionaal voorbeeld voor deff }
  176. writeln('Voorbeeld programma in ‚‚n dimensie, voor domein [0..1]');
  177. writeln;
  178. t := 0.5;
  179. roofnr(@JdeJongEx, 1, t, residu, 1e-6, term);
  180. if term = 1 then
  181. writeln(' Norm van de residuen', residu: 20, #13#10,
  182. ' Berekende oplossing')
  183. else
  184. writeln(' Proces afgebroken term = ', term, #13#10,
  185. ' Laatst berekende waarden');
  186. writeln;
  187. Writeln(' ', t: 20);
  188. end.