Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
Java
/
Maths
/
SimpsonIntegration.java
Java
/
Maths
/
SimpsonIntegration.java
SimpsonIntegration.java 3.10 KB
Copy Edit Raw Blame History
ggkogkou authored 2021年10月24日 15:24 +08:00 . Add Simpson's integration numerical method (#2681)
package Maths;
import java.util.TreeMap;
public class SimpsonIntegration{
/*
* Calculate definite integrals by using Composite Simpson's rule.
* Wiki: https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule
* Given f a function and an even number N of intervals that divide the integration interval e.g. [a, b],
* we calculate the step h = (b-a)/N and create a table that contains all the x points of
* the real axis xi = x0 + i*h and the value f(xi) that corresponds to these xi.
*
* To evaluate the integral i use the formula below:
* I = h/3 * {f(x0) + 4*f(x1) + 2*f(x2) + 4*f(x3) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)}
*
*/
public static void main(String[] args) {
SimpsonIntegration integration = new SimpsonIntegration();
// Give random data for the example purposes
int N = 16;
double a = 1;
double b = 3;
// Check so that N is even
if(N%2 != 0){
System.out.println("N must be even number for Simpsons method. Aborted");
System.exit(1);
}
// Calculate step h and evaluate the integral
double h = (b-a) / (double) N;
double integralEvaluation = integration.simpsonsMethod(N, h, a);
System.out.println("The integral is equal to: " + integralEvaluation);
}
/*
* @param N: Number of intervals (must be even number N=2*k)
* @param h: Step h = (b-a)/N
* @param a: Starting point of the interval
* @param b: Ending point of the interval
*
* The interpolation points xi = x0 + i*h are stored the treeMap data
*
* @return result of the integral evaluation
*/
public double simpsonsMethod(int N, double h, double a){
TreeMap<Integer, Double> data = new TreeMap<>(); // Key: i, Value: f(xi)
double temp;
double xi = a; // Initialize the variable xi = x0 + 0*h
// Create the table of xi and yi points
for(int i=0; i<=N; i++){
temp = f(xi); // Get the value of the function at that point
data.put(i, temp);
xi += h; // Increase the xi to the next point
}
// Apply the formula
double integralEvaluation = 0;
for(int i=0; i<data.size(); i++){
if(i == 0 || i == data.size()-1) {
integralEvaluation += data.get(i);
System.out.println("Multiply f(x" + i + ") by 1");
}
else if(i%2 == 1) {
integralEvaluation += (double) 4 * data.get(i);
System.out.println("Multiply f(x" + i + ") by 4");
}
else {
integralEvaluation += (double) 2 * data.get(i);
System.out.println("Multiply f(x" + i + ") by 2");
}
}
// Multiply by h/3
integralEvaluation = h/3 * integralEvaluation;
// Return the result
return integralEvaluation;
}
// Sample function f
// Function f(x) = e^(-x) * (4 - x^2)
public double f(double x){
return Math.exp(-x) * (4 - Math.pow(x, 2));
// return Math.sqrt(x);
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

All Algorithms implemented in Java
Cancel

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/syysui/Java.git
git@gitee.com:syysui/Java.git
syysui
Java
Java
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /