Question: Need help solving function thanks def minGolden ( func , xl , xu , tol = 1 e - 4 , maxit = 5 0

Need help solving function thanks def minGolden(func, xl, xu, tol =1e-4, maxit =50,*args): #minGolden function
"""Finds the minimum of a function using golden section search method
minGolden(func, xl, xu, tol =1e-4, maxit =50,*args)
Finds the minimum of a one-dimensional function within an interval
using golden section search method
Input:
- func: an anonymous function for f(x)
- xl, xu: lower and upper limits of the interval
- tol : error tolerance (%)(default =0.0001%)
- maxit: maximum number of iterations (default =50)
-*args: any extra arguments to func (optional)
Output:
- xmin: the location of the minimum
- fx: the minimum value of function
- err: relative approximate error (%)
- iter: number of iterations
"""
small =1e-20 # a small number
phi =(1+5**0.5)/2; #golden ratio
iter =0 # initial value of iteration count
err =1000 # initial value of relative approximate error (%)
d =(phi -1)*(xu - xl)
x1= xl + d
x2= xu - d
f1= func(x1,*args) #func value at x1
f2= func(x2,*args) #func value at x2
# |------ d ----->|
# arrangement of points: xl ..... x2..... x1..... xu
# |------ d ------>|
while err > tol and iter maxit: # while err is greater than the tolerance (tol)
# and iter maxit continue the loop
iter = iter +1 # increment iter
dx = xu - xl
if f1 f2: # x1 is the new estimate of min -> xmin=x1, discard [xl,x2]
xmin = x1
xl = x2
x2= x1
f2= f1
d =(phi -1)*(xu - xl)
x1= xl + d
f1= func(x1,*args)
else: # f2>= f1: x2 is the new estimate of min -> xmin=x2, discard [x2,xu]
xmin = x2
xu = x1
x1= x2
f1= f2
d =(phi -1)*(xu - xl)
x2= xu - d
f2= func(x2,*args)
err =(2- phi)* abs(dx /(xmin + small))*100 # relative approximate error (%)
# (a small number is added to the
# denominator to avoid /0 in case xm=0)
fmin = func(xmin,*args)
if iter == maxit: # show a warning if the function is terminated due to iter=maxit
print('Warning: minGolden function is terminated because iter=maxit;')
print(' error tolerance stopping criterion may not be satisfied')
return xmin, fmin, err, iter #returns xmin, fmin, err, iter
 Need help solving function thanks def minGolden(func, xl, xu, tol =1e-4,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!