Question: Module 10 Activities: More on Functions 1. Write a function of two arguments, x and y, that returns the value x2 + y2 + 1


Module 10 Activities: More on Functions 1. Write a function of two arguments, x and y, that returns the value x2 + y2 + 1 2. Write a fruitless function of two variables, n, and m that prints out n plus signs followed by m minus signs. 3. The integral of a function between a and b is the area between the graph of the function, the x-xis, and the lines through a and b. It is denoted as f(x)dx and a great part of Analysis is devoted to studying integrals. You do not really need to understand integrals, just implement some formula about them. There are many ways to approximate the integral of a function numerically 1. Three-point Trapezoid Rule a+ b f(x)dxf(a)2f+f(b) Create a function of a, b, and f that calculates the approximate integral using this formula. Then calculate the approximations for the following integrals and compare with their exact values. (Hint: define a function square) b) | exp(x)dx = 1.71828 (Hint import math and use math,exp) c) xdx 0.666667 (Hint: import math and use math-sqrt) Solution: We have a function of three arguments, a, b, and func: def simple_trap (a, b, func): To implement the formula we build the sum step by step. We then return the sum multiplied by (b - a)/4. The whole function is def simple_trap(a, ,unc): accu func (a) mid-(a+b)/2 accu += 2*func (mld) accufunc (b) return (b-a) *accu/4 To try it out, we import math and define the square function. We then call the function as print (simple_trap (0,1,sq) print (simple_trap (0,1,math.exp) print (simple_trap (0,1,math.sqrt)) 2. Four-point Trapezoid Rule: (b - a) Then try the formula out on the same integrals as before. 3. There is another way to calculate areas, using statistics
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
