We wish to compute the definite integral I = Z 1 0 1 1 + √x dx numerically and compare to the exact result, Iexact = 2 − log 4. (a) Use the composite trapezium rule Z b a f(x)dx ' X N i=0 wifi, wi = ( Δx/2, i = 0 or i = N Δx 1 ≤ i ≤ N − 1 , Δx = b − a N , to compute the integral I, using N + 1 = 64 equidistant points in x ∈ [0, 1]. Use three instances of a vector to store the values of the gridpoints xi, function values fi = f(xi) and weights wi. [Hint: you may use the function from Question 2a to compute the dot product of the vectors wi and fi.] Output to the screen (and list in your report) your numerical result Itrapezium and the difference Itrapezium − Iexact.
#include<iostream.h>
#include <bits/stdc++.h>
#define n 3
using namespace std;
int dotProduct(int vect_A[], int vect_B[])
{
int product = 0;
// Loop for calculate cot product
for (int i = 0; i < n; i++)
product = product + vect_A[i] * vect_B[i];
return product;
}
void crossProduct(int vect_A[], int vect_B[], int cross_P[])
{
cross_P[0] = vect_A[1] * vect_B[2] - vect_A[2] * vect_B[1];
cross_P[1] = vect_A[2] * vect_B[0] - vect_A[0] * vect_B[2];
cross_P[2] = vect_A[0] * vect_B[1] - vect_A[1] * vect_B[0];
}
// Driver function
int main()
{
int vect_A[] = { 3, -5, 4 };
int vect_B[] = { 2, 6, 5 };
int cross_P[n];
cout << "Dot product:";
cout << dotProduct(vect_A, vect_B) << endl;
cout << "Cross product:";
crossProduct(vect_A, vect_B, cross_P);
for (int i = 0; i < n; i++)
cout << cross_P[i] << " ";
return 0;
}
Comments
Leave a comment