MATLAB: Optimizaation problem in matlab

MATLABoptimization

Hi,
I have a function in matlab which is dependent on four variable ( x1,x2,x3 and x4). I want to optimize the function output in matlab. I have access to optimization toolbox but i dont know anything about it.
For my fucntion i cant write function model because output of the function is a complex calculation invoving 100 lines of code.Basically i want to optimize a function output, depending on the four variable within given limits.
Anybody knows how i can proceed ?

Best Answer

Hi Sukuchha,
it doesn't matter how complex your function is or how many lines it has. What you need to do is (conceptually) simple: write a function (with as many lines of code as you like) that has the following structure:
function y = myfun(x)
% x is the vector of unknowns, if you like you could write:
x1 = x(1);
x2 = x(2);
x3 = x(3);
x4 = x(4);
% now come the many lines to compute what ever you want to have
% optimized, i.e., some lengthy computations leading to some y
% where the final y you are looking for fulfills
% |y(xFinal)| = minimum of all x
y = ...;
Then think of bounds for x and call
x0 = [1;1;1;1]; % or some better starting point
lb = [1 2 3 4]; % the lower bounds for your parameters
ub = [5 6 7 8]; % the upper bounds for your parameters
xOpt = fmincon(@myfun, x0, [], [], [], [], lb, ub);
Titus