• Lenguaje

    Pascal

  • Descripción

    Encuentra la solución de f(x) = f(x)=x³+2x²+10x-20 con el método de la secante

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
program secante;
uses crt;

const ITERACIONES_MAXIMAS = 100;
const TOLERANCIA = 0.0;

function f (x : real) : real;
begin
    f := x*x*x + 2.0*x*x + 10.0*x - 20;
end;

var x0, x1, temp, err : real;
var n : integer;
var tecla : char;
begin
    n := 1;
    writeln ('M'#130'todo de la Secante para el c'#160'lculo de la funci'#162'n: f(x)=x'#252'+2x'#253'+10x-20');
    write   (#10#13'Ingrese la aproximaci'#162'n inicial x0: ');
    readln  (x0);
    write   ('Ingrese la aproximaci'#162'n inicial x1: ');
    readln  (x1);
    writeln (#10#13, 'n                   Xn                  Error');
    writeln ('0                   ', x0:0:6);
    repeat
        err := abs(x0-x1);
        writeln (n, '                   ', x1:0:6, '            ', err:0:6);
        if err <> 0 then
            begin
                temp := x1;
                x1 := x1 - (x1 - x0) * f(x1) / (f(x1) - f(x0));
                x0 := temp;
            end;
        inc (n);
    until (err<=TOLERANCIA) or (n>ITERACIONES_MAXIMAS);
    writeln;
    if n<ITERACIONES_MAXIMAS then
        writeln ('La soluci'#162'n es: ', x1)
    else
        writeln ('No se encontr'#162' la ra'#161'z: cambiar aproximaciones iniciales o aumentar ITERACIONES_MAXIMAS');
    write (#10#13'Presione una tecla para continuar . . . ');
    tecla := readkey;
end.