Thread Subject: Optimization problem in MATLAB

Subject: Optimization problem in MATLAB

From: Subrat Swain

Date: 13 Oct, 2008 21:00:19

Message: 1 of 3

Hi all,
I am pretty new to this group and I am a beginner in MATLAB.I am facing a problem while solving optimization equations using MATLAB. I am having an Excel file containing the objective function and the constraint equations and their values. After importing the Excel file in MATLAB I need to create .M files for the objective function and constraint function from the specified equations in the imported Excel file.
So while writing the .M file for the objective function the problem is as to what function needs to be called to access the equations from the Excel file. Right now the code that I have written for creating objective function and constraint function is written below.
               OBJECTIVE Function .M file
function f = objfun(x)
f = @(textdata)textdata{1,6};
// textdata is the imported excel file containing equation for objective function.

              Constraint Function .M file
function [c, ceq] = confun(x)
% Nonlinear inequality constraints
f1 = @(textdata)textdata{7,6}
f2 = @(textdata)textdata{8,6}
f3 = @(textdata)textdata{9,6}
c = [f1;f2;f3]; --- Warning (non-scalar function handle not
                             supported)
% Nonlinear equality constraints
ceq = [];

I am getting error message as Warnings written above.
Can anyone let me know as to how to access the equations from the excel file and write in .M file.

Thanks and Regards,
Subrat Kumar Swain.

Subject: Optimization problem in MATLAB

From: Bruno Luong

Date: 13 Oct, 2008 21:44:02

Message: 2 of 3

"Subrat Swain" <swain.subrat01@gmail.com> wrote in message <gd0ct3$ptb$1@fred.mathworks.com>...

> OBJECTIVE Function .M file
> function f = objfun(x)
> f = @(textdata)textdata{1,6};
> // textdata is the imported excel file containing equation for objective function.

You cannot do that. Under Matlab textdata{1,6} is interpreted as a *string* (char array) and not a command or function. You have to enter the equation.

Example: if textdata{1,6} is '3*x + 5'

then you have to type

f = @(x) (3*x + 5);

Same for the constraints.

Bruno

PS: there is an "eval" function that can accomplish the conversion from string to function, but you are better to stay away from it for now.

Subject: Optimization problem in MATLAB

From: Paul Kerr-Delworth

Date: 15 Oct, 2008 09:18:08

Message: 3 of 3

Hi Subrat,

If you only have a few optimization problems defined in Excel, I agree with
Bruno that you should retype the objectives and constraints in MATLAB as he
suggests.

However, if you have many existing problems defined in Excel you may want an
automated route. One way to do this is as follows:

- Import the excel file as you have done already
- Use the symbolic math toolbox to create function handles from the imported
strings
- Pass these functions to fmincon

Note that you need the latest release of MATLAB (R2008b) and the Symbolic
Math Toolbox to do this.

A simple example of this approach is shown below:

function x = optimSym
% Simulate read from excel file
textread{1, 1} = 'sin(x)';
textread{1, 2} = 'x^2 - 1';
textread{1, 3} = '-tan(x) -1';
nCon = size(textread, 2) - 1;

% Convert the strings to matlab functions via symbolic toolbox
f = matlabFunction(sym(textread{1, 1}));
fCon = cell(1, nCon);
fCon{1} = matlabFunction(sym(textread{1, 2}));
fCon{2} = matlabFunction(sym(textread{1, 3}));

% Call fmincon
opts = optimset('fmincon');
opts = optimset(opts, 'Algorithm', 'active-set');
x = fmincon(f, 0.5, [], [], [], [], -pi, pi, @(x)evalCon(x, fCon), opts);

function [c, ceq] = evalCon(x, fCon)

nCon = length(fCon);
c = zeros(nCon, 1);
for i = 1:nCon
    c(i) = fCon{i}(x);
end
ceq = [];

Hope this helps.

Best regards,

Paul


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:gd0ff2$isb$1@fred.mathworks.com...
> "Subrat Swain" <swain.subrat01@gmail.com> wrote in message
> <gd0ct3$ptb$1@fred.mathworks.com>...
>
>> OBJECTIVE Function .M file
>> function f = objfun(x)
>> f = @(textdata)textdata{1,6};
>> // textdata is the imported excel file containing equation for objective
>> function.
>
> You cannot do that. Under Matlab textdata{1,6} is interpreted as a
> *string* (char array) and not a command or function. You have to enter the
> equation.
>
> Example: if textdata{1,6} is '3*x + 5'
>
> then you have to type
>
> f = @(x) (3*x + 5);
>
> Same for the constraints.
>
> Bruno
>
> PS: there is an "eval" function that can accomplish the conversion from
> string to function, but you are better to stay away from it for now.
>

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
optimization co... Subrat Swain 13 Oct, 2008 17:00:22
rssFeed for this Thread

Contact us at files@mathworks.com