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;
}
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.
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.
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.