1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| {$APPTYPE GUI}
{$MODE DELPHI}
program NewtonG;
{ Approximation de la racine carrée de N par la méthode de Newton ou des
tangentes. }
uses
Couleurs, Graph, SysUtils, WinCrt;
procedure SetLongWordPalette(const index: byte; const color: longWord);
{ Procédure pour utiliser les noms de couleurs de l'unité Couleurs. }
begin
SetRGBPalette(
index,
(color and $FF0000) shr 16,
(color and $FF00) shr 8,
color and $FF
);
end;
procedure ClearScreen(const color: longWord);
begin
SetLongWordPalette(0, color);
SetFillStyle(SolidFill, 0);
Bar(0, 0, GetMaxX, GetMaxY);
end;
function OpenGraph(const color: longWord): boolean;
const
title = 'Newton';
var
pilote, mode: smallInt;
begin
pilote := d8bit;
mode := m640x480;
StrCopy(windowtitle, title);
InitGraph(pilote, mode, '');
Result := GraphResult = grOk;
if Result then
ClearScreen(color);
end;
const
N = 2;
type
tFonction = function(const x: extended): extended;
function F(const x: extended): extended;
begin
F := Sqr(x) - N;
end;
procedure TraceRepere(aCouleur: longWord);
begin
SetLongWordPalette(0, aCouleur);
SetColor(0);
SetLineStyle(SolidLn, 0, NormWidth);
Line( 0, 360, 640, 360);
SetLineStyle(DottedLn, 0, NormWidth);
Line(160, 0, 160, 480);
Line(480, 0, 480, 480);
Line( 0, 120, 640, 120);
end;
procedure TraceCourbe(aFonction: tFonction; aCouleur: longWord);
const
minX = 0.5;
maxX = 2.5;
var
x, y: extended;
xEcran, yEcran: integer;
begin
SetLongWordPalette(0, aCouleur);
x := minX;
repeat
y := aFonction(x);
xEcran := Round(+320 * x - 160);
yEcran := Round(-240 * y + 360);
if (yEcran >= 0) and (yEcran < 480) then
PutPixel(xEcran, yEcran, 0);
x := x + 5E-3;
until x >= maxX;
end;
function Racine(const x: extended; aCouleur: longWord): extended;
const
minY = -0.5;
maxY = +1.5;
var
y, a, b: extended;
x1, x2: integer;
begin
y := F(x);
a := 2 * x;
b := y - a * x;
x1 := Round(320 * ((b-maxY)/-a) - 160);
x2 := Round(320 * ((b-minY)/-a) - 160);
SetLongWordPalette(0, aCouleur);
SetColor(0);
SetLineStyle(SolidLn, 0, NormWidth);
Line(x1, 0, x2, 480);
Result := b / -a;
end;
const
Epsilon = 1E-16;
var
x: extended;
t: text;
begin
if OpenGraph(DarkSlateBlue) then
begin
TraceRepere(DarkIndigo);
TraceCourbe(F, Snow);
x := N;
Assign(t, 'sortie.txt');
rewrite(t);
repeat
Write(t, x:20, ' -> ');
Delay(1000);
x := Racine(x, SlateBlue);
TraceCourbe(F, Snow);
WriteLn(t, x);
until F(x) < Epsilon;
Close(t);
SetLongWordPalette(0, Snow);
SetColor(0);
OutTextXY(10, 10, 'Appuyez sur une touche !');
ReadKey;
end;
end. |
Partager