Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: solving a phrase with 4 unknown

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 20 Jun, 2008 09:06:02

Message: 1 of 24

hi,
let's image such a phrase with 4 unknown:

10 + (a-2) + (b-2.3) + (c-3) + (d-3.2)

how can i find my unknown (a,b,c,d) for which this phrase
will give the minimum?


 i'll be really grateful for any suggestion,

        kind regards


Subject: solving a phrase with 4 unknown

From: Jos

Date: 20 Jun, 2008 09:38:01

Message: 2 of 24

"misty m." <donotspam@smth.be> wrote in message
<g3frtq$d6h$1@fred.mathworks.com>...
> hi,
> let's image such a phrase with 4 unknown:
>
> 10 + (a-2) + (b-2.3) + (c-3) + (d-3.2)
>
> how can i find my unknown (a,b,c,d) for which this phrase
> will give the minimum?
>
>
> i'll be really grateful for any suggestion,
>
> kind regards
>
>

Are the unknowns bounded? Otherwise there is no minimum,
e.g. a = -9999999...

Jos

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 20 Jun, 2008 09:49:02

Message: 3 of 24

hmm,
i know that a/b/c/d > 0. and actually maybe i can present
the phrase in a different way:

10 + (a-a1) + (b-b1) + (c-c1) + (d-d1)

where a1,b1,c1,d1 are a,b,c,d from a previous solution.

 any suggestions?

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 20 Jun, 2008 09:55:04

Message: 4 of 24

ok, and actually i know that a/b shouldn't be bigger than
about 50 and that 'b' is bigger than 'a'

Subject: solving a phrase with 4 unknown

From: Steven Lord

Date: 20 Jun, 2008 14:26:31

Message: 5 of 24


"misty m." <donotspam@smth.be> wrote in message
news:g3fupo$5m9$1@fred.mathworks.com...
> ok, and actually i know that a/b shouldn't be bigger than
> about 50 and that 'b' is bigger than 'a'

Look at FMINCON. You have lower bounds and two linear constraints.

--
Steve Lord
slord@mathworks.com


Subject: solving a phrase with 4 unknown

From: Marcus M. Edvall

Date: 20 Jun, 2008 14:59:39

Message: 6 of 24

You seem to have a linear programming problem so FMINCON is not really
ideal.

If you are recursively solving problems it might be nice to use MINOS
with warm start (assuming your problem size is bigger than this).

Best wishes, Marcus
Tomlab Optimization Inc.
http://tomopt.com/tomlab/
http://tomdyn.com/

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 21 Jun, 2008 11:09:02

Message: 7 of 24

thank You for suggestions. i took a look on optimization
toolbox and decided to use fmincon.

i tried to run an example from the toolbox, just to see the
results, but it doesn't work.

objfun.m :
function f = objfun(x)
f = exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) +
1);

confun.m :

function [c, ceq] = confun(x)
% Nonlinear inequality constraints
c = [1.5 + x(1)*x(2) - x(1) - x(2);
     -x(1)*x(2) - 10];
% Nonlinear equality constraints
ceq = [];

x0 = [-1,1]; % Make a starting guess at the solution
options = optimset('Algorithm','active-set');
[x,fval] = ...
fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options)


when i try to run 'confun' i get:

??? Input argument "x" is undefined.

