Question
C programming: I tried to write a integral function. The program should take 4 arguments: function, a, b, deltax where function is 1, 2, or
C programming:
I tried to write a integral function.
The program should take 4 arguments: function, a, b, deltax where function is 1, 2, or 3. If function is 1, () = sin(). If function is 2, () = . If function is 3, () = sin(^2).
The output should be like this:
$ ./integral 1 0 3.1415926 0.0001
integral value = 2.000000
$ ./integral 2 0 2 0.0001
integral value = 2.666667
this is the code I wrote, the output is not correct, can you help me to fix it.
#include #include
int function; double x, y, a, b, deltax, N, i, result, result1;
double power(double x, double y) { double result1 = 1; for(y;y>0;y--) { result = result1*x; } return result1; }
double integ(int function, double a, double b, double deltax) { double result=0; double N=(b-a)/deltax; double i = N-1;
if(function==1) { result=-cos(b)+cos(a); } else if(function ==2) { result=(power(b,3)-power(a,3))/3; } else if(function ==3) { result=deltax*(sin(power((a+i*deltax),2))+sin(power((a+N*deltax),2))/2); } return result; }
int main(int atoc, char **argv) { int val1=atoi(argv[1]); double val2=atof(argv[2]); double val3=atof(argv[3]); double val4=atof(argv[4]);
double z = integ(val1,val2,val3,val4); printf("integral value = %d ", z);
}
f(x)dx = F(b) F(a) dx where F is the antiderivative of f - i.e. f = F. If the function f is simple, it's possible to find the antiderivative. For example, if f = 2x then F = x2. However, if the function is more complicated, it's not possible to easily find the antiderivative this is called a nonelementary antiderivative. For example, if f = sin (x), the antiderivative is not obvious. From calculus, you should remember that an integral can be calculated as a sum of trapezoids. So, for non-obvious antiderivatives like sin (x), we can use the following trapezoid approximation: i=N-1 F f(a + idx) + f(a +(i + 1)^x). S*f(x)dx = AX i=0 Where N = b. This is equivalent to calculating the sum of (f(x)=f(x+^x) Ax ) as x goes from a to b in steps of AxStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started