4. [6 points] Recall that we can compute an approximation of a definite integral
ID: 3710825 • Letter: 4
Question
4. [6 points] Recall that we can compute an approximation of a definite integral (the area under a function curve) by summation of the areas of the trapezoids formed under the segments connecting the value of the function at pairs of points, denoted S; and S41, separated by some fixed interval (T in the figure). The area of each trapezoid is computed by (S, + Stt1)2 S(t) 9 10 11 For an arbitrary signal, such as illustrated above, one method of representing the signal is to store the values of the points S, called samples, in an array, so The following is a contract for a function to integral of the function represented by the given sampled dat function using the algorithm described above. implement this algorithm to comp ute the delinite . Give an implementation for the * inlegrale Numerical inlegralio7 * @parassampled data ber of dala poinls double integrate (double s [n. doubleExplanation / Answer
import java.util.Scanner;
public class AreaTrapezoid {
private static Scanner sc;
static double integrate(double s[], int n, double t)
{
double area=0.0;
for(int i=0;i<n-1;i++)
{
area=area+(0.5*(s[i]+s[i+1])/2);
}
return area;
}
public static void main(String[] args) {
double s[] = new double[20], t;
double Area;
int n, i;
sc = new Scanner(System.in);
System.out.println(" Please Enter number of data points = ");
n = sc.nextInt();
System.out.println(" Please Enter Sample rate t = ");
t = sc.nextDouble();
System.out.println(" Please Enter sample data points s[i] = ");
for(i=0;i<n;i++)
s[i] = sc.nextDouble();
Area = integrate(s,n,t);
System.out.println(" Area = ");
System.out.println(Area);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.