Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added simpsons rule in c++ and java #1828

Merged
merged 6 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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