Answered step by step
Verified Expert Solution
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 minGoldenfunc xl xu tol e maxit args: #minGolden function
Finds the minimum of a function using golden section search method
minGoldenfunc xl xu tol e maxit args
Finds the minimum of a onedimensional function within an interval
using golden section search method
Input:
func: an anonymous function for fx
xl xu: lower and upper limits of the interval
tol : error tolerance default
maxit: maximum number of iterations default
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 e # a small number
phi ; #golden ratio
iter # initial value of iteration count
err # initial value of relative approximate error
d phi xu xl
x xl d
x xu d
f funcxargs #func value at x
f funcxargs #func value at x
# d
# arrangement of points: xl x x xu
# d
while err tol and iter maxit: # while err is greater than the tolerance tol
# and iter maxit continue the loop
iter iter # increment iter
dx xu xl
if f f: # x is the new estimate of min xminx discard xlx
xmin x
xl x
x x
f f
d phi xu xl
x xl d
f funcxargs
else: # f f: x is the new estimate of min xminx discard xxu
xmin x
xu x
x x
f f
d phi xu xl
x xu d
f funcxargs
err phi absdx xmin small # relative approximate error
# a small number is added to the
# denominator to avoid in case xm
fmin funcxminargs
if iter maxit: # show a warning if the function is terminated due to itermaxit
printWarning: minGolden function is terminated because itermaxit;
print error tolerance stopping criterion may not be satisfied'
return xmin, fmin, err, iter #returns xmin, fmin, err, iter
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started