Skip to content

Instantly share code, notes, and snippets.

@paulojp-dev
Last active December 29, 2016 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulojp-dev/2a490d26cabf7b20dc9d9f8c8ff04683 to your computer and use it in GitHub Desktop.
Save paulojp-dev/2a490d26cabf7b20dc9d9f8c8ff04683 to your computer and use it in GitHub Desktop.
Scripts em pascal. Capaz de calcular: Correlação e Regressão Linear; Coeficiente de Pearson; Equação de 2ª grau.
Program CorrelacaoRegressao;
USES crt;
Const
max = 12;
Type
vetor = Array[1..max] OF Real;
classes = record
xi : vetor;
yi : vetor;
xiyi : vetor;
xi2 : vetor;
yi2 : vetor;
s_xi : Real;
s_yi : Real;
s_xiyi : Real;
s_xi2 : Real;
s_yi2 : Real;
end;
Var
c : classes;
n, i : Byte;
et, at, bt, ct, dt, r : Real;
FUNCTION recebe_valores(n : Byte):vetor;
var
x : vetor;
begin
FOR i := 1 TO n DO
begin
writeln('Digite o ', i, 'ª valor: ');
read(x[i]);
end;
recebe_valores := x;
end;
FUNCTION calc_xiyi(xi : vetor; yi : vetor; n : Byte) : vetor;
var
x : vetor;
begin
FOR i := 1 TO n DO
x[i] := xi[i] * yi[i];
calc_xiyi := x;
end;
FUNCTION calc_xi2(xi : vetor; n : Byte) : vetor;
var
x : vetor;
begin
FOR i := 1 TO n DO
x[i] := xi[i] * xi[i];
calc_xi2 := x;
end;
FUNCTION calc_yi2(yi : vetor; n : Byte) : vetor;
var
x : vetor;
begin
FOR i := 1 TO i DO
x[i] := yi[i] * yi[i];
calc_yi2 := x;
end;
FUNCTION soma_xi(xi : vetor; n : Byte) : Real;
var
x : Real;
begin
x := 0;
FOR i := 1 TO n DO
x := x + xi[i];
soma_xi := x;
end;
FUNCTION soma_yi(yi : vetor; n : Byte) : Real;
var
x : Real;
begin
x := 0;
FOR i := 1 TO n DO
x := x + yi[i];
soma_yi := x;
end;
FUNCTION soma_xiyi(xiyi : vetor; n : Byte) : Real;
var
x : Real;
begin
x := 0;
FOR i := 1 TO n DO
x := x + xiyi[i];
soma_xiyi := x;
end;
FUNCTION soma_xi2(xi2 : vetor; n : Byte) : Real;
var
x : Real;
begin
x := 0;
FOR i := 1 TO n DO
x := x + xi2[i];
soma_xi2 := x;
end;
FUNCTION soma_yi2(yi2 : vetor; n : Byte) : Real;
var
x : Real;
begin
x := 0;
FOR i := 1 TO n DO
x := x + yi2[i];
soma_yi2 := x;
end;
Begin
writeln('Digite a quantidade de elementos: ');
read(n);
clrscr;
writeln('------VALORES DE xi------');
c.xi := recebe_valores(n);
clrscr;
writeln('------VALORES DE yi------');
c.yi := recebe_valores(n);
clrscr;
c.xiyi := calc_xiyi(c.xi, c.yi, n);
c.xi2 := calc_xi2(c.xi, n);
c.yi2 := calc_yi2(c.yi, n);
c.s_xi := soma_xi(c.xi, n);
c.s_yi := soma_yi(c.yi, n);
c.s_xiyi := soma_xiyi(c.xiyi, n);
c.s_xi2 := soma_xi2(c.xi2, n);
c.s_yi2 := soma_yi2(c.yi2, n);
FOR i := 1 TO n DO
writeln('xi = ', c.xi[i]:0:2);
writeln;
FOR i := 1 TO n DO
writeln('yi = ', c.yi[i]:0:2);
writeln;
FOR i := 1 TO n DO
writeln('xi2 = ', c.xi2[i]:0:2);
writeln;
FOR i := 1 TO n DO
writeln('yi2 = ', c.yi2[i]:0:2);
writeln;
FOR i := 1 TO n DO
writeln('xiyi2 = ', c.xiyi[i]:0:2);
writeln;
writeln('Somatório de xi = ', c.s_xi:0:2);
writeln('Somatório de yi = ', c.s_yi:0:2);
writeln('Somatório de xiyi = ', c.s_xiyi:0:2);
writeln('Somatório de xi2 = ', c.s_xi2:0:2);
writeln('Somatório de yi2 = ', c.s_yi2:0:2);
at := n * (c.s_xiyi);
bt := c.s_xi * c.s_yi;
ct := n * c.s_xi2 - EXP(ln(c.s_xi) * 2);
dt := (n * c.s_yi2) - EXP(ln(c.s_yi) * 2);
et := ct * dt;
writeln;
writeln('r = ', at:0:2, ' - ', bt:0:2, ' / Raíz de (', ct:0:2, ' . ', dt:0:2, ')');
writeln('r = ', (at - bt):0:2, ' / Raíz de (', et:0:2, ')');
IF et < 0 THEN
et := (sqrt(et * -1)) * -1
ELSE et := sqrt(et);
writeln('r = ', (at - bt):0:2, ' / ', et:0:2 );
r := (at - bt) / et;
writeln('r = ', r:0:2);
writeln;
readln;
end.
Program DeterminarCoeficientePearson;
Var
n, cont, i: integer;
somax, somay, somax2, somay2, somaxy:real;
t_x, t_y, y, x, y_medio, x_medio, a, b, r, r2, outro:real;
resp:char;
v_x: array[1..15] of real;
v_y: array[1..15] of real;
correlacao:string;
Begin
writeln;
writeln('Programa que determina o coeficiente de Pearson, interpreta o coeficiente de determinação,');
writeln(' gera a equação da reta e aceita valores distintos para calcular a relação na reta entre x e y.');
writeln;
{Aqui Pergunta se ja tem o somatório de x e y}
write('Já tem a soma dos valores dos pares elevados ao quadrado e a multiplicação entre x e y? (s/n) ');
readln(resp);
{Aqui recebe os valores do Somatório de x e y, caso
o usuário ja tenho calculado}
if resp = 's' then
begin
write('Somatório de x: ');
readln(somax);
write('Somatório de y: ');
readln(somay);
write('Somatório de x²: ');
readln(somax2);
write('Somatório de y²: ');
readln(somay2);
write('Somatório de xy: ');
readln(somaxy);
write('Pares: ');
readln(n);
write('Atribua um valor para x: ');
readln(x);
end
else
begin
{Caso o usuário ainda não tenha calculado o somatório de x e y,
aqui o programa recebe cada valor de x e y e a quantidade de elementos
na tabela, para calcular os somatórios}
writeln('Digite a quantidade de pares: ');
readln(n);
cont := 0;
{de acordo com a quantidade de elementos, o programa recebe
os valores de x e y}
for i := n downto 1 do
begin
cont := cont + 1;
writeln(cont, 'º valor de x: ');
readln(v_x[cont]);
writeln(cont, 'º valor de y: ');
readln(v_y[cont]);
end;
cont := 0;
for i := n downto 1 do
begin
cont := cont + 1;
somax := somax + v_x[cont];
somay := somay + v_y[cont];
somax2 := somax2 + (v_x[cont] * v_x[cont]);
somay2 := somay2 + (v_y[cont] * v_y[cont]);
somaxy := somaxy + (v_x[cont] * v_y[cont]);
end;
write('Atribua um valor para x: ');
readln(x);
end;
//Coeficiente de relação de Pearson
r := (n * somaxy - somax * somay) / ( sqrt(n * somax2 - somax * somax) * sqrt(n * somay2 - somay * somay) );
writeln;
writeln(' Coeficiente de Corelação | Correlação');
writeln(' ---------------------------------------------------------------');
writeln(' -1 = r | Perfeita negativa');
writeln(' -1 < r <= -0,95 | Muito forte negativa');
writeln(' -0,95 < r <= -0,65 | Forte negativa');
writeln(' -0,65 < r <= -0,35 | Moderada negativa');
writeln(' -0,35 < r < 0 | Fraca negativa');
writeln(' r = 0 | Nula');
writeln(' 0 < r < 0,35 | Fraca positiva');
writeln(' 0,35 <= r < 0,65 | Moderada positiva');
writeln(' 0,65 <= r < 0,95 | Forte positiva');
writeln(' 0,95 <= r < 1 | Muito forte positiva');
writeln(' 1 = r | Perfeita positiva');
writeln(' ---------------------------------------------------------------');
writeln;
if r = -1 then
correlacao := 'Perfeito negativa'
else if (r > -1) and (r <= -0.95 ) then
correlacao := 'Muito forte negativa'
else if (r > -0.95) and (r <= -0.65 ) then
correlacao := 'Forte negativa'
else if (r > -0.65) and (r <= -0.35 ) then
correlacao := 'Moderada negativa'
else if (r > -0.35) and (r < 0 ) then
correlacao := 'Fraca negativa'
else if r = 0 then
correlacao := 'Nula'
else if (r > 0) and (r < 0.35 ) then
correlacao := 'Fraca positiva'
else if (r >= 0.35) and (r < 0.65 ) then
correlacao := 'Moderada positiva'
else if (r >= 0.65) and (r < 0.95 ) then
correlacao := 'Forte positiva'
else if (r >= 0.95) and (r < 1 ) then
correlacao := 'Muito forte positiva'
else
correlacao := 'Perfeito positiva';
//Coeficientente de determinação
r2 := (r * r) * 100;
outro := r2 - 100;
b := (n * somaxy - somax * somay) / (n * somax2 - somax * somax);
y_medio := somay / n;
x_medio := somax / n;
a := y_medio - b * x_medio;
y := a + b * x;
writeln('Somatório x: ', somax:0:2);
writeln('Somatório y: ', somay:0:2);
writeln('Somatório x²: ', somax2:0:2);
writeln('Somatório y²: ', somay2:0:2);
writeln('Somatório x.y: ', somaxy:0:2);
writeln;
writeln;
writeln('O coeficiente de relação de Pearson foi: ', r:0:2, ' -> ', correlacao);
writeln(r2:0:2, '% da variação de y é explicado pela variação de x');
writeln(outro:0:2, '% da variação de y é explicado por outros fatores');
writeln('Equação(y = a + bx) da reta é: y = ', a:0:2,' + ', b:0:2, 'x' );
writeln('Quando x for', x:0:2, 'o y será: ', y:0:2);
writeln;
writeln;
End.
Program equacao2;
Var
a, b, c : Integer;
delta, x, x1, x2 : Real;
FUNCTION calc_delta(a : Integer, b : Integer, c : Integer) : Integer;
begin
delta := (b * b) - 4 * a * c;
end;
FUNCTION calc_x(a );
Begin
writeln('Digite o valor de "a", "b" e "c": ');
read(a, b, c);
IF delta < 0 THEN
begin
writeln('Não existe raízes reais.');
readln;
end
ELSE
begin
IF delta = 0 THEN
begin
x := (-b) / (2 * a);
//X
writeln('Delta = 0, então as duas raízes são iguais.');
IF -b mod (2 * a) = 0 THEN
begin
writeln('x = ', x:0:0);
readln;
end
ELSE
begin
writeln('x = ', -b, '/', 2 * a);
readln;
end;
end
ELSE IF delta > 0 THEN
begin
x1 := (-b + sqrt(delta)) / 2 * a;
x2 := (-b - sqrt(delta)) / 2 * a;
//X1
writeln('Delta > 0, então existem duas raízes reais.');
IF ((-b + sqrt(delta)) / (2 * a)) - INT((-b + sqrt(delta)) / (2 * a)) = 0 THEN
begin
writeln('x1 = ', x1:0:0);
readln;
end
ELSE
begin
writeln('x1 = ', (-b + sqrt(delta)), '/', 2 * a);
end;
//X2
IF ((-b - sqrt(delta)) / (2 * a)) - INT((-b - sqrt(delta)) / (2 * a)) = 0 THEN
begin
writeln('x2 = ', x2:0:0);
readln;
end
ELSE
begin
writeln('x2 = ', (-b - sqrt(delta)), '/', 2 * a);
readln;
end;
end
ELSE
begin
writeln('Não existem raízes reais.');
end;
end;
End.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment