Hello, kindly help. I am getting the following error: Error using odearguments @(T,X)STATEEQ(T,X,U,TU) must return a column vector. Error in Descent (line 17)

5 visualizaciones (últimos 30 días)
function
% Steepest descent method is used to find the solution.
eps = 1e-3;
options = odeset('RelTol', 1e-4, 'AbsTol',[1e-4]);
t0 = 0; tf = 1440;
% R = 0.1;
step = 0.4;
t_segment = 500;
Tu = linspace(t0, tf, t_segment); % discretize time
u = ones(1,t_segment); % guessed initial control u=1
initx = 0.1; % initial values for states
initp = 0; % initial values for costates
max_iteration = 100; % Maximum number of iterations
for i = 1:max_iteration
% 1) start with assumed control u and move forward
[Tx,X] = ode45(@(t,x) stateEq(t,x,u,Tu), [t0 tf], initx); %options);
% 2) Move backward to get the trajectory of costates
x = X(:,1);
[Tp,P] = ode45(@(t,p) costateEq(t,p,u,Tu,x,Tx), [tf t0], initp, options);
p = P(:,1);
% Important: costate is stored in reverse order. The dimension of
% costates may also different from dimension of states
% Use interploate to make sure x and p is aligned along the time axis
p = interp1(Tp,p,Tx);
% Calculate deltaH with x1(t), x2(t), p1(t), p2(t)
dH = pH(x,p,Tx,u,Tu);
H_Norm = dH'*dH;
% Calculate the cost function
E(i,1) = tf*((x')*x/length(Tx) + u/length(Tu));
% if dH/du < epslon, exit
if H_Norm < eps
% Display final cost
E(i,1)
break;
else
% adjust control for next iteration
u_old = u;
u = AdjControl(dH,Tx,u_old,Tu,step);
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(t)');
%text(.25,-.1,'x_2(t)');
text(.2,.4, 'u(t)');
s = strcat('Final cost is: E=',num2str(E(end,1)));
text(.4,1,s);
xlabel('time');
ylabel('states');
hold off;
print -djpeg90 -r300 eg2_descent.jpg
figure(2);
plot(E,'x-');
xlabel('Iteration number');
ylabel('J');
print -djpeg90 -r300 eg2_iteration.jpg
if i == max_iteration
disp('Stopped before required residual is obtained.');
end
% State equations
function dx = stateEq(t,x,u,Tu)
dx = zeros(2,1);
u = interp1(Tu,u,t); % Interploate the control at time t
R=50;
L=2;
q=10*sin(2*pi*Tu);
dx = 1/L*(q/u)-x*R/L-q/u;
% Costate equations
function dp = costateEq(t,p,u,Tu,x,xt)
dp = zeros(2,1);
x = interp1(xt,x,t); % Interploate the state varialbes
u = interp1(Tu,u,t); % Interploate the control
R=50;
L=2;
q=10*sin(2*pi*Tu);
dp = -2*x+p*R/L;
% Partial derivative of H with respect to u
function dH = pH(x,p,tx,u,Tu)
% interploate the control
u = interp1(Tu,u,tx);
R=50;
L=2;
q=10*sin(2*pi*Tu);
dH = 1+p*q/u^2-(p*q)/(L*u^2);
% 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;

Respuestas (1)

Cris LaPierre
Cris LaPierre el 11 de Mayo de 2023
Your function stateEq returns a variable dx. The error is telling you it must be a colum vector. The most likely solution is to transpose dx.
dx = 1/L*(q/u)-x*R/L-q/u;
dx = dx';
  12 comentarios
ILENIKEMANYA
ILENIKEMANYA el 11 de Mayo de 2023
I see what you are saying, thanks very much. It is caused by q=10*sin(2*pi*Tu).
q=10*sin(2*pi*t) is the correct one, my state is a function of time, I don't know how to treat that. My understanding is it will calculate q at each time. Or what am I suppose to do in that case? The other one was not a function of time.
Kindly advise again Please.
Cris LaPierre
Cris LaPierre el 11 de Mayo de 2023
You will know better than me if it is working correctly. Make the change, run the code, and inspect the results. Are they what you expect?

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by