Error in ==> confun at 3
c = [1.5 + x(1)*x(2) - x(1) - x(2);

can You please suggest me what i miss?

 kind regards

Subject: solving a phrase with 4 unknown

From: John D'Errico

Date: 21 Jun, 2008 11:19:02

Message: 8 of 24

"misty m." <donotspam@smth.be> wrote in message
<g3inge$sp3$1@fred.mathworks.com>...
> thank You for suggestions. i took a look on optimization
> toolbox and decided to use fmincon.
>
> i tried to run an example from the toolbox, just to see the
> results, but it doesn't work.
>
> objfun.m :
> function f = objfun(x)
> f = exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) +
> 1);
>
> confun.m :
>
> function [c, ceq] = confun(x)
> % Nonlinear inequality constraints
> c = [1.5 + x(1)*x(2) - x(1) - x(2);
> -x(1)*x(2) - 10];
> % Nonlinear equality constraints
> ceq = [];
>
> x0 = [-1,1]; % Make a starting guess at the solution
> options = optimset('Algorithm','active-set');
> [x,fval] = ...
> fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options)
>
>
> when i try to run 'confun' i get:
>
> ??? Input argument "x" is undefined.
>
> Error in ==> confun at 3
> c = [1.5 + x(1)*x(2) - x(1) - x(2);
>
> can You please suggest me what i miss?
>
> kind regards


Subject: solving a phrase with 4 unknown

From: misty m.

Date: 21 Jun, 2008 11:23:01

Message: 9 of 24

sorry, i've just noticed that the part:

x0 = [-1,1]; % Make a starting guess at the solution
options = optimset('LargeScale','off');

[x,fval] = fmincon(@objfun,x0,[],[],[],[],[],
[],@confun,options)


should be in a different m-file

Subject: solving a phrase with 4 unknown

From: John D'Errico

Date: 21 Jun, 2008 11:23:01

Message: 10 of 24

"misty m." <donotspam@smth.be> wrote in message
<g3inge$sp3$1@fred.mathworks.com>...
> thank You for suggestions. i took a look on optimization
> toolbox and decided to use fmincon.
>
> i tried to run an example from the toolbox, just to see the
> results, but it doesn't work.
>
> objfun.m :
> function f = objfun(x)
> f = exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) +
> 1);
>
> confun.m :
>
> function [c, ceq] = confun(x)
> % Nonlinear inequality constraints
> c = [1.5 + x(1)*x(2) - x(1) - x(2);
> -x(1)*x(2) - 10];
> % Nonlinear equality constraints
> ceq = [];
>
> x0 = [-1,1]; % Make a starting guess at the solution
> options = optimset('Algorithm','active-set');
> [x,fval] = ...
> fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options)
>
>
> when i try to run 'confun' i get:
>
> ??? Input argument "x" is undefined.
>
> Error in ==> confun at 3
> c = [1.5 + x(1)*x(2) - x(1) - x(2);
>
> can You please suggest me what i miss?
>
> kind regards

You might want to spend some time reading
the tutorials, instead of just trying random
things and wondering why they did not work.

Did you just type

confun

at the command line? Or did you try to use
"run" on it?

John

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 21 Jun, 2008 11:51:01

Message: 11 of 24

i tried now to run it on my example, but now i got an error:

??? Undefined function or variable "functionIsScalar".

Error in ==> finitedifferences at 116
    if functionIsScalar

Error in ==> optim\private\nlconst at 280
          [gf,gnc,NEWLAMBDA,OLDLAMBDA,s]=finitedifferences
(XOUT,x,funfcn,confcn,lb,ub,f,nc, ...

Error in ==> fmincon at 512
   [X,FVAL,lambda,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...

Error in ==> test at 11
[x,fval] = fmincon(@objfun,x0,[],[],[],[],[],
[],@confun,options)


actually it does suggest me too much.

my function f is:

f = (repmat(x(1), [1 ii]) - variable(1:ii) + (repmat(x(2),
[1 ii]) - variable(1:ii) + (repmat(x(3), [1 ii]) - variable
(1:ii) + (repmat(x(4), [1 ii]) - variable(1:ii)

so it is a function of four unknowns, where 'ii' is defined
at the beginning

my nonlinear inequality constraints is:

c = [x(1) - 10;
     x(2) - 10;
     x(3) - 15;
     x(4) - 15];

    
and my x0 is:

x0 = [0,0, 0,0];

what may be wrong?

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 21 Jun, 2008 12:42:01

Message: 12 of 24

what i understand is 'f' should be scalar, yes? and my 'f'
is a vector for sure. can You suggest me, what can i do
with it?

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 21 Jun, 2008 13:25:03

Message: 13 of 24

ok, i've found where i had a mistake:)

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 21 Jun, 2008 14:01:03

Message: 14 of 24

hi, i still have a question.
i call to 'fmincon' function in a for loop. with every
iteration i must send to
'objfun' and 'confun' the number of actual iteration.

i tried to do it this way:

1) function f = objfun(x, iteration_nr)

f=...

2) function [c, ceq] = confun(x, iteration_nr)

...........

3) in test.m file:

for iteration_nr = 1:10

....

[x,fval] = fmincon(@objfun(iteration_nr),x0,[],[],[],[],[],
[],@confun(iteration_nr),options)


end


but matlab screams in a line with 'fmincon', so it looks
like @objfun(iteration_nr) is wrong.

can You suggest me how can i send iteratio_nr into objfun
and confun?

 kind regards,

         misty

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 21 Jun, 2008 14:08:01

Message: 15 of 24

sorry, objfun and confun should be ofcourse:

function [c, ceq, iteration_nr] = confun(x)

function f(iteration_nr) = objfun(x)


the rest of my question is the same. how can i send the
iteration_nr wtih fmincon calling?

Subject: solving a phrase with 4 unknown

From: Marcus M. Edvall

Date: 21 Jun, 2008 15:01:49

Message: 16 of 24

> my nonlinear inequality constraints is:
>
> c =3D [x(1) - 10; =A0 =A0
> =A0 =A0 =A0x(2) - 10;
> =A0 =A0 =A0x(3) - 15;
> =A0 =A0 =A0x(4) - 15];

These are not nonlinear constraints, neither are they linear
constraints, but simple bounds. They should go into lb and ub.

Best wishes, Marcus
Tomlab Optimization Inc.
http://tomopt.com/
http://tomdyn.com/

Subject: solving a phrase with 4 unknown

From: Marcus M. Edvall

Date: 21 Jun, 2008 15:05:21

Message: 17 of 24

Using FMINCON for a linear programming problem is incorrect so it's
pointless to answer these questions.

See help LINPROG for example.

Best wishes, Marcus
Tomlab Optimization Inc.
http://tomopt.com/
http://tomdyn.com/

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 21 Jun, 2008 15:46:02

Message: 18 of 24

but my question was how can i send an input into objfun and
confun. because everytime i call fmincon, an input must be
sent into these function.

my problem is nonlinear, i just showed a part of equation,
because it was not necessary to show the whole.
and fmincon solves my problem quite good.

and i'm sure that by:
  [x,fval] = fmincon(@objfun,x0,[],[],[],[],[],
[],@confun,options)

i can send somehow an input to objfun and confun, but as i
mentioned before, @objfun(variable) doesn't work.

can You suggest me how can i send it?

kind regards
  

Subject: solving a phrase with 4 unknown

From: Marcus M. Edvall

Date: 21 Jun, 2008 15:55:30

Message: 19 of 24

myinput = something;

@(x)objfun(x,myinput)

is your first input to fmincon.

Best wishes, Marcus
Tomlab Optimization Inc.
http://tomopt.com/
http://tomdyn.com/

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 21 Jun, 2008 16:12:01

Message: 20 of 24

sorry to bother You once again, but there still is
something wrong.
step by step:

1) function [c, ceq] = confun(x)
2) function f = objfun(x)
3) [x,fval] = fmincon(@(x)objfun(x,variable),x0,[],[],[],
[],[],[],@(x)confun(variable),options)
     

it still does not work. i modyfied to:

1) function [c, ceq] = confun(x, variable)
2) function f = objfun(x, variable)

