Skip to content

Commit

Permalink
added simpsons rule in c++ and java (#1828)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuktirapartiwar authored Mar 2, 2020
1 parent a3445e7 commit f904f12
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Simpsons_rule/Simpsons_Rule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//Simpson's Rule in c++
//It is used to calculate numerical approximation of definite integral
//In this rule, number of intervals must be EVEN

#include <iostream>
using namespace std;

//Function to calculate f(x) where f(x) = x * x
float func(float x){
return x*x;
}

//Function to calculate approximate integral
float simpsons (float lb, float ub, int n){
float h = (ub - lb) / n;
float result = 0;
for (int i = 0; i <= n; i++){
if(i == 0 || i == n)
result = result + func(lb + i * h);
else if( i % 2 != 0)
result = result + 4 * func(lb + i * h);
else
result = result + 2 * func(lb + i * h);
}
result = result * (h / 3);
return result;
}

int main() {
float lower_limit, upper_limit, output;
int intervals;
cout << "Enter lower limit = ";
cin >> lower_limit;
cout << "Enter upper limit = ";
cin >> upper_limit;
cout<< "Enter number of intervals = ";
cin >> intervals;
if(intervals % 2 == 0){
output = simpsons(lower_limit, upper_limit, intervals);
cout << "Output = " << output;
}
else
cout << "Interval must be even.";
return 0;
}

//output -
//Enter lower limit = 4
//Enter upper limit = 5.2
//Enter number of intervals = 6
//Output = 25.536
57 changes: 57 additions & 0 deletions Simpsons_rule/Simpsons_Rule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//Simpson's Rule in java
//It is used to calculate numerical approximation of definite integral
//In this rule, number of intervals must be EVEN

import java.util.Scanner;

public class Main {

//Function to calculate f(x) where f(x) = x * x
static float func(float x){
return x * x;
}
//Function to calculate approximate integral
static float simpsons(float lb, float ub, int n){
float h = (ub - lb) / n;
float result = 0;
for (int i = 0; i <= n; i++){
if(i == 0 || i == n)
result += func(lb + i * h);
else if( i % 2 != 0)
result += 4 * func(lb + i * h);
else
result += 2 * func(lb + i * h);
}
result = result * (h / 3);
return result;

}

public static void main(String[] args) {
float lower_limit, upper_limit, output;
int intervals;
Scanner scanner= new Scanner(System.in);
System.out.println("Enter lower limit = ");
lower_limit = scanner.nextFloat();
System.out.println("Enter upper limit = ");
upper_limit = scanner.nextFloat();
System.out.println("Enter number of intervals = ");
intervals = scanner.nextInt();
if(intervals % 2 == 0){
output = simpsons(lower_limit, upper_limit, intervals);
System.out.println("Output = "+output);
}
else
System.out.println("Interval must be even.");
}
}

//output -
// Enter lower limit =
// 4
// Enter upper limit =
// 5.2
// Enter number of intervals =
// 6
// Output = 25.535994

0 comments on commit f904f12

Please sign in to comment.