First of all, let's fix your code.
g = @(u) 30*u(1) + (20/2)*(u(2))^2 + (20/2)*(u(3))^2 + (10/2)*(u(4))^2 + (40/2)*(u(5))^2;
u0 = [1 1 1 1 100000]; %
lb = zeros(1,5);
ub = ones(1,5);
[u,fval,output,lambda] = fmincon(g, u0, [],[],[],[], lb, ub);
In this way I find only one value for each u. How can I do that and find the different values for each instant of time?
By writing a loop over time.
for t=1:T
g = ----- something t-dependent --------
[u(t,:),fval,output,lambda] = fmincon(g, u0, [],[],[],[], lb, ub);
end