not obtaining a correct answer

Hello, for some reason this golden section method to find the optimization guess, isnt giving the correct answer, its only doing two iteration and I can't figure out why.
function [xopt] = Sanchez_Gold1(xlow,xhigh,err)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
f=@(x)-1.5*x^6-2*x^4+12*x
N=1000
for i=1:N
xl=xlow;
xu=xhigh;
d=.5*(sqrt(5)-1)*(xu-xl);
x1=xl+d;
x2=xu-d;
if f(xl)>f(x2)
x1=x2;
else
xu=x1;
end
j=i+1;
xx(j)=x1; jj=j-1;
Err=abs(xx(j) - xx(j-1)); if Err < err, break;end
end
FF=f(x1);
disp(['optimal x= ' num2str(x1)])

2 个评论

@Anthony Ming: please give us the exact input values that you are using.
Every iteration for i, you overwrite xl and xu with the initial xlow and xhigh.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by