# Question:Option remember

## Question:Option remember

Maple

Hi,

I am trying to implement some numerical algorithm for an optimal control problem (still).

It involves double cycle. In the inner one two DE systems are solved sequentially and the result is used to update values of some objective function.

This part uses remeber option thanks to pagan  and is ok.

However, in the outer cycle I use the values of this objective function to obtain solution for yet another DE and update ICs for the inner cycle.

In this part option remeber seems of no help and I have no idea as to how deal with it.

Here is the code:

restart:
> sys1:=STATC:
> sys2:=COSTATC:
>
> systot:=STATG;
> REC:=piecewise(RECC>1,1,RECC<1,RECC,RECC=1,1):
> F:=t->0.1;
> UU:=t->0.1:
> G:=t->0.1:
> GR:=0:
> N:=5:
> alpha:=0.005:
> TAUS:=0.7307:
> KS:=733.2:
> M:=808.9:x:=10:
> KS;
> kk:=t->733.2:

#the outer cycle starts here

for j from x to 220 by x do
> sys1:=subs(K=KS[j-x],tau[s]=TAUS[j-x],STATC):
> sys2:=subs(K=KS[j-x],tau[s]=TAUS[j-x],COSTATC):
> REC:=piecewise(RECC>1,1,RECC<1,RECC,RECC=1,1):
> print(j);
> F[j]:=UU[j-x]:
> #print(F[j]);

#the inner cycle starts here

> for i from 1 to N do
>
> print(i);
> newsys1:=subs(u(t)=F[i-1][j](t),sys1):
> #print(newsys1);
>
> #print(F[i-1]);

#first DE system computation

>  sol1[i][j]:=dsolve(newsys1, numeric,range=j-x+0.1..j,output=listprocedure, known=[F[i-1][j]]):
> #print(i);
> KK[i][j]:=subsop(3=remember,eval(K(t),sol1[i][j])):
>
> #KKS[i]:=piecewise(t<j-x,kk[j-x](t),t>j-x,KK[i](t),t=j-x,kk[j-x](t)):
> #print(KKS[i]);

#second DE system computation

> newsys2:=subs(u=F[i-1][j],K(t)=KK[i][j](t),sys2):
>
> #print(newsys2);
> sol2[i][j]:=dsolve(newsys2, numeric, output=listprocedure, range=j-x+0.1..j,known=[F[i-1][j],KK[i][j]]):
>
> PSI1[i][j]:=subsop(3=remember,eval(psi(t),sol2[i][j])):

#update of an objective function

> F[i][j]:=unapply('evalf'(subs(psi(t)=subsop(3=remember,eval(psi(t),sol2[i][j]))(t),K(t)=subsop(3=remember,eval(K(t),sol1[i][j]))(t),
> FF=F[i-1][j],REC)),t,numeric,proc_options=[remember]):
> #print(F[i]);
> end do:

#end of inner cycle

#outer DE system

aftersys:=subs(k=KS[j-x],tau=TAUS[j-x],m=M[j-x],u(t)=F[N][j](t),systot);
>
> #print(aftersys);
> aftersol[j]:=dsolve(aftersys, numeric,range=j-x+0.1..j,output=listprocedure, known=[F[N][j]]):
> kk[j]:=subsop(3=remember,eval(k(t),aftersol[j])):
> mm[j]:=subsop(3=remember,eval(m(t),aftersol[j])):
> tt[j]:=subsop(3=remember,eval(tau(t),aftersol[j])):

#updates of ICs for the inner DE systems newsys1 and newsys2

> KS[j]:=evalf(kk[j](j)):
> TAUS[j]:=evalf(tt[j](j)):
> M[j]:=evalf(mm[j](j)):
> UU[j]:=unapply('evalf'(subs(psi(t)=subsop(3=remember,eval(psi(t),sol2[N][j]))(t),K(t)=subsop(3=remember,eval(K(t),sol1[N][j]))(t),
> FF=F[N-1][j],REC)),t,numeric,proc_options=[remember]):
>
> print(UU[j](1));
> end do:

BaU is just the file with exact formulae for DEs and functions I use.

I have two problems.

First, the algorithm takes around 2 hours to compute until j=120 with step x=10, which is long. I think there might be some shortcut with remeber trick, as I use in the inner cycle (with i steps), but I do not know how to make it.

Second, when I evaluate variables from the output of sol1[i][j], it evaluates variable not in the range j-x..j as desired, but  on the whole available range, given by boundary conditions. Can I push Maple to evaluate variables only in some restricted (smaller) range. This may boost performance, I think...

Please, any ideas might be useful

AB ﻿