Science, asked by bikramjitmondal147, 11 months ago

Write a c-code for Simpson's 1/3rd rule and use it to find the value of the integral of (4х-3x²)dx with lower limit 0 and upper limit 1.
Correct upto five decimal places taking 10 sub-intervals.​

Attachments:

Answers

Answered by kapoorsamarth90
0

Answer:

It is used when it is very difficult to solve the given integral mathematically.

This rule gives approximation easily without actually knowing the integration rules.

Explanation:

// CPP program for simpson's 1/3 rule  

#include <iostream>  

#include <math.h>  

using namespace std;  

 

// Function to calculate f(x)  

float func(float x)  

{  

   return log(x);  

}  

 

// Function for approximate integral  

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[10], fx[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 program  

int main()  

{  

   float lower_limit = 4; // Lower limit  

   float upper_limit = 5.2; // Upper limit  

   int n = 6; // Number of interval  

   cout << simpsons_(lower_limit, upper_limit, n);  

   return 0;  

}

Similar questions