C Language Review---Rectangular method for determining integral function

Source: Internet
Author: User

One: Analysis:
When I was studying for a freshman, we learned that we could use the rectangle method to find the integral. The idea is to divide the integral interval into n equal parts, then approximate the n equal parts as a rectangle (or trapezoid), and then sum the area of all rectangles (or trapezoidal).

Two: A simple example
The definite integral of finding function x^2

Rectangle Method:

#include <iostream>#include<math.h>using namespacestd;intMain () {floatFunfloatx); floatA, B; cout<<"Please enter the lower bound of the definite integral of the function x^2 A and upper bound B:"; CIN>> a >>b; intn = -;//divide the interval into 50 parts    floath = (b-a)/n;//h is the size of each interval    floats =0;//S is the area of the rectangle and    floati =0;  for(i = A; I < b; i + =h) {s= s + Fun (i) *h; } cout<<"\ n The result is:"<< s <<Endl; cout<<Endl;}
floatFunfloatx) { returnPOW (x,2);}
Three: Using C language to achieve the following three functions of the definite integral solution

#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#include<math.h>//a general function of using rectangle method to find definite integral//p is the function pointer, a is the lower bound, B is the upper bound, N is the equal numberfloatIntegralfloat(*p) (float),floatAfloatBintN) {    inti; floatArea=0; floatew = (b-a)/N;  for(i =1; I <= n;i++) Area+ = (*p) (A + i*ew) *ew; returnArea ;}floatF_sin (floatx) {    returnsin (x);}floatF_cos (floatx) {    returncos (x);}floatF_exp (floatx) {    returnexp (x);}intMain () {floatA, B,area; float(*p) (float); intn = -; printf ("Test Sin,input A, B:"); scanf ("%f,%f", &a, &b); P=F_sin; Area=Integral (P, a, B, N); printf ("Get value:%f\n", area); printf ("Test Cos,input A, B:"); scanf ("%f,%f", &a, &b); P=F_cos; Area=Integral (P, a, B, N); printf ("Get value:%f\n", area); printf ("Test Exp,input A, B:"); scanf ("%f,%f", &a, &b); P=F_exp; Area=Integral (P, a, B, N); printf ("Get value:%f\n", area); System ("Pause"); return 0;}



C Language Review---Rectangular method for determining integral function

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.