Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

from math import * def main(): def f1(x): return x - 2 * sin(x) def f2(x): return (x - 2.5) * (x - 7) *

image text in transcribedimage text in transcribed

from math import *

def main():

def f1(x):

return x - 2 * sin(x)

def f2(x):

return (x - 2.5) * (x - 7) * (x - 4) * (x + 3)

#for part a)

x0 = 1

x1 = 2

root, niter = Secant(f1, x0, x1)

print('the root is: {:.6f} after {:d} iterations'.format(root, niter))

root, niter = Secant(f2, x0, x1, xtol=0.001, maxiter=8)

print('the root is: {:.6f} after {:d} iterations'.format(root, niter))

root, niter = Secant(lambda x: (x - 1.523) * (x - 5), x0, x1, xtol=1e-4, maxiter=6)

print('the root is: {:.6f} after {:d} iterations'.format(root, niter))

root, niter = Secant(lambda x: x - 2 * sin(x), x0, x1, maxiter=2)

print('the root is: {:.6f} after {:d} iterations'.format(root, niter))

# for part b)

a = 1

b = 3

nsteps = 9

integral = Simpson(f1, a, b, nsteps)

print('the integral of function 1 is: {:.7f} '.format(integral))

a = 1

b = 3

nsteps = 27

integral = Simpson(f1, a, b, nsteps)

print('the integral of function 1 is: {:.7f} '.format(integral))

nsteps = 81

integral = Simpson(f1, a, b, nsteps)

print('the integral of function 1 is: {:.7f} '.format(integral))

nsteps = 82

integral = Simpson(f1, a, b, nsteps)

print('the integral of function 1 is: {:.7f} '.format(integral))

a = 0

b = 4

nsteps = 11

integral = Simpson(f2, a, b, nsteps)

print('the integral of function 2 is: {:.2f} '.format(integral))

# for part c)

MyA = [[4, -1, -1, 3],

[-2, 6, 1, 9],

[-1, 1, 7, -6]]

MyX = [0, 0, 0]

answer = GaussSeidel(MyA, MyX, maxiter=6)

print(" ", answer)

answer = GaussSeidel(MyA, MyX, maxiter=15, xtol = 1e-15)

print(answer)

#for part d)

#From MAE 3013 ... the 3 male mice problem

x = 2; n = 10; N = 100; M = 3

p = HyperGeometric(x,n,N,M)

print(" the HyperGeometric probablilty is {:.5f}".format(p))

p = Binomial(x,n,M/N)

print("the Binomial probablilty is {:.5f}".format(p))

main()

Write a single Python program named Homework 2.py, that fulfills all of the requirements given below You will upload that single python file to the BrightSpace Dropbo% labeled-Homework 2 Upload. The description below requires that you write five different functions. All ive functions and the function named main0 which is used to call and test the five fanctions, must be placed in that single Homework 2py Python file. There is a fle on Brightspace named Homework 2 main py. This file contains the main function required to test the five fumctions that vou will write. You must copy upload your program to the dropbos, it must inchude this exact mainO function with no changes to this main function. that code into your program When you You will test your finctions using the particular merical values and functions given in the main0 fanction below. When we grade your assignment, we will run your program with those given mumerical values, looking for corect answers. Then we will change the umerical values including changing the SIZES of the arrays) and look for correct answers for those modified values as well. We will only use numerical values, aray sizes and functions that make sense. We will not be testing your program to see how it handles bad data (except for the Simpson function). In this assignment you must use variables, loops, if statements, your own function definitions and function calls to write the required fanctions. For now. you may not use any of the powerfal functions available in python modules, with one exception: you may import functions from themath module and the copy module. See the MAE 3013 Equation Sheet for a reminder of The Secant Method for finding the solution (root or zero) or a nonlinear equation The Simpsons 1/3 rule for numerical integration The Gauss-Seidel method for solving a set of linear equations The Binomial and HyperGeometric functions from Probability and Statistics a) Write a function defined as: Purpose: use the Secant Method to find the root off cn(%). in the neighborhood of s0 and x1 def Secant fcn x0, xl, xtol-le-5, maxiter-10): fcn: the fiunction for which we want to find the root N0 and %1 : two x values in the neighborhood ofthe root xtol exit if the knevent-Spreen- Stol maxiter. exit if the number of iterations (new x vaues) equals this umber return value: the final estimate of the root (most recent new value) and the rumber of actual iterations

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions