d ode := C( x ) = k C( x ) dx > ics := C(0)=C[0]; Solve the ODE (for comparison only) ics := C( 0 ) = C0 > dsolve({ode, ics}); ( k x ) C( x ) = C0 e > C100:=C[0]*exp(-k*100); ( 100 k ) C100 := C0 e
Modified Euler Method
> k:=.01:C[0]:=1:printf("deltat/s C/C[0] %%error Time"): for j from 0 to 4 do deltat:=1/(10^j); Ccalc:=C[0]: icount:=0; starttime:=time(): for i from deltat to 100 by deltat do icount:=icount+1; ytemp:=Ccalc*(1-0.5*k*deltat); yprimeavg:=-k*ytemp; Ccalc:=Ccalc+deltat*yprimeavg: end do: P_error:=100*(C100-Ccalc)/C100; calctime:=time()-starttime: printf("%8.6lf %8.6lf %8.6lf %7.4lf\n",deltat,Ccalc,P_error,calctime): end do: deltat/s C/C[0] %error Time 1.000000 0.367886 -0.001679 0.0000 0.100000 0.367880 -0.000017 0.0160 0.010000 0.367879 -0.000001 0.1720 0.001000 0.367879 0.000001 1.8120 0.000100 0.367879 -0.000001 17.3290 > >