-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added simpsons rule in c++ and java (#1828)
- Loading branch information
1 parent
a3445e7
commit f904f12
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|