1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) Scilab Enterprises - 2013 - Paul Bignier
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING,
7 // which you should have received as part of this distribution.
8 // The terms are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 // =============================================================================
12 //C-----------------------------------------------------------------------
14 //C The initial value problem is..
15 //C DY/DT = ((2*LOG(Y) + 8)/T - 5)*Y, Y(1) = 1, 1 .LE. T .LE. 6
16 //C The solution is Y(T) = EXP(-T**2 + 5*T - 4), YPRIME(1) = 3
17 //C The two root functions are..
18 //C G1 = ((2*LOG(Y)+8)/T - 5)*Y (= DY/DT) (with root at T = 2.5),
19 //C G2 = LOG(Y) - 2.2491 (with roots at T = 2.47 and 2.53)
20 //C-----------------------------------------------------------------------
21 y0=1;t=2:6;t0=1;y0d=3;
22 info=list([],0,[],[],[],0,[],1,[],0,1,[],[],1);
23 atol=1.d-6;rtol=0;ng=2;
24 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,'res1',ng,'gr1',info,'psol1','pjac1');
25 if abs(nn(1)-2.47)>0.001 then pause,end
26 y0=yy(2,2);y0d=yy(3,2);t0=nn(1);t=[3,4,5,6];
27 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,'res1',ng,'gr1',info,'psol1','pjac1');
28 if abs(nn(1)-2.5)>0.001 then pause,end
29 y0=yy(2,1);y0d=yy(3,1);t0=nn(1);t=[3,4,5,6];
30 info=list([],0,[],[],[],0,[],0,[],0,0,[],[],1);
31 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,'res1',ng,'gr1',info);
32 if abs(nn(1)-2.53)>0.001 then pause,end
34 // Same problem, but using macro for the derivative evaluation function 'res1'
35 deff('[delta,ires]=res1(t,y,ydot)','ires=0;delta=ydot-((2.*log(y)+8)./t-5).*y')
36 deff('[rts]=gr1(t,y,yd)','rts=[((2*log(y)+8)/t-5)*y;log(y)-2.2491]')
38 y0=1;t=2:6;t0=1;y0d=3;
39 atol=1.d-6;rtol=0;ng=2;
40 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
41 if abs(nn(1)-2.47)>0.001 then pause,end
42 y0=yy(2,2);y0d=yy(3,2);t0=nn(1);t=[3,4,5,6];
43 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
44 if abs(nn(1)-2.5)>0.001 then pause,end
45 y0=yy(2,1);y0d=yy(3,1);t0=nn(1);t=[3,4,5,6];
46 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
47 if abs(nn(1)-2.53)>0.001 then pause,end
49 // Same problem, but using macros for the preconditioner evaluation and application functions 'pjac' and 'psol'
50 // pjac uses the macro res1 defined above.
51 function [wp, iwp, ires] = pjac(neq, t, y, ydot, h, cj, rewt, savr)
57 wp = zeros(neq*neq, 1);
58 iwp = zeros(neq*neq, 2);
60 del = max(SQuround*max(abs(y(i)), abs(h*ydot(i))), 1/rewt(i))
61 if h*ydot(i) < 0 then del = -del; end
65 ydot(i) = ydot(i) + cj*del;
66 [e ires] = res1(tx, y, ydot);
73 wp(nrow+j) = delinv*(e(j)-savr(j));
74 if isnan(wp(nrow+j)) then
86 function [r, ier] = psol(wp, iwp, b)
88 //Compute the LU factorization of R.
91 //Solve the system LU*X = b
96 y0=1;t=2:6;t0=1;y0d=3;
97 info=list([],0,[],[],[],0,[],1,[],0,1,[],[],1);
98 atol=1.d-6;rtol=0;ng=2;
99 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,'gr1',info,psol,pjac);
100 if abs(nn(1)-2.47)>0.001 then pause,end
101 y0=yy(2,2);y0d=yy(3,2);t0=nn(1);t=[3,4,5,6];
102 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,'gr1',info,psol,pjac);
103 if abs(nn(1)-2.5)>0.001 then pause,end
104 y0=yy(2,1);y0d=yy(3,1);t0=nn(1);t=[3,4,5,6];
105 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,'gr1',info,psol,pjac);
106 if abs(nn(1)-2.53)>0.001 then pause,end
109 //C-----------------------------------------------------------------------
110 //C Second problem (Van Der Pol oscillator).
111 //C The initial value problem is..
112 //C DY1/DT = Y2, DY2/DT = 100*(1 - Y1**2)*Y2 - Y1,
113 //C Y1(0) = 2, Y2(0) = 0, 0 .LE. T .LE. 200
114 //C Y1PRIME(0) = 0, Y2PRIME(0) = -2
115 //C The root function is G = Y1.
116 //C An analytic solution is not known, but the zeros of Y1 are known
117 //C to 15 figures for purposes of checking the accuracy.
118 //C-----------------------------------------------------------------------
119 info=list([],0,[],[],[],0,[],0,[],0,0,[],[],1);
120 rtol=[1.d-6;1.d-6];atol=[1.d-6;1.d-4];
121 t0=0;y0=[2;0];y0d=[0;-2];t=[20:20:200];ng=1;
122 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,'res2','jac2',ng,'gr2',info);
123 if abs(nn(1)-81.163512)>0.001 then pause,end
125 deff('[delta,ires]=res2(t,y,ydot)',...
126 'ires=0;y1=y(1),y2=y(2),delta=[ydot-[y2;100*(1-y1*y1)*y2-y1]]')
127 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,'jac2',ng,'gr2',info);
128 deff('J=jac2(t,y,ydot,c)','y1=y(1);y2=y(2);J=[c,-1;200*y1*y2+1,c-100*(1-y1*y1)]')
129 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,'gr2',info);
130 deff('s=gr2(t,y,yd)','s=y(1)')
131 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,gr2,info);
133 // Same problem, with psol and pjac example routines
135 info=list([],0,[],[],[],0,[],1,[],0,1,[],[],1);
136 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,'gr2',info,'psol1','pjac1');
137 if abs(nn(1)-81.163512)>0.009 then pause,end
138 deff('s=gr2(t,y,yd)','s=y(1)')
139 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,gr2,info,'psol1','pjac1');
140 if abs(nn(1)-81.163512)>0.009 then pause,end
142 // Same problem, with psol and pjac macros
144 // Redefine pjac to use res2
145 function [wp, iwp, ires] = pjac(neq, t, y, ydot, h, cj, rewt, savr)
147 SQuround = 1.490D-08;
151 wp = zeros(neq*neq, 1);
152 iwp = zeros(neq*neq, 2);
154 del = max(SQuround*max(abs(y(i)), abs(h*ydot(i))), 1/rewt(i))
155 if h*ydot(i) < 0 then del = -del; end
159 ydot(i) = ydot(i) + cj*del;
160 [e ires]=res2(tx, y, ydot);
161 if ires < 0 then return; end
164 wp(nrow+j) = delinv*(e(j)-savr(j));
173 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,'gr2',info,psol,pjac);
174 if abs(nn(1)-81.163512)>0.003 then pause,end
175 deff('s=gr2(t,y,yd)','s=y(1)')
176 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,gr2,info,psol,pjac);
177 if abs(nn(1)-81.163512)>0.003 then pause,end
178 info=list([],0,[],[],[],0,[],0,[],0,0,[],[],1);
182 [yy,nn,hotd]=daskr([y0,y0d],t0,t,atol,rtol,'res2','jac2',ng,'gr2',info);
183 t01=nn(1);t=100:20:200;[pp,qq]=size(yy);y01=yy(2:3,qq);y0d1=yy(3:4,qq);
184 [yy,nn,hotd]=daskr([y01,y0d1],t01,t,atol,rtol,'res2','jac2',ng,'gr2',info,hotd);
185 if abs(nn(1)-162.57763)>0.004 then pause,end
187 //Test of Dynamic link (Require f77!)
188 // 1 making the routines
190 ' SUBROUTINE RES22(T,Y,YDOT,DELTA,IRES,RPAR,IPAR)';
191 ' IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
193 ' DIMENSION Y(*), YDOT(*), DELTA(*)';
195 ' CALL F2(NEQ,T,Y,DELTA)';
197 ' DELTA(I) = YDOT(I) - DELTA(I)';
201 ' SUBROUTINE F2 (NEQ, T, Y, YDOT)';
202 ' IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
204 ' DOUBLE PRECISION T, Y, YDOT';
205 ' DIMENSION Y(*), YDOT(*)';
207 ' YDOT(2) = 100.0D0*(1.0D0 - Y(1)*Y(1))*Y(2) - Y(1)';
212 ' SUBROUTINE JAC22 (T, Y, ydot, PD, CJ, RPAR, IPAR)';
214 ' IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
216 ' DOUBLE PRECISION T, Y, PD';
217 ' PARAMETER (NROWPD=2)';
218 ' DIMENSION Y(2), PD(NROWPD,2)';
221 ' PD(2,1) = -200.0D0*Y(1)*Y(2) - 1.0D0';
222 ' PD(2,2) = 100.0D0*(1.0D0 - Y(1)*Y(1))';
223 ' PD(1,1) = CJ - PD(1,1)';
224 ' PD(1,2) = - PD(1,2)';
225 ' PD(2,1) = - PD(2,1)';
226 ' PD(2,2) = CJ - PD(2,2)';
232 ' SUBROUTINE GR22 (NEQ, T, Y, NG, GROOT, RPAR, IPAR)';
233 ' IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
235 ' DOUBLE PRECISION T, Y, GROOT';
236 ' DIMENSION Y(*), GROOT(*)';
241 //Uncomment lines below: link may be machine dependent if some f77 libs are
243 //unix_g('cd /tmp;rm -f /tmp/res22.f');unix_g('cd /tmp;rm -f /tmp/gr22.f');
244 //unix_g('cd /tmp;rm -f /tmp/jac22.f');
245 //write('/tmp/res22.f',res22);write('/tmp/gr22.f',gr22);write('/tmp/jac22.f',jac22)
246 //unix_g("cd /tmp;make /tmp/res22.o");unix_g('cd /tmp;make /tmp/gr22.o');
247 //unix_g('cd /tmp;make /tmp/jac22.o');
248 // 2 Linking the routines
249 //link('/tmp/res22.o','res22');link('/tmp/jac22.o','jac22');link('/tmp/gr22.o','gr22')
250 //rtol=[1.d-6;1.d-6];atol=[1.d-6;1.d-4];
251 //t0=0;y0=[2;0];y0d=[0;-2];t=[20:20:200];ng=1;
252 // 3 Calling the routines by dasrt
253 //[yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,'res22','jac22',ng,'gr22',info);
255 //[yy,nn,hotd]=dasrt([y0,y0d],t0,t,atol,rtol,'res22','jac22',ng,'gr22',info);
256 //t01=nn(1);t=100:20:200;[pp,qq]=size(yy);y01=yy(2:3,qq);y0d1=yy(3:4,qq);
257 //[yy,nn,hotd]=dasrt([y01,y0d1],t01,t,atol,rtol,'res22','jac22',ng,'gr22',info,hotd);