Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error 98 (variable returned by scilab argument function is incorrect)?

1,792 views
Skip to first unread message

jarino

unread,
Apr 14, 2008, 4:00:46 AM4/14/08
to
Hi,

I have been trying to figure this one out for a few days and just
cannot get it. I have looked up previous posts on the subject, and the
only thing really constructive that I found seems to indicate to take
the real part of the output, which I have done, to no avail..

So if anyone could shed some light on what I am doing wrong, I would
be very grateful.

The context: I want to do some minimization to find parameters of an
ODE so that the solution is as close as possible to some data points.
So I have two functions, given below.

I set values, etc. Then, if I call the wrapper function by itself, I
get a correct result:
-->wrapper_2d([2,3])
ans =

250.51406

It also returns a gradient vector if the invocation is of the form
[f,g]=wrapper_2d(). However, if I try to use optim I get the following
output:
-->[error_size,result]=optim(wrapper_2d,[p.gmax,p.C]);
!--error 98
variable returned by scilab argument function is incorrect

What am I doing wrong? I have the equivalent version running smoothly
under matlab, but I was hoping to get that running for a student of
mine (hence the wordy comments, sorry) who does not have matlab at
home, and I am baffled..

Thanks,
J.

1) First, the RHS of the ODE

function dx=rhs_growth(t,x,p)


// Set h=x(1) and s=x(2) for clarity

h=x(1);
s=x(2);


// Compute g(s)

g=p.gmax*s/(p.C+s);


// dx=[h',s']

dx=[g*p.K*(1-h/(p.Ch+h));p.D*(p.s0-s)-g];


endfunction


2) Then a wrapper that computes squares of differences.
function [f,g,ind]=wrapper_2d(v,ind)
// The optimization routine does not accept parameters, so the
parameter
// structure p must be given global scope (when calling wrapper, not
for
// running simulations). (This is not true in scilab, but the matlab
constraint is kept
// for backward compatibility with matlab).
global p;

// Set the values of gmax and C
p.gmax=v(1);
p.C=v(2);

// Call the numerical solver. Note that we want the values at the
exact same
// dates as provided in the data. So instead of calling with the
classical
// time span, we provide a vector of dates for which we want values.
We have
// these dates: they are the ones in the data, p.dates
x=ode(p.IC,0,p.dates,list(rhs_growth,p));

g=(x(1,:)-p.population).^2; //This is the vector of square of
differences
f=real(sum(g)); //And the sum of elements of this vector (the
error)
g=real(g);

endfunction

ycollet

unread,
Apr 14, 2008, 9:02:11 AM4/14/08
to
> It also returns a gradient vector if the invocation is of the form
> [f,g]=wrapper_2d(). However, if I try to use optim I get the following
> output:
> -->[error_size,result]=optim(wrapper_2d,[p.gmax,p.C]);
> !--error 98
> variable returned by scilab argument function is incorrect
>

Hello,

Some months ago, I met this problem. This was due to the objective
function which was returning a complex value.
Maybe you can add some test in your code to see if a complex value is
returned despite the "real" function ...

f = real(sum(g));
if ~isreal(f) then disp(f); end
g = real(g);
if or(~isreal(g)) then disp(g'); end

YC

jarino

unread,
Apr 14, 2008, 10:16:30 AM4/14/08
to
I tried. I also added a printf('g is real')/printf('g is complex') -
and same for f- depending on the cases, and it seems to say the output
is real. The fact that ind is undefined is the function is not called
as wrapper_2d([v1,v2],ind) does not pose a problem, right?

Rainer von Seggern

unread,
Apr 15, 2008, 10:24:37 AM4/15/08
to

Hi
In your function [f,g,ind] = wrapper_2d(v,ind) f should be the sum of
squares which you want to minimize,
and g the gradient of this sum w.r.t. the parameter vector v. I think
this gradient is not correct in your case.
Try a wrapper without the gradient, and use the Scilab function NDcost
in optim.
(You can test your gradient with the help of the Scilab function
derivative which is used in NDcost)
Rainer

0 new messages