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 $\sin^2(x)$. You can change the function in the code to integrate any function you want. Increasing the number of midpoints increases accuracy.

importmathdeffunction(x):return(math.sin(x))**2n=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)/ndefmean(a,b):meanValue=(a+b)/2returnmeanValuexValue=[a]heightValue=[]foriinrange(0,n):xValue.append(a+deltaX)deltaX=deltaX+(b-a)/nforiinxValue:heightValue.append(function(mean(i,i+(b-a)/n)))delheightValue[-1]the_sum=sum(heightValue)definite_integral=the_sum*((b-a)/n)print('Definite integral evaluated by midpoint rule: '+str(definite_integral))

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.

Markdown

Appears as

*italics* or _italics_

italics

**bold** or __bold__

bold

- bulleted - list

bulleted

list

1. numbered 2. list

numbered

list

Note: you must add a full line of space before and after lists for them to show up correctly

@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/3$ and $3/8$ rule.
Or can you give me some article where these rules are nicely illustrated.
Thanks in advance

@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.

importmathdeffunction(x):return(math.sin(x))**2n=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)/ndefmean(a,b):meanValue=(a+b)/2returnmeanValuexValue=[a]heightValue=[]foriinrange(0,n):xValue.append(a+deltaX)deltaX=deltaX+(b-a)/nforiinxValue:heightValue.append(function(mean(i,i+(b-a)/n)))delheightValue[-1]the_sum=sum(heightValue)definite_integral=the_sum*((b-a)/n)print('Definite integral evaluated by midpoint rule: '+str(definite_integral))

importmathdeffunction(x):return(math.sin(x))**2n=2500b=3a=2deltaX=(b-a)/ndefmean(a,b):meanValue=(a+b)/2returnmeanValuexValue=[a]heightValue=[]foriinrange(0,n):xValue.append(a+deltaX)deltaX=deltaX+(b-a)/nforiinxValue:heightValue.append(function(mean(i,i+(b-a)/n)))delheightValue[-1]the_sum=sum(heightValue)definite_integral=the_sum*((b-a)/n)print('Definite integral evaluated by midpoint rule: '+str(definite_integral))

@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.

@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

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:

`*italics*`

or`_italics_`

italics`**bold**`

or`__bold__`

boldNote: you must add a full line of space before and after lists for them to show up correctlyparagraph 1

paragraph 2

`[example link](https://brilliant.org)`

`> This is a quote`

Remember to wrap math in`\(`

...`\)`

or`\[`

...`\]`

to ensure proper formatting.`2 \times 3`

`2^{34}`

`a_{i-1}`

`\frac{2}{3}`

`\sqrt{2}`

`\sum_{i=1}^3`

`\sin \theta`

`\boxed{123}`

## Comments

Sort by:

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

Thanks in advance

Log in to reply

Got it man. I'll do it right now

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/3$ and $3/8$ rule.

Or can you give me some article where these rules are nicely illustrated.

Thanks in advance

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.

Log in to reply

@Krishna Karthik bro i am recently evaluating $\int_{2}^{3} \sin^{2} x dx$

here is my code but not getting answer

Log in to reply

Oh, nevermind. I'll have a look.

Log in to reply

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

Log in to reply

Log in to reply

Log in to reply

Log in to reply

@Krishna Karthik it is showing this

Log in to reply

Log in to reply

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

Log in to reply

Log in to reply

Log in to reply

i want the code of that particular question

thanks in advance

Log in to reply

Log in to reply

Log in to reply

Log in to reply

Log in to reply

Log in to reply

Log in to reply

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

Log in to reply

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

Log in to reply

@Krishna Karthik guide me to evaluate this using explicit euler

Log in to reply

@Krishna Karthik bro please solve the above double integral using midpoint rule.

i am waiting

thanks in advance.

Log in to reply

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

Log in to reply