# trapline - printf"Estimate of integral =%f\n"...

This preview shows page 1. Sign up to view the full content.

/* trap_line.c * Example code to integrate the function f(x)=x+1/2 defined by * the arrays x[N], f[N], using the trapezoidal rule. */ #include <stdio.h> #define N 10 float trapzd(float x[], float f[], int n); int main() { int i; float x[N], f[N], integral; /* define x[], f[] */ for (i = 0; i < N; i++) { x[i] = 1.0*i/(N - 1.0); f[i] = x[i] + 0.5; } integral = trapzd(x, f, N);
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: printf("Estimate of integral = %f\n", integral); return 0; } float trapzd(float x, float f, int n) { /* apply trapezoidal rule to integrate f[n] defined on x[n] */ float integral, dx; int i; integral = 0.0; for (i = 0; i < n - 1; i++) { dx = x[i + 1] - x[i]; integral += 0.5*dx*(f[i] + f[i+1]); } return integral; }...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online