Midpoint Rule Numerical integration using python

I created a program to evaluate definite integrals using the Midpoint Rule.

Firstly, the midpoint rule states that:

Since evaluating definite integrals using the midpoint rule is a repetitious method, it would be best if a program would do it. Increasing the value of n would increase the integral's accuracy, but it would be more difficult to do by hand.

Here is my python code for the definite integral of sin2(x)\sin^2(x). You can change the function in the code to integrate any function you want. Increasing the number of midpoints increases accuracy.

Here's the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import math

def function(x):
  return (math.sin(x))**2




n=int(input('How many midpoints would you like to have? How much accuracy would you like to have? Enter an integer value from 1 to 50000, 50000 being the most precise (you could be more accurate, but what is the point because 50000 gives you more than 10 decimal places accuracy) and 1 being the least precise: '))

b=float(input('what top value of integration would you like to have? Enter: '))

a=float(input('what bottom value of integration would you like to have?Enter: '))


deltaX=(b-a)/n

def mean(a,b):
  meanValue=(a+b)/2
  return meanValue

xValue=[a]
heightValue=[]

for i in range(0,n):
  xValue.append(a+deltaX)
  deltaX=deltaX+(b-a)/n

for i in xValue:
  heightValue.append(function(mean(i,i+(b-a)/n)))
del heightValue[-1]


the_sum=sum(heightValue)
definite_integral=the_sum*((b-a)/n)

print('Definite integral evaluated by midpoint rule: '+str(definite_integral))

Note by Krishna Karthik
1 year, 5 months ago

No vote yet
1 vote

  Easy Math Editor

This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.

When posting on Brilliant:

  • Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
  • Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
  • Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.
  • Stay on topic — we're all here to learn more about math and science, not to hear about your favorite get-rich-quick scheme or current world events.

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold

- bulleted
- list

  • bulleted
  • list

1. numbered
2. list

  1. numbered
  2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1

paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
    # 4 spaces, and now they show
    # up as a code block.

    print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in \( ... \) or \[ ... \] to ensure proper formatting.
2 \times 3 2×3 2 \times 3
2^{34} 234 2^{34}
a_{i-1} ai1 a_{i-1}
\frac{2}{3} 23 \frac{2}{3}
\sqrt{2} 2 \sqrt{2}
\sum_{i=1}^3 i=13 \sum_{i=1}^3
\sin \theta sinθ \sin \theta
\boxed{123} 123 \boxed{123}

Comments

Sort by:

Top Newest

@Krishna Karthik Thanks for this note. Bro can you please share a photo of the code of definite integral of sin2x\sin^{2} x.
Thanks in advance

Neeraj Anand Badgujar - 1 week, 4 days ago

Log in to reply

Got it man. I'll do it right now

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Krishna Karthik Bro can we solve differential equations in python.
Like this way you have posted the midpoint rule can you also post the Simpson 1/31/3 and 3/83/8 rule.
Or can you give me some article where these rules are nicely illustrated.
Thanks in advance

Neeraj Anand Badgujar - 1 week, 4 days ago

Log in to reply

We can definitely solve ODEs in Python. I'll post and article as well as some stuff clarifying the theory behind it.

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Krishna Karthik bro i am recently evaluating 23sin2xdx\int_{2}^{3} \sin^{2} x dx
here is my code but not getting answer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
print"import math

def functions(x):
    return (math.sin(x))**2


n=int(input(50000))

b=float(input(3))

a=float(input(2))

deltaX=1/50000

def mean(a,b):
    meanValue=(a+b)/2
    return meanValue

xValue=[a]
heightvalue=[]

