# Trapezoidal rule and Simpson's rule from fixed array

• (3 Pages)
• 1
• 2
• 3

## 31 Replies - 15443 Views - Last Post: 03 December 2010 - 12:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=203268&amp;s=d87298d4114aa5b0abb5c584d2b74cfc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 timwonderer

Reputation: 0
• Posts: 16
• Joined: 02-December 10

# Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 01:29 PM

Hi!
I am trying to write a program that uses Simpson's rule and Trapezoidal rule from a fixed array. But I am having trouble integrating the array into the rules.

Here is the code
``` #include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <math.h>
//Function prototypes
double f_value (double x);

int main (void){

int k;
double f[21], xx[21], del_x, x, s_value, t_value;
del_x=0.05;
for (k=0, x=0.; k<=20; k++){
xx[k]=x;
f[k]=f_value(x);
x+= del_x;
}
printf ("    x         f(x)\n");
for(k=0; k<21; k++) printf ("%8.2f %15.4e\n", xx[k], f[k]);
/*trapezoidal rule*/
t_value=
/*Simpson's rule*/
s_value=
return;
}

double f_value (double x)
{
double fx;
fx=sin(x)* exp(-0.5 *x*x);
return fx;
}

```

This post has been edited by timwonderer: 02 December 2010 - 01:39 PM

Is This A Good Question/Topic? 0

## Replies To: Trapezoidal rule and Simpson's rule from fixed array

### #2 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 02:11 PM

Can you be a little more specific as to what is causing you the trouble?

### #3 timwonderer

Reputation: 0
• Posts: 16
• Joined: 02-December 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 02:17 PM

ButchDean, on 02 December 2010 - 01:11 PM, said:

Can you be a little more specific as to what is causing you the trouble?

I am supposed to supply the f(x) value from f[k] (the one calculated by the f_value function) into simpson's rule and trapezoidal rule. But I am not sure how to take the value from f[k] and put it into the rules.

### #4 timwonderer

Reputation: 0
• Posts: 16
• Joined: 02-December 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 02:27 PM

i also know simpson's rule
[f(x0)+4*f(x0+dx)+2*f(x0+2dx)+4*f(x0+3dx)+2*f(x0+4dx)+... +2*f(xf-2dx)+4*f(xf-dx)+f(xf)]*dx/3.0
and trapezoidal rule
[f(x0)+2*f(x0+dx)+2*f(x0+2dx)+2*f(x0+3dx)+2*f(x0+4*dx)+...+2*f(xf-2dx)+2*f(xf-dx) +f(xf)]*dx/2.0

both have coefficient that alternates like 1,4,2,4,2....2,4,1

I am supposed to get the f() value of both rules from the f[k] value I calculated earlier in the code.

### #5 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 02:32 PM

I'm guess that your tutor wants you to used C as opposed to C++, so I'll stick with that.

You know your start and end points, 'a' and 'b' respectively. So the midpoint 'm' is simply something like float m = (a + b ) / 2;. So now you have your ranges to perform your calculations. I didn't see this in your code.

You will now need to create a function that computes f(x) to call itself recursively.

Does that help?

This post has been edited by ButchDean: 02 December 2010 - 02:33 PM

### #6 #define

• Duke of Err

Reputation: 1636
• Posts: 5,767
• Joined: 19-February 09

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 04:04 PM

Composite trapezoidal rule.

Although you have uniform intervals, you can still use the rule for non-uniform intervals. In the following there is an example in C++ which uses a for loop to iterate through the arrays/containers.

Trapezoidal rule - Wikipedia

### #7 timwonderer

Reputation: 0
• Posts: 16
• Joined: 02-December 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 04:12 PM

ButchDean, on 02 December 2010 - 01:32 PM, said:

I'm guess that your tutor wants you to used C as opposed to C++, so I'll stick with that.

You know your start and end points, 'a' and 'b' respectively. So the midpoint 'm' is simply something like float m = (a + b ) / 2;. So now you have your ranges to perform your calculations. I didn't see this in your code.

You will now need to create a function that computes f(x) to call itself recursively.

Does that help?

My starting f(x) value is when x=0.0 and ends at x=1.0 with intervals of 0.05
so the table of values looks like this
```    x         f(x)
0.00      0.0000e+00
0.05      4.9917e-02
0.10      9.9335e-02
0.15      1.4777e-01
0.20      1.9474e-01
0.25      2.3979e-01
0.30      2.8252e-01
0.35      3.2253e-01
0.40      3.5948e-01
0.45      3.9308e-01
0.50      4.2309e-01
0.55      4.4932e-01
0.60      4.7163e-01
0.65      4.8994e-01
0.70      5.0423e-01
0.75      5.1453e-01
0.80      5.2091e-01
0.85      5.2350e-01
0.90      5.2246e-01
0.95      5.1801e-01
1.00      5.1038e-01

```

so in the simpson (or trapezoidal rule) we have this 1*f() + 4*f()....thing, I am supposed to substitute the value of f() with the f(x) value from the table, which is a fixed size array stored in f[].

### #8 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 04:28 PM

### #9 timwonderer

Reputation: 0
• Posts: 16
• Joined: 02-December 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 04:33 PM

So I am supposed to get the f(x) value from the fixed array f[] and use it to evaluate with Simpson's rule and trapezoidal rules. then print the results on the screen.

I know that I need to use a for-loop for this, but I am not sure how to write the loop because of the different coefficient changes (1,4,2,4,2....2,4,1).

### #10 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 04:36 PM

Well no, you iterate through the array using an incrementing index i and retrieve the value from f[i].

### #11 timwonderer

Reputation: 0
• Posts: 16
• Joined: 02-December 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 04:49 PM

ButchDean, on 02 December 2010 - 03:36 PM, said:

Well no, you iterate through the array using an incrementing index i and retrieve the value from f[i].

Hmm.. can you please explain it a bit more ? I am afraid I don't understand it that well.

thank you!

### #12 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 04:57 PM

Okay, well:

1. You declare an array of type float and size 22.
2. Compute f(x) for each element in the array in order (of course).
3. Maybe you might want an array for the coefficient as well, lets call the array 'c'.

I think I should stop now. Getting too close to giving you the answer. you gotta work for the grade!

### #13 timwonderer

Reputation: 0
• Posts: 16
• Joined: 02-December 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 05:21 PM

After a long thought, I still couldn't figure out the coefficient sequence for 2,4,2,4.... so I did it the slow way (hopefully not the dumb way..hahaha)

```int k;
double f[21], xx[21], del_x, x, s_value, t_value;
del_x=0.05;
for (k=0, x=0.; k<=20; k++){
xx[k]=x;
f[k]=f_value(x);
x+= del_x;
}
printf ("    x         f(x)\n");
for(k=0; k<21; k++) printf ("%8.2f %15.4e\n", xx[k], f[k]);
/*trapezoidal rule*/
for (k=0; k<21; k++){
t_value=(1*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]
+1*f[k])*del_x/2.0;
}
for (k=0; k<21; k++){
/*Simpson's rule*/
s_value=(1*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]+2*f[k]+4*f[k]
+1*f[k])*del_x/3.0;
}
printf ("t_value = %12.4e s_value = %12.4e\n", t_value, s_value);
return;
}

double f_value (double x)
{
double fx;
fx=sin(x)* exp(-0.5 *x*x);
return fx;
}

```

and the output becomes
```       x         f(x)
0.00      0.0000e+00
0.05      4.9917e-02
0.10      9.9335e-02
0.15      1.4777e-01
0.20      1.9474e-01
0.25      2.3979e-01
0.30      2.8252e-01
0.35      3.2253e-01
0.40      3.5948e-01
0.45      3.9308e-01
0.50      4.2309e-01
0.55      4.4932e-01
0.60      4.7163e-01
0.65      4.8994e-01
0.70      5.0423e-01
0.75      5.1453e-01
0.80      5.2091e-01
0.85      5.2350e-01
0.90      5.2246e-01
0.95      5.1801e-01
1.00      5.1038e-01
t_value =   7.6557e-01 s_value =   5.1038e-01

```

### #14 #define

• Duke of Err

Reputation: 1636
• Posts: 5,767
• Joined: 19-February 09

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 05:26 PM

If you take the first two values in each array, that will give you a, b, f(a) and f(b). Then you can calculate the approximation of the integral for that interval. Then if/as you calculate the rest of the intervals you can sum them.

### #15 timwonderer

Reputation: 0
• Posts: 16
• Joined: 02-December 10

## Re: Trapezoidal rule and Simpson's rule from fixed array

Posted 02 December 2010 - 05:35 PM

#define, on 02 December 2010 - 04:26 PM, said:

If you take the first two values in each array, that will give you a, b, f(a) and f(b). Then you can calculate the approximation of the integral for that interval. Then if/as you calculate the rest of the intervals you can sum them.

The first values of each array? Do you mean from the f[] array and the coefficient array? Sorry...I am only beginning to learn C language..Can you please explain?

Thanks