0

I am trying to implement following in MATLAB,

n1 = 6; 
n2 = 1;
n3=0.1;
global ps

ps=zeros(3,15);
[R,t,d]=model(n1,n2,n3);
ps=R;
[x, fval] = fmincon(@Obj,[1/3,1/3, 1/3],[],[],[],[],[],[],@cons);


function f = Obj(x)
f = x(1)^2+x(2)^3+x(3)^4;

function [c, ceq] = cons(x)
c=[];
ceq(1) = sum(ps(1,:))*x(1)+sum(ps(2,:))*x(2)+sum(ps(3,:))*x(3) - (sum(d(1,:)));
ceq(2) = sum(x) - 4;

I'm getting following error, what is wrong here?

variable or function 'ps' undefined
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122

2 Answers2

3

You don't need to and definitelty should not use global variables for this. It is a very bad habit and inefficient to boot. Any time you think about using global you should ask yourself if there is another way and search for it. It is only in the very very rare case that globals are needed/helpful (usually in large codebases such as toolboxes).

In your case, you should pass your ps variable in as a parameter by creating an anonymous function. First define your cons function like this so that it takes in a parameter argument:

function [c, ceq] = cons(x,ps)

Then create the anonymous function with one input (x) and one captured parameter (the variable ps, which needs to be defined before this):

[x, fval] = fmincon(@Obj,[1/3,1/3, 1/3],[],[],[],[],[],[],@(x)cons(x,ps));

Alternatively you can save a handle to the anonymous function and pass that in:

cfun = @(x)cons(x,ps);
[x, fval] = fmincon(@Obj,[1/3,1/3, 1/3],[],[],[],[],[],[],cfun);

Here's a blog post from The MathWorks with other bad habits.

Community
  • 1
  • 1
horchler
  • 18,384
  • 4
  • 37
  • 73
  • I completely agree, but still the accepted answer is the one that answers the question as posed. – A. Donda Dec 21 '13 at 17:52
  • @A.Donda: And mine doesn't? The question mentioned nothing about globals and even if it did, it would still be a bad idea that is easily corrected. – horchler Dec 21 '13 at 17:53
  • @horchler In the original version of the question, there was a `global ps` among the first lines of code, so I just wanted to give a hint on how to access that variable from inside another function. Without a doubt, your answer is the one that is cleaner, more enlightening and more complete, and should be the accepted answer. – blackbird Dec 21 '13 at 20:31
  • @horchler, as I said, I agree with your advice. But the question is "I'm getting following error, what is wrong here?", and the answer is that the `global` statement in the function is missing. In that sense, no, you don't answer the question – no offence intended. And I only made that comment because your original text seemed to suggest that @blackbird's answer was accepted undeservedly. That's not the case imho. – A. Donda Dec 21 '13 at 22:47
  • 2
    @blackbird: Thanks. That clarifies things, though I'm not sure why the OP deleted that line. – horchler Dec 21 '13 at 23:52
1

I cannot run your lines of code, but I think you have to put

global ps

in your function cons to inform Matlab that you are referring to the global ps. More information on global variables can be found here: http://www.mathworks.de/de/help/matlab/ref/global.html

EDIT: For a cleaner design, you should take into account horchler's advice: https://stackoverflow.com/a/20721808/3060323

Community
  • 1
  • 1
blackbird
  • 957
  • 12
  • 18
  • I've added global ps to my main script. Do you mean I need to add it to constraint file as well? – user3125389 Dec 21 '13 at 15:28
  • @user3125389, yes, the `global` command has to be given in each workspace in which you'd like to access the global variable. – A. Donda Dec 21 '13 at 15:57