Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in C coding Write a program that can calculate a definite integral of a function. If you haven't taken Calculus 2, a definite integral is

in C coding image text in transcribed
image text in transcribed
Write a program that can calculate a definite integral of a function. If you haven't taken Calculus 2, a definite integral is defined as follows: abf(x)dx=F(b)F(a) where F is the antiderivative of f-i.e. f=dxdF. 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(x2), the antiderivative is not obvious. An integral can be calculated as a sum of trapezoids. So, for non-obvious antiderivatives like sin(x2), we can use the following trapezoid approximation: abf(x)dx=i=0l=N12f(a+ix)+f(a+(i+1)x)x Where N=xba. Another way to say this is that you are calculating the sum of (2f(x)+f(x+x)x) as x goes from a to b in steps of x. Your program should take 4 arguments: function a b deltax where function is 1,2 , or 3 . If function is 1,f(x)=sin(x). If function is 2,f(x)=x2. If function is 3,f(x)=sin(x2). For example, to calculate 0sin(x) with x=0.0001, the program will do the following: $. /integral 103.14159260.0001 integral value =2.000000 To calculate 02x2, your program should do the following: $. I integra1 2020.0001 integral value =2.666667 Writing the code Starter code is provided for you below int main (int args, char **argv) f int fselect =atoi(argv[1]); double a = atof (argv [2]); double b=atof(argv[3]); double deltax = atof (argv[4]); double integral =0; /I TODO wite your code here printf:("integral =8fln, integral); 1 I have provided code to input the 4 arguments using a rgv and also code to print the results. Do not change this code. The first argument is an integer, but the last 3 arguments are floating-point arguments, so that's why 1 use the double type and the atof function instead of atoi. You will need to write a loop that iterates from i=0 to i=N1 and accumulates the integral sum using the formula above. Depending on the value of the fselect variable, you will change which function you call in the formula. Do not use any functions from the built-in math library. You can use the sine function from the third lab exercise. If you're having trouble figuring out why you're getting wrong answers, put in print f 's in the code to print out important values and see if it is what you expected. You can also use the gdb debugger to help debug the code. Often times, this is more efficient and quicker than using printf's. There is a video in the resources folder that explains how to use the gdb debugger. Submit your . c file on HuskyCT. Use comments to explain what your code does

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle PL/SQL Programming Database Management Systems

Authors: Steven Feuerstein

1st Edition

978-1565921429

More Books

Students also viewed these Databases questions