Finding minimum of a function using dichotomous search

10 views (last 30 days)
Hi, so for my question i am trying to minmise the function f(x)= x^(4) – 14*x(3) + 60*x(2) -70*x using a dichotomous search method.
The interval is between [0,2] so i know that a and b are 0 and 2 respectively.
Delta has been set to 0.005.
Through working out, the optimal value of n = 6 so i have added this to the code.
I am a beginner and therefore i found a code online for the dichotomous search algorithm.
Any clue why i get: Error: File: dichotomous1.m Line: 1 Column: 14
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To
construct matrices, use brackets instead of parentheses.
I have added his notes just incase they help you.
Any extra help is greatful.
The code is:
function [F] optimal = dichotomous(F,a,b,n)
F= @(x) x(1)^4 - 14 * x(1)^3 + 60*x(1)^2 - 70*x(1);
a=0;
b=2;
n=6;
% Program : dichotomous.m
%
% Purpose : find the optimal point of a given single variable function
% within the given interval
%
% Author : Aamir Alaud Din
% Date : 26.09.2013
%
% Inputs : Four input arguments are required viz., function, two points
% belonging to the interval, and number of iterations
%
% Syntax : optimal = dichotomous(f,a,b,n)
% where f is an inline function
% e.g., f = inline('sin(x)','x');
% a and b are two points belonging to the interval
%
% Example: optimal = dichotomous(f,-3,5,20)
if (nargin < 3)
error('Less number of inputs');
optimal = 'Optimal can''t be found with too few inputs';
return;
elseif (nargin == 3)
n = 250;
disp('Iteration a b f(a) f(b)');
disp('========= ========= ========= ========= =========');
for ii = 1:n
mid = (a + b)/2;
epsilon = 0.005;
a1 = mid - epsilon;
b1 = mid + epsilon;
if (f(a1) < f(b1))
b = b1;
elseif (f(a1) > f(b1))
a = a1;
elseif (f(a1) == f(b1))
a = a1;
b = b1;
end
fprintf('%4d', ii);
fprintf('\t\t\t');
fprintf('%11.4f', a);
fprintf('\t\t');
fprintf('%11.4f', b);
fprintf('\t\t');
fprintf('%11.4f', f(a));
fprintf('\t\t');
fprintf('%11.4f', f(b));
fprintf('\n');
end
elseif (nargin == 4)
disp('Iteration a b f(a) f(b)');
disp('========= ========= ========= ========= =========');
for ii = 1:n
mid = (a + b)/2;
epsilon = 0.005;
a1 = mid - epsilon;
b1 = mid + epsilon;
if (f(a1) < f(b1))
b = b1;
elseif (f(a1) > f(b1))
a = a1;
elseif (f(a1) == f(b1))
a = a1;
b = b1;
end
fprintf('%4d', ii);
fprintf('\t\t\t');
fprintf('%11.4f', a);
fprintf('\t\t');
fprintf('%11.4f', b);
fprintf('\t\t');
fprintf('%11.4f', f(a));
fprintf('\t\t');
fprintf('%11.4f', f(b));
fprintf('\n');
end
elseif (nargin > 4)
error('Too many input arguments')
optimal = 'Optimal can''t be found with too many inputs';
return;
end
optimal = min(a,b);
  2 Comments
John D'Errico
John D'Errico on 29 Apr 2019
Funny, how for code you found on the internet, it is actually really poor code. It looks like it tries to be good. The author was trying hard. But it really is crap code in the end. Sorry. But it is.
Do you really need to use this code? Is there a valid reason why?
Is there any valid reason why you would not just use either of fminbnd or fminsearch, or one of the tools from the optimization toolbox, or a tool from the global optimization toolbox? Note that both fminbnd and fminsearch both come in MATLAB already, so they cost you nothing.
Jan
Jan on 29 Apr 2019
I agree with John. The code is not smart. E.g.: It stops with an error if nargin>4, but after stopping the code tries to create the variable optimal and to return - but the code has been stopped already. Defining epsilon in each iteration is a waste of time. After comparing f(a1) < f(b1) and f(a1) > f(b1), it is not needed to check for f(a1) == f(b1).
The sections for 3 and 4 inputs differ by "n=250" only. Then it would be better just to define n and use the same code.

Sign in to comment.

Answers (2)

Jan
Jan on 29 Apr 2019
Edited: Jan on 29 Apr 2019
The first line is bad already:
function [F] optimal = dichotomous(F,a,b,n)
I guess, this is meant:
function optimal = dichotomous(F,a,b,n)
Then do not define the function and the limits inside the function, but in a separate function:
function yourFcn
F = @(x) x(1)^4 - 14 * x(1)^3 + 60*x(1)^2 - 70*x(1);
a=0;
b=2;
n=6;
opt = dichotomous(F,a,b,n)
end
  2 Comments
Adam Stevenson
Adam Stevenson on 29 Apr 2019
I'm still unsure about this, is the new function within a separate file?
If so how do i link the files because currently it says that there aren't any inputs.
Otherwise if the new function is in the same file, i get an error saying that there is an incorrect 'end' term.
Aswell as this, it says 'optimal = min(a,b);' is not within a function.
Jan
Jan on 29 Apr 2019
You can write this function to a new file, e.g. called "yourFcn.m". Or you can copy both functions to the same M-file. Then remember, that the first function is the "main" function, which is called, if you write yourFcn in the command window.
"it says that there aren't any inputs" - do not post parts of error messages, but exact copies of the complete message. The partial messages do not reveal the important details.
If you write several functions to one M-file, and one of the functions is closed with an end, all functions in the file must be closed with an end.
I suggest to read the "Getting Started" chapters of the documentation and to learn from Matlab's free Onramp Tutorial (ask an internet search engine for the link). The forum is not the perfect location to learn the basics.

Sign in to comment.


haftom
haftom on 11 Dec 2024
good work

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!