• Lenguaje

    Pascal

  • Descripción

    Método de Newton-Raphson para encontrar la solución de:
    f(x) = cos(x) - x³

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

function funcion (var x: real): real;
begin
    funcion := cos (x) - x * x * x;
end;

function derivada (var x: real): real;
begin
    derivada := -sin (x) - 3.0 * x * x;
end;

var x, x_1, err : real;
var i : integer;
var tecla : char;

begin
    writeln ('M'#130'todo de Newton-Raphson, C'#160'lculo de la funci'#162'n: cos(x) - x'#252#10#13);
    write ('Ingrese el valor inicial de x0: ');
    readln (x);
    writeln;
    i := 0;
    repeat
        x_1 := x;
        x := x - funcion (x) / derivada (x);
        err := abs ((x - x_1)/x);
        writeln ('x', i, ' = ', x_1:0:6, '        error = ', err:0:6);
        inc (i);
    until (x = x_1) or (i = 100);
    if i=100 then
        writeln (#10#13'La soluci'#162'n no es convergente.')
    else
        writeln (#10#13'La soluci'#162'n es ', x:0:6);
    write (#10#13'Presione una tecla para terminar . . . ');
    tecla := readkey;
end.