Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
patternsearch, error message: Objective function must be real and not NaN at the starting point.

Subject: patternsearch, error message: Objective function must be real and not NaN at the starting point.

From: Sara

Date: 10 May, 2014 18:43:12

Message: 1 of 4

Hi!

I am stuck with a problem with my code concerning optimization.
I have a maximization problem, which i easily convert to an minimization problem by setting the objective value f to -f.

My objective problem is an non-smooth function. I solved the optimization problem in the beginning with fmincon and got the error messsage "the userfcn must be type double".

Therefore I decided to use patternsearch.

The problem is now that i get the following error message: "Objective function must be real and not NaN at the starting point."
When I run the code for the objective function, for some values, I dont get any Nan values.
Anyone that know how I should proceed to get the optimization to work?

Codes:

clc;clear all;
L1=0.9144;L2=0.8382;L3=0.762;
b1=0.4318;b2=0.4318;b3=0.4318;
max=3.53-0.8;
number=max/b1;
area=35*3.53;

AA=[L1*b1*number L2*b2*number L3*b3*number];
bb=[area];
x0=[0;0;0];

[x, fval]=patternsearch(@myfun2,x0,AA,bb,[],[],[0;0;0],[area_flyg/(L1*b1*number);area/(L2*b2*number);area/(number*L3*b3)])%,[],options)
 -------------------
function [ f ] = myfun2( x )

rev= [20000 10000 5000];
b1=0.4318;
max=3.53-0.8;
number=max/b1;

C = 200;
slh1=0; slh2=0; slh3=0;
for i=0:x(1)
    ny=hpf(i*binopdf(i*number,C,0.05));
    slh1=slh1 + ny;
end
for i=0:x(2)
    ny=hpf(i*binopdf(i*number,C,0.12));
    slh2= slh2+ny;
end
for i=0:x(3)
    ny=hpf(i*binopdf(i*antal,C,0.86));
    slh3=slh3+ny;
end
f=-(slh1*20000+20000*x(1)*(hpf(binocdf(C,C,0.05))-hpf(binocdf(antal*(x(1)+1),C,0.05)))+slh2*5000+5000*x(2)*(hpf(binocdf(C,C,0.12))-hpf(binocdf(antal*(x(2)+1),C,0.12)))+slh3*1000+1000*x(3)*(hpf(binocdf(C,C,0.86))-hpf(binocdf(antal*(x(3)+1),C,0.86))));

end
---------

Subject: patternsearch, error message: Objective function must be real

From: dpb

Date: 10 May, 2014 19:04:07

Message: 2 of 4

On 5/10/2014 1:43 PM, Sara wrote:
...

> My objective problem is an non-smooth function. I solved the
> optimization problem in the beginning with fmincon and got the error
> messsage "the userfcn must be type double".
> Therefore I decided to use patternsearch.
...

Why does that follow? Let's see the function definition and the error
in context.

--

Subject: patternsearch, error message: Objective function must be real

From: Sara

Date: 10 May, 2014 21:05:12

Message: 3 of 4

dpb <none@non.net> wrote in message <lklt7h$9n6$1@speranza.aioe.org>...
> On 5/10/2014 1:43 PM, Sara wrote:
> ...
>
> > My objective problem is an non-smooth function. I solved the
> > optimization problem in the beginning with fmincon and got the error
> > messsage "the userfcn must be type double".
> > Therefore I decided to use patternsearch.
> ...
>
> Why does that follow? Let's see the function definition and the error
> in context.
>
> --

If you run the code for the function myfun2 that is given above with:
[x, fval]=fmincon(@myfun2,x0,AA,bb,[],[],[0;0;0],[area_flyg/(L1*b1*number);area/(L2*b2*number);area_flyg/(number*L3*b3)])%,[],options)
you get the following message:

Error using fmincon (line 783)
FMINCON requires all values returned by user functions to be of data type double.

Error in testversion2 (line 24)
[x,
fval]=fmincon(@myfun2,x0,AA,bb,[],[],[0;0;0],[area_flyg/(L1*b1*antal);area_flyg/(L2*b2*antal);area_flyg/(antal*L3*b3)])%,[],options)
>>
Thats probably because of myfun2, using the hpf which makes my calculations in slh1, slh2 and slh3 nonzero. Without this hpf, because the values are to small, matlab calculate them to 0.

Subject: patternsearch, error message: Objective function must be real

From: dpb

Date: 10 May, 2014 22:16:00

Message: 4 of 4

On 5/10/2014 4:05 PM, Sara wrote:
...

> Thats probably because of myfun2, using the hpf which makes my
> calculations in slh1, slh2 and slh3 nonzero. Without this hpf, because
> the values are to small, matlab calculate them to 0.

In that case, I'd suggest you find an alternative evaluation technique.
  If the values underflow a double, it's not likely there's real meaning
there, anyway.

--

Tags for this Thread

No tags are associated with this thread.

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.

Contact us