From: Philip M on
I want to minimize funrr with respect to the constraint nonlcon. I always get the following error message
??? Error using ==> nonlcon
Too many output arguments.

Error in ==> @(X)nonlcon(X)
Actually I don't know whats wrong. I used "Technical Solutions
How do I pass additional parameters to the constraint and objective functions in the Optimization Toolbox functions?" to set up my constraint. Thanks for your help!
----------------------------------------------------
function f = funrr(X)

mu=X(1);
a=X(2);
b=X(3);

....
----------------------------------------------------
x0 = [-0.55, 0.6, 0.7];

parameter=patternsearch(@funrr,x0,[],[],[],[],[-10 0 0],[10 1 10],@(X)nonlcon(X));

-----------------------------------------------------
function f = nonlcon(X)
mu=X(1);
a=X(2);
b=X(3);
varomikron=0.75;
p=0.0174;
c= -2.1102;
sigma=sqrt(b^2+varomikron);
rho=((a*b)/sigma);

expectedvalue=@(x) normcdf(mu+rho*sigma*x)/sqrt(1+sigma^2*(1-rho^2))*normpdf(x);

f = (1/p)*quad(expectedvalue,-999999,c)-0.4;
From: Alan Weiss on
On 7/26/2010 6:43 PM, Philip M wrote:
> I want to minimize funrr with respect to the constraint nonlcon. I
> always get the following error message
> ??? Error using ==> nonlcon
> Too many output arguments.
>
> Error in ==> @(X)nonlcon(X)
> Actually I don't know whats wrong. I used "Technical Solutions
> How do I pass additional parameters to the constraint and objective
> functions in the Optimization Toolbox functions?" to set up my
> constraint. Thanks for your help!
(SNIP)
> -----------------------------------------------------
> function f = nonlcon(X)
> mu=X(1);
> a=X(2);
> b=X(3);
> varomikron=0.75;
> p=0.0174;
> c= -2.1102;
> sigma=sqrt(b^2+varomikron);
> rho=((a*b)/sigma);
>
> expectedvalue=@(x)
> normcdf(mu+rho*sigma*x)/sqrt(1+sigma^2*(1-rho^2))*normpdf(x);
>
> f = (1/p)*quad(expectedvalue,-999999,c)-0.4;

According to the documentation for quad:
"The function y = fun(x) should accept a vector argument x and return a
vector result y, the integrand evaluated at each element of x."

In other words, you need to put in vectorized operations:
expectedvalue=@(x)
normcdf(mu+rho.*sigma.*x)./sqrt(1+sigma.^2.*(1-rho.^2)).*normpdf(x);

Alan Weiss
MATLAB mathematical toolbox documentation