I would like to numerically approximate the solution of Hamilton-Jacobi-Bellman equation:
For $t \in (0,1)$, $x \in (-1,1)$, and $\lambda \in \{0,0.5,1\}$,
$$\left\{ \begin{array}{l} \partial_tu+\sup_{\lambda\in\Lambda}\{\lambda\partial_{xx}u+(1-x^2)\}=0 \\ u(t=1,x) = 0 \\ u(t,x=-1) = 0 \\ u(t,x=1) = 0 \end{array} \right.$$
I know that the analytic solution is: $u(t,x)=(1-t)(1-x^2)$.
I want to find the numerical solution for any initial guess in $\Lambda=\{0,0.5,1\}$.
Below is my code:
% Parameters
t=linspace(0,1,20);
x=linspace(-1,1,20);
dt=t(2)-t(1);
dx=x(2)-x(1);
% Initialization
u=NaN(length(t),length(x));
% Boundary conditions
u(end,:) = 0;
u(:,1) = 0;
u(:,end) = 0;
% Initial control
a=1;
for j =length(t)-1:-1:1
for i=2:length(x)-1
f(j,i)=1-x(i)^2;
u(j,i) = u(j+1,i) + a*dt/dx^2*(u(j+1,i+1)-2*u(j+1,i)+u(j+1,i-1)) + dt*f(j,i);
fun = @(y) -(y*(u(j,i+1)-2*u(j,i)+u(j,i-1))/dx^2+f(j,i)); %maximize the function
a = fminbnd(fun,-0.0001,1.0001);
%end
end
end
This code doesn't give the expected solution (analytic solution). Do you see where I am missing something?
Thank you.