
% Assume the stochastic control
% min E[integral from o to T over f(alp_s,X_s)ds + g(X_T)]
% X_t = x and dX_s = sig(alp_s,X_s)dW_s + b(alp_s)ds for s>t

% Example 2:

f_fct = @(t, alp_t, x) 1;
g_fct = @(x) 0;
sig_fct = @(t,alp_t,x) 0;
b_fct = @(t, alp_t) -alp_t;

u_sol = @(t,x) min(1-t, 1- abs(x) );


T = 1;
x_vec = linspace(-1,1,20);
t_vec = linspace(0,T,t_length);

x_length = length(x_vec);
t_length = length(t_vec);

A_M = zeros(t_length,2);
A_M(:,1) = -1;
A_M(:,2) = 1;

[alpha,u_num] = stoch_SL_scheme(f_fct,g_fct,sig_fct,b_fct,A_M,x_vec,t_vec);

% Plot results

%[T_mesh,X_mesh] = meshgrid(t_vec,x_vec);

hold on
subplot(2,1,2)
xmin = -1;
xmax = 1;
tmin = 0;
tmax = 1;
ezsurf(u_sol,[tmin,tmax,xmin,xmax]);
xlabel('x');
ylabel('u');
title('exact solution u_{sol}');
subplot(2,1,2)
surf(t_vec,x_vec,u_num');
%surf(t,x,u_num');
xlabel('x');
ylabel('u');
title('numerical solution u_{num}');
zlim([-50 50]);
hold off
