Doubt in an equation in Pascal

0

I'm having a problem with a code here. What you are asking is: Given X as a parameter (in degrees), calculate cos (x) with the sum of the first 15 terms in the series below:

With the compiler I've gone step by step, but I can not find fault. I know I should really find this on my own, but I'm already in it for some time and getting lost in thought.

The code I've made so far:

Program eq;

var
x, i, j, k, switch:integer;
coss:real;

function fatorial(n:integer):real;

var
fatN: real;

begin           
    fatN := 1;  
    for i := 1 to n do
    begin   
        fatN := fatN*i;
    end;
    fatorial := fatN; 
end; 

function expoente(x, y:integer):real;

var
range:integer;

begin
    range := y - 1;
    for k := 1 to range do
    begin
        x := x * x
    end;
    expoente := x;
end;

begin   
    readln(x);
    coss := 0;
    for j := 1 to 15 do
    begin
        if j mod 2 = 1 then
            switch := -1
        else
            switch := 1;
        coss := (coss + ((expoente(x, (j*2)))/(fatorial(j*2)))) * switch;
    end;
    coss := 1 - coss;
    writeln(coss:0:8);
end.

Can anyone imagine where I'm skating?

    
asked by anonymous 28.08.2017 / 22:03

1 answer

1

This is the Fourier series for cosine.

For exponent use X raised to Y:

exp(y*ln(x))

in this line:

coss := (coss + ((expoente(x, (j*2)))/(fatorial(j*2)))) * switch;

Replace with:

coss := coss + (exp(j*2*ln(x))/(fatorial(j*2)) * switch;
    
04.10.2017 / 15:48