Crash related to complex matrix
Bug Description
I just came across a (dirty) Scilab bug and I'm having a little trouble simplifying the program below, but if you want to try I invite you to run it. It is a program which calculates (with the method of the complex step) the gradient of a function which is calculated by integrating an ODE with Euler method.
The cplxjac
function works well otherwise as can be seen on the first call. The script crashes from N=12:
Steps to reproduce
clear
function J=cplxjac(_F,x)
par=list();
if typeof(_F) == "list"
par=_F(2:$);
__F=_F(1);
else
__F=_F;
end
xi=x+0*%i;
d=1e-200;
J=zeros(length(__F(x,par(:))),length(x));
for i=1:length(x)
xi(i)=x(i)+%i*d;
J(:,i)=imag(__F(xi,par(:)))/d;
xi(i)=x(i)
end
endfunction
function dXdt=myRhs(t,X,u)
M=1;
m=0.1;
l=0.5;
g=9.81;
theta=X(2);
thetadot=X(4);
accel = [M+m, m*l*cos(theta); cos(theta), l] \ [u+m*l*thetadot^2*sin(theta); g*sin(theta)];
dXdt=[X(3:4);accel]
end
function [f,g,X]=cost(u,t,r,q)
N=length(u)+1;
h=t(2)-t(1);
X(:,1)=[1;%pi;0;0];
Xd=[0;%pi;0;0];
f = 0;
for k=1:N-1
f1 = myRhs(t(k),X(:,k),u(k));
X(:,k+1) = X(:,k)+h*f1;
f = f+h*sum(q.*(X(:,k)-Xd).^2)+h*r*u(k)^2;
end
endfunction
q=[4;1;1;1];
r=0.1;
l=0.5;
for N=2:20
T=4;
h=T/(N-1);
t=0:h:T;
u=zeros(N-1,1);
disp(N)
d=cplxjac(sum,u);
disp(d);
gc=cplxjac(list(cost,t,r,q),u);
disp(gc);
end
Error log
scilab-bin(7129,0x70000c55a000) malloc: Incorrect checksum for freed object 0x7f9df18646a8: probably modified after being freed.
Corrupt value: 0x0
scilab-bin(7129,0x70000c55a000) malloc: *** set a breakpoint in malloc_error_break to debug
Running the program in VS debugger gives the following info:
Edited by Stéphane MOTTELET