Escolar Documentos
Profissional Documentos
Cultura Documentos
AULA # 5
UMA APLICAÇÃO: INTEGRAÇÃO NUMÉRICA
f(x)
a b x
•um enfoque para estimar o valor da integral, é
dividir a área baixo a curva em formas geométricas
regulares,
•calcular a área de cada uma das formas, e somar
todas as áreas,
INTEGRAÇÃO NUMÉRICA
•se utilizarmos como forma regular o trapézio
teremos a regra dos trapézios,
Y
f(x)
a b x
Ai = (h/2)[f(xi-1)+f(xi)]
f(xi)
h
xi-1 xi x
INTEGRAÇÃO NUMÉRICA
•A área total:
PROGRAMA SERIAL
int main()
{
float integral, a=0.0, b=1.0, h, x;
int n=512, i;
h = (b-a)/n;
integral = (f(a) + f(b))/2;
x = a;
for(i=1; i<=n-1; i++){
x += h;
integral += f(x);
}
integral *= h;
a b x
0 1 2 3
PARLELIZANDO A REGRA DO TRAPÉZIO
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_Size(MPI_COMM_WORLD, &p);
h = (b-a)/n;
local_n = n/p;
local_a = a + rank*local_n*h;
local_b = local_a + local_n*h;
integral = Trap(local_a, local_b, local_n, h);
if(rank==0){
total = integral;
for(i=1; i<p; i++){
MPI_Recv(&integral, 1, MPI_Float, i, 0,
MPI_COMM_WOLRD, &status);
total += integral;
}
printf("Com n=%d trapezios, a estimativa da\n", n);
printf("integral de %.2f a %.2f e: %.6f\n",
a, b, integral);
} else
MPI_Send(&integral, 1, MPI_FLOAT, 0, 0,
MPI_COMM_WOLRD);
MPI_Finalize();
return 0;
}
float f (float x){
return x*x + 1.0;
}
return integral;
}
PARLELIZANDO A REGRA DO TRAPÉZIO
•Exercício:
Modifique o programa anterior, introduzindo uma
função GET_DATA(), que leia os dados de
entrada no processo 0 e envie os dados lidos aos
outros processos.