xl = -2; %Lower limit
xu = 1; %Upper limit
es = 100;imax = 100;while (ea>=es) && (iter < imax) R = (sqrt(5)-1)/2; %Golden Ratio value
d = R*(xu - xl);x1 = xl + d; %Initial guesses to determine the bounds
x2 = xu - d;iter = 1;ea = es +1;f(x1) = f1;f(x2) = f2; if f(x1) > f(x2) xl = x2; x2 = x1; xopt = x1; fmax = f(x1); x1 = xl + d; f2 = f1; else xopt = x2; x1 = x2; d = R + d; x2 = xu - d; f(x1) = f(x2); end if xopt ~=0 ea = (1 - R)*abs(xint/xopt)*100; end if (ea>=es) && (iter < imax) xint = xu - xl; iter = iter + 1; else break endenddisp (['The maximum is located at ', num2str(xopt), ' and ', num2str(fopt)]);function [y] = f(x) y = -(x^4) - (2*x^3) - (8*x^2) - (5*x);end
MATLAB: What is wrong with line 13
unused variable
Best Answer