Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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
image text in transcribed

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions

Question

1. Describe the power of nonverbal communication

Answered: 1 week ago