Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that can calculate a definite integral of a function. If you hav

ID: 2268728 • Letter: W

Question

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 f(x)dx = F(b)-F(a) dF where F is the antiderivative of f - i.e. f If the function f is simple, it's possible to find the antiderivative. For example, iff = 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(t), the antiderivative is not obvious In this case, we can use the following approximation: 1=N-1 f(a f(x)dx = 2 f(x)+f(x+4x) ) as goes Where N =-. This is equivalent to calculating summing up from a to b in steps of 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 !ffunction is 3, f(x) = sin (x2). So, to calculate sin (x) with = 0.0001, your program should do the following: $ ./integral 1 0 3.1415926 0.0001 integral value = 2.000000 To calculate f^ x2, your program should do the following: $ ./integral 2 0 20.0001 integral value = 2.666667

Explanation / Answer

#include<iostream>
#include<cmath>
using namespace std;
int main(){
int y;
cin>>y;
if(y==2){
float a,b,delx;
cin>>a>>b>>delx;
float n=(b-a)/delx;
float ans=0;
for(float i=0;i<=n-1;i++){
ans=ans+((pow((a+i*delx),2)+pow((a+(i+1)*delx),2))/2)*delx;
}
cout<<ans;
}
else if(y==1){
float a,b,delx;
cin>>a>>b>>delx;
float n=(b-a)/delx;
float ans=0;
for(float i=0;i<=n-1;i++){
ans=ans+((sin(a+i*delx)+sin(a+(i+1)*delx))/2)*delx;
}
cout<<ans;
}

else if(y==3){
float a,b,delx;
cin>>a>>b>>delx;
float n=(b-a)/delx;
float ans=0;
for(float i=0;i<=n-1;i++){
ans=ans+((sin(pow((a+i*delx),2))+sin(pow((a+(i+1)*delx),2)))/2)*delx;
}
cout<<ans;
}
}

Give feedback

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote