Using matlab: 3.2 Simpson\'s Rule you can access the interpolation polynomial gi
ID: 3706399 • Letter: U
Question
Using matlab:
3.2 Simpson's Rule you can access the interpolation polynomial given a set of points Now that the exact integral of a polynomial of degree 2, you can implement Simpson's Rule where you only need to interpolate on sets of three points. and compute function [I, e]-SimpsonIntegral (f, a, b, n) Input Type Description The function handle (for the function to be in- tegrated) The lower limit of the integral. Th 1 function handle 1x1 double 1x1 double 1x1 double e upper limit of the integral. Subdivide the interval Ja, b in 2n equal parts. Output The integral of the function handle f using Simpson's Rule and subdividing the interval la, ? in 2n equal parts. The absolute value of the error between estimate of the integral and Matlab's esti- mate which you can obtain using the function integral. 1x1 double 1x1 double your Here you will write a function to calculate the integral I of the function handle f using Simpson's Rule and subdividing the interval a, b in 2n equal parts. Details . You must use the function you wrote in Lab 10 to get the coefficients of the Lagrange polynomial to pass into TrinomialIntegral.m to compute the integral . Make sure the functions are working before you use them here! Don't forget to include my lagrange.m when you submit!Explanation / Answer
By using Simpson's one third rule calculate ?0.60exdx?00.6exdx and correct upto four decimal places, taking six intervals.
By using Simpson's one third rule calculate ??20?1?0.162sin2xdx?0?21?0.162sin2?xdx and correct upto four decimal places, taking six intervals.
// Java program for simpson's 1/3 rule
public class GfG{
// Function to calculate f(x)
static float func(float x)
{
return (float)Math.log(x);
}
// Function for approximate integral
static float simpsons_(float ll, float ul,
int n)
{
// Calculating the value of h
float h = (ul - ll) / n;
// Array for storing value of x
// and f(x)
float[] x = new float[10];
float[] fx= new float[10];
// Calculating values of x and f(x)
for (int i = 0; i <= n; i++) {
x[i] = ll + i * h;
fx[i] = func(x[i]);
}
// Calculating result
float res = 0;
for (int i = 0; i <= n; i++) {
if (i == 0 || i == n)
res += fx[i];
else if (i % 2 != 0)
res += 4 * fx[i];
else
res += 2 * fx[i];
}
res = res * (h / 3);
return res;
}
// Driver Code
public static void main(String s[])
{
// Lower limit
float lower_limit = 4;
// Upper limit
float upper_limit = (float)5.2;
// Number of interval
int n = 6;
System.out.println(simpsons_(lower_limit,
upper_limit, n));
}
}
// Java program for simpson's 1/3 rule
public class GfG{
// Function to calculate f(x)
static float func(float x)
{
return (float)Math.log(x);
}
// Function for approximate integral
static float simpsons_(float ll, float ul,
int n)
{
// Calculating the value of h
float h = (ul - ll) / n;
// Array for storing value of x
// and f(x)
float[] x = new float[10];
float[] fx= new float[10];
// Calculating values of x and f(x)
for (int i = 0; i <= n; i++) {
x[i] = ll + i * h;
fx[i] = func(x[i]);
}
// Calculating result
float res = 0;
for (int i = 0; i <= n; i++) {
if (i == 0 || i == n)
res += fx[i];
else if (i % 2 != 0)
res += 4 * fx[i];
else
res += 2 * fx[i];
}
res = res * (h / 3);
return res;
}
// Driver Code
public static void main(String s[])
{
// Lower limit
float lower_limit = 4;
// Upper limit
float upper_limit = (float)5.2;
// Number of interval
int n = 6;
System.out.println(simpsons_(lower_limit,
upper_limit, n));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.