trapline - printf("Estimate of integral =...

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

View Full Document Right Arrow Icon
/* 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);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: printf(&quot;Estimate of integral = %f\n&quot;, 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 &lt; n - 1; i++) { dx = x[i + 1] - x[i]; integral += 0.5*dx*(f[i] + f[i+1]); } return integral; }...
View Full Document

Ask a homework question - tutors are online