Expected duration: less than 1 week I need a Python Programmer to program a Lab for school for me. It has a criteria that has to be followed and has to be correct and sent in for a grade.
Below is the Criteria: Please look over and follow.
Numeric Integration! There is a good chance you won't finish this lab in two hours; but math isn't supposed to be the sticking point; watch the lecture video first!
In class we looked at a numeric integration method that summed the area of either rectangles or trapezoids (rectangles topped with triangles).
We also wrote a simple python program which graphed a function using the textbook's graphics library.
For this lab I want you to modify that program to draws rectangles and computes the integral of the sine function. Call your program sinarea.py
In python you can get the value of sine(x) by import math and then calling math.sin(x) (See the text chapter 3 to see an example of importing math functiions.) Note that the value of x and that returned are in radians.
To start, copy my program (above), rename it sinarea.py and modify it to plot only the sine function. (You'll also need a copy of the graphics.py code in your working directory.)
Change the header to add your information indicating you modified my code.
When you look at the (four) math functions I coded, you'll see a disconnect between the comments (that talk about N/100) and the actual code (that uses 130 instead.) That's often a problem - when I changed the code in class, I failed to update my comment to match. Bad example!
You'll want to delete my four math functions and replace them with a wrapper for the math.sin function. The goal of your wrapper is to return a value that fits nicely in our graphics window that expects to plot values between 0 and 1.
The nice thing about the sin function is that over certain ranges it naturally returns values between 0 and 1 (for example, when X is between 0 and math.pi):
The math.sin function expects its actual parameter to be in radians. You might know that math.pi radians = 180 degrees, or 1 radian = 180 / math.pi degrees - but the important thing is that the X axis on the above graph has radians as its unit - so as long as we plotour function between 0 and pi we'll get a curve with a Y value ranging from 0 to 1. To make things simple, for this lab, let's assume endpoints (lower, upper)are always in the range (0, pi). This means your wrapper function merely needs to call math.sin:
def sine(n): return math.sin(n) After you write the new sin wrapper function, you should stop and test your program.
Unfortunately, the range function I used in my for loops doesn't allow floating point numbers, i.e.
for lc in range(0,math.pi,0.1):
generates
TypeError: 'float' object cannot be interpreted as an integer.
This works, but the X value is too compressed - the formula 12+lc*5 needs to be changed to spread the values (0, pi) over the range (12, 510). Adjust your code to do that.
Next, rather than drawing circles, change your code to plot rectangles. Examples using our graphics.py librarys to plot rectangles can be found in our textbook. They'll be easier to see if you don't fill them.
Test your program. Working, it will draw the same number of rectangles every time.
Next, modify your program to take the number of rectangles as input
You'll need to calculate your width of each rectangle (the step) by:
(upper - lower) / number of rectangles
And now your should loop between lower and upper with your calculated step, calling math.sin(lc) and using that value to draw each rectangle.
Finally, add code to calculate (sum) the area of all the rectangles within your loop. Inside the loop, each rectangle you draw has the area of its math.sin(lc) value times its width (the step). Print out this result. As you increase the number of rectangles, the results will be more accurate.
You can check your answer by comparing it with the definite integral:
In other words, if you use lower = 0 and upper = 3.14159 and the sin function you should get about 2.0.
Advanced AR Headset Development Category: Android, Game Development, IPhone, Matlab And Mathematica, Mobile App Development Budget: ₹500000 - ₹1000000 INR