still doesnąt work, matlab says that 'variable' is
undefined.
but it is defined in a file with calling to fmincon.

can You please correct me?

kind regards

Subject: solving a phrase with 4 unknown

From: Marcus M. Edvall

Date: 21 Jun, 2008 16:19:29

Message: 21 of 24

> 1) function [c, ceq] =3D confun(x)
> 2) function f =3D objfun(x)
> 3) =A0[x,fval] =3D fmincon(@(x)objfun(x,variable),x0,[],[],[],
> [],[],[],@(x)confun(variable),options)
>

@(x)confun(variable) should say @(x)confun(x,variable)

Best wishes, Marcus
Tomlab Optimization Inc.
http://tomopt.com/
http://tomdyn.com/

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 21 Jun, 2008 16:28:02

Message: 22 of 24

still no:(

once again:
1) [x,fval] = fmincon(@(x)objfun(x,variable),x0,[],[],[],
[],[],[],@(x)confun(x,variable),options)

2) function [c, ceq] = confun(x,variable)

3)function f = objfun(x,variable)

and matlab screams:

FMINCON cannot continue because user supplied nonlinear
constraint function
 failed with the following error:
Undefined function or variable "xot".

Error in ==> test at 31
   [x,fval] = fmincon(@(x)objfun(x,variable),x0,[],[],[],[],
[],[],@(x)confun(x,variable),options)


what is still wrong?

kind regards
  

Subject: solving a phrase with 4 unknown

From: Marcus M. Edvall

Date: 21 Jun, 2008 16:35:15

Message: 23 of 24

Check the following then:

which -all objfun
which -all confun

Then maybe you need to call your files something else, or define them
as nested.

Best wishes, Marcus
Tomlab Optimization Inc.
http://tomopt.com/
http://tomdyn.com/

Subject: solving a phrase with 4 unknown

From: misty m.

Date: 21 Jun, 2008 16:41:02

Message: 24 of 24

sorry, my mistake. the variable is sent. this error is
about smth else.
thank You very much for the patience!

 kind regards,
 misty

Tags for this Thread

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics