Numerical Integration

I need help developing a C-code that numerically integrates using N = 20 and N = 40, where N is the number of steps or interval and print out all xi, fi=F(xi) and final result.

This is what i have so far but i do not know how to add the latter part of the question.

(1) use of arrays, (2) use of pointers, (3) use of structure, (4) use of union, (5) use of functions and function calls, (6) formatted output on screen and (7) saving of the same output on a file.

Can someone help me? I do not know how to add structure or pointers with a formatted output. This is as far as i can get

#include <stdio.h>
#include <math.h>
#define PI 3.14159265

double f(double x);
double simpsons(int n, double a, double b);

int main()
{
double x0 = 0, xn = 10;
int i, n = 20;

for (i = 1; i <= 2; i++) {
n = i*20;
printf( "For n=%d, the integral is %f\n\n", n, simpsons(n, x0, xn));
n=n+20;
}
return 0;
}

double f(double x)
{
double y;

y = (x*exp(x))*pow(sin(0.5*PI*x),2);

return y;
}

double simpsons(int n, double a, double b)
{
int i;
double dx, x, sum;

dx = (b - a) / n;
sum = f(a) + f(b);
for (i = 1; i < n; i++) {
x = a + dx * i;
sum += 2 * (1 + i%2) * f(x);
}
sum *= dx/3;
return sum;
}
Last edited on
Your code looks fine to me.

(1) use of arrays, (2) use of pointers, (3) use of structure, (4) use of union
I can't see how these apply to this problem.

(5) use of functions and function calls
You've done that.

(6) formatted output on screen
You've done that.

(7) saving of the same output on a file.
Open a file for output, and use fprintf to write to the file where you already use printf.
This is required for this program per instructor. Can you show me how to (1) use of arrays, (2) use of pointers, (3) use of structure, (4) use of union and saving of the same output on a file.

This is what i need done and i was never taught how to do that.

saving of the same output on a file.
I've explained that above.

(1) use of arrays, (2) use of pointers, (3) use of structure, (4) use of union
IT doesn't make sense to try to use these on your problem. Maybe someone else can help.

Perhaps if you posted the original question, we could see if you've omitted something, but as it stands ...
Develop a C-code that numerically integrates the following function:


F(x) = x + 2x^3 + 4x^7 from (0,20)


using N = 20 and N = 40, where N is the number of steps or intervals. Print out all xi, fi=f(xi) and final result.

Analytically integrate the function you choose for the interval and compare the result with the sum calculated using the above formula.

Your C-code must contain the following features: (1) use of arrays, (2) use of pointers, (3) use of structure, (4) use of union, (5) use of functions and function calls, (6) formatted output on screen and (7) saving of the same output on a file.
Last edited on
Print out all xi, fi=f(xi) and final result.
You could collect all the results per run in array.

You'll find that once you start passing arrays around, you start using pointers.

You could contrive a struct that has the range and step size to be calculated that's passed to simpsons().

Once you have the results back in main, you can print them to stdout with printf and to a file with fprintf.
Uhm i dont follow. Im still new to programming. Is there anyway you could show me how to go about doing that?
If you need help, check out the tutorial on this website, it is an amazing resource for beginners.

http://www.cplusplus.com/doc/tutorial/arrays/ - About arrays and how to use them.

http://www.cplusplus.com/doc/tutorial/pointers/ - About pointers and how to use them.

Now you just have to come up with a way to use these in your program.

One helpful tip, the variable that you use to access an array is a pointer, the brackets will add that amount to the pointer before trying to access that memory location, so when you use arrays, you are using pointers.
Topic archived. No new replies allowed.