6
// dy1/dt = -0.04*y1 + 1.e4*y2*y3
8
// dy2/dt =0.04*y1 - 1.e4*y2*y3 - 3.e7*y2**2
10
// dy3/dt = 3.e7*y2**2
12
// on the interval from t = 0.0 to t = 4.e10, with initial conditions
14
// y1 = 1.0, y2 = y3 = 0. the problem is stiff.
18
deff('[ydot]=f(t,y)',...
19
'f1=-0.04*y(1)+1e4*y(2)*y(3),...
21
ydot=[f1;-f1-f3;f3]','n')
25
deff('[jac]=j(t,y)',...
26
'jac(1,1)=-0.04;jac(1,2)=1.e4*y(3);jac(1,3)=1.e4*y(2),...
27
jac(3,1)=0;jac(3,2)=6.e7*y(2);jac(3,3)=0;...
28
jac(2,1)=-jac(1,1);jac(2,2)=-jac(1,2)-jac(3,2);jac(2,3)=-jac(1,3);','n')
32
y0=[1;0;0];t0=0;t1=[0.4,4];nt=size(t1,'*');
36
yref=[0.9851721 0.9055180;0.0000339 0.0000224;0.0147940 0.0944596];
40
// 1. fortran called by fydot, without jacobian
42
y1=ode(y0,t0,t1,'fex');
44
if maxi(y1-yref) > Leps then bugmes();quit;end
46
// 2. fortran called by fydot, type given (stiff), no jacobian
48
y2=ode('stiff',y0,t0,t1,'fex');
50
if maxi(y2-yref) > Leps then bugmes();quit;end
52
// 3. fortran called by fydot , fjac, type given
54
y3=ode('stiff',y0,t0,t1,'fex','jex');
56
if maxi(y3-yref) > Leps then bugmes();quit;end
60
[z,w,iw]=ode('stiff',y0,0,0.4,'fex','jex');
62
z=ode('stiff',z,0.4,4,'fex','jex',w,iw);
64
if maxi(z-y3(:,2)) > %eps then bugmes();quit;end
67
[y1,w,iw]=ode(y0,t0,t1(1),'fex');
69
y2=ode(y0,t1(1),t1(2:nt),'fex',w,iw);
71
if maxi([y1 y2]-yref) > Leps then bugmes();quit;end
74
[y1,w,iw]=ode(y0,t0,t1(1),'fex','jex');
76
y2=ode(y0,t1(1),t1(2:nt),'fex','jex',w,iw);
78
if maxi([y1 y2]-yref) > Leps then bugmes();quit;end
81
// variation of tolerances
83
atol=[0.001,0.0001,0.001];rtol=atol;
87
// 4. type given , scilab lhs ,jacobian not passed
89
y4=ode('stiff',y0,t0,t1(1),atol,rtol,f);
91
if maxi(y4(:,1)-yref(:,1)) > 0.01 then bugmes();quit;end
93
// 5. type non given, rhs and scilab jacobian
97
if maxi(y5-yref) > Leps then bugmes();quit;end
99
// 6. type given (stiff),rhs and jacobian by scilab
101
y6=ode('stiff',y0,t0,t1,0.00001,0.00001,f,j);
103
if (y6-yref) > 2*0.00001 then bugmes();quit;end
105
// 7. matrix rhs, type given(adams),jacobian not passed
109
a=rand(3,3);ea=expm(a);
111
deff('[ydot]=f(t,y)','ydot=a*y')
112
Warning :redefining function: f
115
t1=1;y=ode('adams',eye(a),t0,t1,f);
117
if maxi(ea-y) > Leps then bugmes();quit;end
123
// dy1/dt = -0.04*y1 + 1.e4*y2*y3
125
// dy2/dt =0.04*y1 - 1.e4*y2*y3 - 3.e7*y2**2
127
// 0. = y1 + y2 + y3 - 1.
129
// y1(0) = 1.0, y2(0) = y3(0) = 0.
133
deff('[r]=resid(t,y,s)','...
134
r(1)=-0.04*y(1)+1.d4*y(2)*y(3)-s(1),...
135
r(2)=0.04*y(1)-1.d4*y(2)*y(3)-3.d7*y(2)*y(2)-s(2),...
136
r(3)=y(1)+y(2)+y(3)-1')
138
deff('[p]=aplusp(t,y,p)','...
142
deff('[p]=dgbydy(t,y,s)','[-0.04,1.d4*y(3),1.d4*y(2);...
143
0.04,-1.d4*y(3)-6.d7*y(2),-1.d4*y(2);...
150
rand('seed',0);rand('normal');
152
nx=20;A=rand(nx,nx);A=A-4.5*eye();
154
deff('y=f(t,x)','y=A*x')
155
Warning :redefining function: f
158
deff('J=j(t,x)','J=A')
159
Warning :redefining function: j
162
x0=ones(nx,1);t0=0;t=[1,2,3,4,5];
169
// Test itask=%ODEOPTIONS(1)
172
//itask=1 ---> usual call (t=vector of instants, solution at all t)
174
//========================
176
itask=1;tcrit=0;h0=0;hmax=0;hmin=0;ixpr=0;mxstep=0;maxordn=0;maxords=0;
178
jacflag=2;ml=-1;mu=-1;
180
%ODEOPTIONS=[itask,tcrit,h0,hmax,hmin,jacflag,mxstep,maxordn,maxords,ixpr,ml,mu];
182
xf=ode(x0,t0,t,f); //lsoda
184
if norm(xf(:,nt)-eAt*x0)>Leps then bugmes();quit;end
186
xfj=ode(x0,t0,t,f,j); //lsoda with jacobian
187
Warning: Jacobian external is given, but
188
not used!, see %ODEOPTIONS(6)
191
if norm(xfj(:,nt)-eAt*x0)>Leps then bugmes();quit;end
195
//itask=2; ---> solution at mesh points ---> t=tmax
197
//========================
199
%ODEOPTIONS(1)=2;tmax=t(5);
201
xft=ode(x0,t0,tmax,f);
207
if xft(1,q)<tmax then bugmes();quit;end
209
if norm(xlast-expm(A*xft(1,q))*x0)>Leps then bugmes();quit;end
212
//itask=3; ---> solution at first mesh point beyond t=tmax
216
x3=ode(x0,t0,tmax,f);
218
if norm(x3(2:nx+1)-xlast,1)>Leps then bugmes();quit;end
221
//itask=4; test with %ODEOPTIONS(2)=tcrit
223
%ODEOPTIONS(1)=4; //---> computation at all t and t>tcrit are not called
225
tcrit=2.5;%ODEOPTIONS(2)=tcrit;
229
deff('y=fcrit(t,x)',['if t<=tcrit then'
232
' y=A*x;chk=resume(1);end'])
234
x42=ode(x0,t0,t,fcrit);
235
Warning: integration up to tcrit
238
if chk==1 then bugmes();quit;end
242
if norm(x42(:,q)-ode(x0,t0,tcrit,f),1)>Leps then bugmes();quit;end
245
//itask=5; test with %ODEOPTIONS(2)=tcrit
247
%ODEOPTIONS(1)=5; //---> computation at mesh points and t>tcrit are not called
249
%ODEOPTIONS(6)=2; // Estimated jacobian
253
x52=ode(x0,t0,2.3,fcrit);
255
if chk==1 then bugmes();quit;end
259
if x52(1,q)>tcrit then bugmes();quit;end
262
//test of %ODEOPTIONS(3:5)=[h0,hmax,hmin]
266
h0=0.0;hmax=0.1;hmin=0.0001;
268
%ODEOPTIONS(3:5)=[h0,hmax,hmin];
272
if norm(x35-xf,1)>Leps then bugmes();quit;end
275
//test of %ODEOPTIONS(6)=jacflag
277
%ODEOPTIONS(6)=1;//Jacobian given
279
%ODEOPTIONS(3:5)=[0 0 0];
281
x61=ode('st',x0,t0,t,f,j); //with Jacobian
283
if norm(x61-xf,1)>10*Leps then bugmes();quit;end
287
%ODEOPTIONS(6)=0; // jacobian nor called nor estimated
289
x60=ode('st',x0,t0,t,f,j); //Jacobian not used (warning)
290
Warning: Jacobian external is given, but
291
not used!, see %ODEOPTIONS(6)
294
x60=ode('st',x0,t0,t,f); //Jacobian not used
296
if norm(x60-x61,1)>10*Leps then bugmes();quit;end
300
%ODEOPTIONS(6)=1;//Jacobian estimated
302
x60=ode('st',x0,t0,t,f) ;
303
Warning: No Jacobian external given but
304
one is required by %ODEOPTIONS(6) value!
307
if norm(x60-x61,1)>10*Leps then bugmes();quit;end
310
//test of %ODEOPTIONS(6)=jacflag (adams)
312
%ODEOPTIONS(6)=1;//with given Jacobian
314
x60=ode('ad',x0,t0,t,f,j) ;
316
if norm(x60-x61,1)>10*Leps then bugmes();quit;end
320
%ODEOPTIONS(6)=0;// jacobian nor called nor estimated
322
x60=ode('ad',x0,t0,t,f,j); //Jacobian not used (warning)
323
Warning: Jacobian external is given, but
324
not used!, see %ODEOPTIONS(6)
327
x60=ode('ad',x0,t0,t,f); //Jacobian not used
329
if norm(x60-x61,1)>10*Leps then bugmes();quit;end
334
%ODEOPTIONS(6)=2;// jacobian estimated
338
if norm(x60-x61,1)>10*Leps then bugmes();quit;end
341
%ODEOPTIONS(6)=1;//Jacobian estimated
344
Warning: No Jacobian external given but
345
one is required by %ODEOPTIONS(6) value!
348
if norm(x60-x61,1)>10*Leps then bugmes();quit;end
354
itask=1;tcrit=0;h0=0;hmax=0;hmin=0;ixpr=0;mxstep=0;maxordn=0;maxords=0;
356
//provisional values as default
358
jacflag=2;ml=-1;mu=-1;
361
%ODEOPTIONS=[itask,tcrit,h0,hmax,hmin,jacflag,mxstep,maxordn,maxords,ixpr,ml,mu];
363
jacflag=2;%ODEOPTIONS(6)=jacflag; //Banded Jacobian, given
365
nx=20;A=diag(-[1:nx]);x0=ones(nx,1);
369
for k=1:nx-1, A(k,k+1)=1;end
371
for k=1:nx-2, A(k,k+2)=-2;end
373
for k=1:nx-1, A(k+1,k)=-1;end
375
for k=1:nx-2, A(k+2,k)=2;end
377
for k=1:nx-3, A(k+3,k)=-3;end
379
deff('xd=f(t,x)','xd=A*x')
380
Warning :redefining function: f
385
%ODEOPTIONS(11:12)=[ml,mu];
389
if A(i,j)<>0 then J(i-j+mu+1,j)=A(i,j);end
391
Warning :redefining function: j
394
// J is a ml+mu+1 x ny matrix.
396
// Column 1 of J is made of mu zeros followed by df1/dx1, df2/dx1, df3/dx1,
398
// i.e. 1 + ml possibly nonzeros entries.
400
// Column 2 of J is made of mu-1 zeros followed by df1/dx2, df2/dx2,0...
404
%ODEOPTIONS(6)=1;%ODEOPTIONS(11:12)=[-1,-1];
406
deff('jj=j1(t,x)','jj=A')
408
xnotband=ode('st',x0,t0,t,f,j1);
412
%ODEOPTIONS(6)=4;//banded jacobian external given
414
%ODEOPTIONS(11:12)=[3,2];
416
deff('jj=j(t,x)','jj=J')
418
xband=ode('st',x0,t0,t,f,j);
420
if norm(xnotband-xband,1)>Leps then bugmes();quit;end
423
%ODEOPTIONS(6)=5;//banded jacobian evaluated
425
%ODEOPTIONS(11:12)=[3,2];
427
deff('jj=j(t,x)','jj=J')
429
xband=ode('st',x0,t0,t,f,j);
430
Warning: Jacobian external is given, but
431
not used!, see %ODEOPTIONS(6)
434
if norm(xnotband-xband,1)>Leps then bugmes();quit;end
438
// Test of %ODEOPTIONS(7)
440
//%ODEOPTIONS(7)=mxstep ---> maximum number od steps allowed
442
itask=1;tcrit=0;h0=0;hmax=0;hmin=0;ixpr=0;mxstep=0;maxordn=0;maxords=0;
444
//provisional values as default
446
jacflag=2;ml=-1;mu=-1;
448
%ODEOPTIONS=[itask,tcrit,h0,hmax,hmin,jacflag,mxstep,maxordn,maxords,ixpr,ml,mu];
452
//ode(x0,t0,t,f); // ---> Non convergence
455
// Test of %ODEOPTIONS(8:9)
457
//%ODEOPTIONS(8:9)=[maxordn,maxords] ---> maximum order for nonstiff and stiff
459
itask=1;tcrit=0;h0=0;hmax=0;hmin=0;ixpr=0;mxstep=0;maxordn=0;maxords=0;
461
//provisional values as default
463
jacflag=2;ml=-1;mu=-1;
465
%ODEOPTIONS=[itask,tcrit,h0,hmax,hmin,jacflag,mxstep,maxordn,maxords,ixpr,ml,mu];
467
%ODEOPTIONS(8:9)=[0,0]; //---> default values
469
wref=ode(x0,t0,t,f); //just for computing reference
472
%ODEOPTIONS(8:9)=[4,3];
474
ww=ode(x0,t0,t,f);norm(wref-ww,1);
477
%ODEOPTIONS(8:9)=[12,5];
479
if norm(wref-ode(x0,t0,t,f),1)>Leps then bugmes();quit;end
487
wref=ode('st',x0,t0,t,f);
491
if norm(wref-ode('st',x0,t0,t,f),1) >Leps then bugmes();quit;end //=0
495
if norm(wref-ode('st',x0,t0,t,f),1) >Leps then bugmes();quit;end //small
499
//using nonstiff method
504
wref=ode('ad',x0,t0,t,f);
508
if norm(wref-ode('ad',x0,t0,t,f),1) >Leps then bugmes();quit;end //=0
512
if norm(wref-ode('ad',x0,t0,t,f),1) >Leps then bugmes();quit;end //small
517
%ODEOPTIONS(8:9)=[5,12];
521
%ODEOPTIONS(8:9)=[4,10];
523
if norm(ode(x0,t0,t,f)-wref,1)>Leps then bugmes();quit;end //small
527
A=diag([-10,-0.01,-1]);
529
deff('uu=u(t)','uu=sin(t)');
533
deff('y=f(t,x)','y=A*x+B*u(t)')
534
Warning :redefining function: f
539
yy1=ode('stiff',[1;1;1],0,1,f);
541
yy2=ode('stiff',[1;1;1],0,2,f);
545
yy1=ode('stiff',[1;1;1],0,1,f);
547
yy2=ode('stiff',[1;1;1],0,2,f);
552
rand('seed',0);rand('normal');
554
nx=20;A=rand(nx,nx);A=A-4.5*eye();
556
deff('y=f(t,x)','y=A*x')
557
Warning :redefining function: f
560
deff('J=j(t,x)','J=A')
561
Warning :redefining function: j
566
y2=ode('stiff',ones(nx,1),0,2,f,j);
568
[y1,w,iw]=ode('stiff',ones(nx,1),0,1,f,j);
570
y2p=ode('stiff',y1,1,2,f,j,w,iw);
572
y12=ode('stiff',ones(nx,1),0,[1,2],f,j);
576
yaf=ode('adams',ones(nx,1),0,2,f,j);
578
yaj=ode('adams',ones(nx,1),0,2,f,j);
580
ysf=ode('stiff',ones(nx,1),0,2,f,j);
582
ysj=ode('stiff',ones(nx,1),0,2,f,j);
585
deff('xd=f(t,x)','xd=A*x+B*sin(3*t)')
586
Warning :redefining function: f
589
A=rand(10,10)-4.5*eye();B=rand(10,1);
591
x=ode(ones(10,1),0,[1,2,3],f);
593
//link('fexab.o','fexab')
595
if norm(x-ode(ones(10,1),0,[1,2,3],'fexab'),1) > 1.d-10 then bugmes();quit;end