Write a program which calculates the integral of the function
f(x)=g(x)/n! , where g(x)=1+K*cos^n( n*x)
on the interval from a to b (a<b). In the main program, scanf a
non-negative integer value of n and double values of K, a, and b.
Call the function Integ() to evaluate the integral.
Your main program should be followed by four functions:
double Integ(int n, double K, double a, double b)
double func(double x, int n, double K) to evaluate f(x)
double gunc(double x, int n, double K) to evaluate g(x)
double nfact(int n) to evaluate n!
integrate an arbitrary function f(x) from a to b (letting the
program ask you for n_trap only once). Within the Integ() function
call another function func() to specify f(x). Within the function
func(), call the function nfact() to evaluate n!, and to
to evaluate g(x) call the function gunc().
This is my code so far, but ssh keeps prompting for errors that I can't find. Could someone tell me what's wrong with the code?
f(x)=g(x)/n! , where g(x)=1+K*cos^n( n*x)
on the interval from a to b (a<b). In the main program, scanf a
non-negative integer value of n and double values of K, a, and b.
Call the function Integ() to evaluate the integral.
Your main program should be followed by four functions:
double Integ(int n, double K, double a, double b)
double func(double x, int n, double K) to evaluate f(x)
double gunc(double x, int n, double K) to evaluate g(x)
double nfact(int n) to evaluate n!
integrate an arbitrary function f(x) from a to b (letting the
program ask you for n_trap only once). Within the Integ() function
call another function func() to specify f(x). Within the function
func(), call the function nfact() to evaluate n!, and to
to evaluate g(x) call the function gunc().
This is my code so far, but ssh keeps prompting for errors that I can't find. Could someone tell me what's wrong with the code?
Code:
#include<stdio.h>
#include<math.h>
double Intgr(int n, double K, double a, double b);
double func(double x, int n, double K;
double gunc(double x, int n, double K);
double mfact(int n);
main()
{
printf("\n\n");
int n, n_trap;
double K, a, b, integral;
printf("Enter the non-negative integer: ");
scanf("%d", &n);
printf("Enter the coefficient K in g(x): ");
scanf("%lf", &K);
printf("Enter the bounds for the integration interval, a < b : ");
scanf("%lf %lf", &a, &b);
printf("\n");
printf("Integrate f(x)on [a,b]\n");
integral = Intgr(n, K, a, b);
printf("\nThe value of the integral is %.5f\n", integral);
printf("\n\n");
}
double Intgr(int n, double K, double a, double b)
{
int k, n_trap, n1;
double f, del_x, x, sum;
printf("Enter the number of trapezoids: ");
scanf("%d", &n_trap);
n1 = n_trap + 1;
del_x = (b - a)/n_trap;
x = a;
f = func(x,n,K);
sum = -0.5 * f;
for(k=0; k<n1; k++)
{
x = a + k * del_x;
f = func(x,n,K);
sum += f;
}
sum -= 0.5 * f;
sum *= del_x;
return sum;
}
double gunc(double x, int n, double K)
{
double g;
g = 1+K*cos^n(n*x);
return g;
}
double func(double x, int n, double K)
{
double factorial, y;
if(n <= 0) y = K;
else
{
factorial = mfact(n);
y = gunc(x,n,K)/factorial;
}
return y;
}
double mfact(int n)
{
int i=1, n_fact=1;
for(i=m; i>0; i--)
{
n_fact *= i;
}
return n_fact;
}
Comment