Romberg integration method written in C #

Source: Internet
Author: User

A long time ago, someone in the group asked me if I had used the credit function written in C #. Because I had used a credit calculator before, I told him to search for the Romberg integral method, but later he said that all the data written on the internet is written in C ++. If there is no C #, I asked if I did it, but I did it at home and there was no copy in the school, unfortunately.

I turned over my mailbox today and found that I forgot to add a tag. As a result, I was not found at the time. I feel a little sorry for the brother. I 'd like to share it here.

I have rummaged over the InternetCodeAll copiesAlgorithmI just replaced the formula and reduced it to only four items, reducing the space requirement. If you have any suggestions for improvement, please kindly advise! The following is a code snippet:

 

 Code 
/// <Summary>
/// Romberg longberger Algorithm
/// </Summary>
/// <Param name = "F"> Product Expression Function </Param>
/// <Param name = "A"> Minimum credits </Param>
/// <Param name = "B"> Credit Limit </Param>
/// <Param name = "E"> Precision </Param>
/// <Param name = "maxsteps"> Maximum iterations </Param>
/// <Returns> </returns>
Public Static Double Romberg (func < Double , Double > F, Double A, Double B, Double E, Int Maxsteps ){
Double M = 2835.0 ;
Double A = 217.0 ;
Double B = 2048.0 ;
Double C = 352.0 ;
Double D = 218.0 ;

Double T1n = (B - A) * (F (B) + F ()) / 2 ;
Double H4n = H (F, A, B, 4 );
Double H2n = H (F, A, B, 2 );
Double H1n = H (F, A, B, 1 );

Double R1 = ( * T1n + B * H4n + C * H2n + D * H1n) / M;
Double R2 = 0.0 ;

For ( Int Steps = 0 ; Steps < Maxsteps; ++ Steps ){
T1n = (T1n + H1n) / 2 ;
H1n = H2n;
H2n = H4n;
H4n = H (F, A, B, 8 < Steps );
R2 = ( * T1n + B * H4n + C * H2n + D * H1n) / M;

If (Math. Abs (r2 - R1) <= E ){
Return R2;
}
Else {
R1 = R2;
}
}
Return R2;
}

Private Static Double H (func < Double , Double > F, Double A, Double B, Int N ){
Double H = (B - A) / N;
Double Sum = 0.0 ;
For ( Int K = 0 ; K < N; ++ K ){
Sum + = F ( + (K + 0.5 ) * H );
}
Return H * SUM;
}

 

The Private Static double H method is actually the simplest trapezoid method. Maybe it can be written as an anonymous method in the Romberg Method, but because the Romberg Method in my class still has a lot of overloading, so let's write it like this. Maybe we can do this when C #4.0 comes out and there are optional variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.