changing the value function
显示 更早的评论
Please see the following code. How can I modify J(i,1) if value functional involves only u instead of u^2? (simply u gives error as the dimension does not match.
function eg2OCmymodel_Descent
eps = 1e-2;
options = odeset('RelTol', 1e-4, 'AbsTol',[1e-4]);
t0 = 0;
tf = 60;
step = .4;
t_segment = 100;
Tu = linspace(t0, tf, t_segment);
u = ones(1,t_segment);
initx = [100];
initp = [0];
max_iteration = 100;
for i = 1:max_iteration
[Tx,X] = ode45(@(t,x) stateEq(t,x,u,Tu), [t0 tf], initx, options);
x1 = X(:,1);
[Tp,P] = ode45(@(t,p) costateEq(t,p,u,Tu,x1,Tx), [tf t0], initp, options);
p1 = P(:,1);
p1 = interp1(Tp,p1,Tx);
dH = pH(x1,p1,Tx,u,Tu);
H_Norm = dH'*dH;
J(i,1) = 60*((6*(x1')*x1)/length(Tx)-.5*u*(u')/length(Tu));
if H_Norm < eps
J(i,1)
break;
else
% adjust control for next iteration
u_old = u;
u = AdjControl(dH,Tx,u_old,Tu,step); %this function is defined later
end;
end
% plot the state variables & cost for each iteration
figure(1);
plot(Tx, X ,'-');
hold on;
plot(Tu,u,'r:');
text(.2,0.08,'x_1(t)');
text(.2,.4, 'u(t)');
s = strcat('Final cost is: J=',num2str(J(end,1)));
text(.4,1,s);
xlabel('time');
ylabel('states');
hold off;
figure(2);
plot(J,'x-');
xlabel('Iteration number');
ylabel('J');
if i == max_iteration
disp('Stopped before required residual is obtained.');
end
function dx = stateEq(t,x,u,Tu)
dx = zeros(1,1);
u = interp1(Tu,u,t);
dx(1) = -u;
% Costate equations
function dp = costateEq(t,p,u,Tu,x1,xt)
dp = zeros(1,1);
x1 = interp1(xt,x1,t); % Interploate the state varialbes
u = interp1(Tu,u,t); % Interploate the control
dp(1) = 0;
% Partial derivative of H with respect to u
function dH = pH(x1,p1,tx,u,Tu)
% interploate the control
u = interp1(Tu,u,tx);
dH = 6-u-p1;
% Adjust the control
function u_new = AdjControl(pH,tx,u,tu,step)
% interploate dH/du
pH = interp1(tx,pH,tu);
u_new = u - step*pH;
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Econometrics Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!