for i in range(0,n):
    xValue.append(a+deltaX)
    deltaX=deltaX+(b-a)/n

    for i in  xValue:
 heightValue.append(function(mean(i,i+(b-a)/n))
    del heightValue[-1]

    the_sum=sum(heightValue)
    definite_integral=the_sum*((b-a)/n)

    print "

Neeraj Anand Badgujar - 1 week, 4 days ago

Log in to reply

Oh, nevermind. I'll have a look.

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Krishna Karthik at last what we have to write after print in mid point theorem to compute the answer bro.

Neeraj Anand Badgujar - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar Print the value of the definite integral stored by the variable "definite_integral".

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar I got 0.38 approximately. The values converge well.

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar And you can't put the input into the "input section". That's not how the code works. Run the code, and it will ask you to input.

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Krishna Karthik @Krishna Karthik it is showing this

Neeraj Anand Badgujar - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar Ok, you need to indent the "heightValue.append" line. It doesn't match the indentation for the forloop.

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Krishna Karthik @Krishna Karthik can you please share your code for this integral .please bro

Neeraj Anand Badgujar - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar Sure. No problem ;)

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar Ok, ill show:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import math

def function(x):
  return (math.sin(x))**2




n=int(input('How many midpoints would you like to have? How much accuracy would you like to have? Enter an integer value from 1 to 50000, 50000 being the most precise (you could be more accurate, but what is the point because 50000 gives you more than 10 decimal places accuracy) and 1 being the least precise: '))

b=float(input('what top value of integration would you like to have? Enter: '))

a=float(input('what bottom value of integration would you like to have?Enter: '))


deltaX=(b-a)/n

def mean(a,b):
  meanValue=(a+b)/2
  return meanValue

xValue=[a]
heightValue=[]

for i in range(0,n):
  xValue.append(a+deltaX)
  deltaX=deltaX+(b-a)/n

for i in xValue:
  heightValue.append(function(mean(i,i+(b-a)/n)))
del heightValue[-1]


the_sum=sum(heightValue)
definite_integral=the_sum*((b-a)/n)

print('Definite integral evaluated by midpoint rule: '+str(definite_integral))

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Krishna Karthik @Krishna Karthik bro it is the same as above .
i want the code of that particular question
thanks in advance

Neeraj Anand Badgujar - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar Ok, I'll modify it and show you.

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar The one above is fine, but the problem is in your indentation levels.

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Krishna Karthik Enter the n, a , b values like this and it'll work:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import math

def function(x):
  return (math.sin(x))**2




n=2500

b=3

a=2


deltaX=(b-a)/n

def mean(a,b):
  meanValue=(a+b)/2
  return meanValue

xValue=[a]
heightValue=[]

for i in range(0,n):
  xValue.append(a+deltaX)
  deltaX=deltaX+(b-a)/n

for i in xValue:
  heightValue.append(function(mean(i,i+(b-a)/n)))
del heightValue[-1]


the_sum=sum(heightValue)
definite_integral=the_sum*((b-a)/n)

print('Definite integral evaluated by midpoint rule: '+str(definite_integral))

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar Your indentation is incorrect, that can alter the code completely.

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar Copy the code exactly as it is, with the exact same spacing.

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar Copy and paste the code above without modifying it, and just run it. It'll get you to enter the values into the console. I'll share a photo with you.

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Neeraj Anand Badgujar See, you have to run the code, and it will get you to enter the values. Don't modify the code yourself unless you are willing to change the function you want to integrate. If you run the code for the integral above, it will come with a menu like this:

How many midpoints would you like to have? How much accuracy would you like to have? Enter an integer value from 1 to 50000, 50000 being the most precise (you could be more accurate, but what is the point because 50000 gives you more than 10 decimal places accuracy) and 1 being the least precise: 10000 what top value of integration would you like to have? Enter: 3 what bottom value of integration would you like to have?Enter: 2 Definite integral evaluated by midpoint rule: 0.3806532505238656

Krishna Karthik - 1 week, 4 days ago

Log in to reply

I've posted a picture of the code in a note above.

Krishna Karthik - 1 week, 4 days ago

Log in to reply

@Krishna Karthik guide me to evaluate this using explicit euler

Neeraj Anand Badgujar - 1 week, 4 days ago

Log in to reply

@Krishna Karthik bro please solve the above double integral using midpoint rule.
i am waiting
thanks in advance.

Neeraj Anand Badgujar - 1 week, 3 days ago

Log in to reply

I have posted it. Let me know if I can be of further help.

Krishna Karthik - 1 week, 2 days ago

Log in to reply

×

Problem Loading...

Note Loading...

Set Loading...