There's error ezplot(sol_a.x1,[0 1]) in my code below for solving a special optimal control problem
% State equations
syms x1 x2 p1 p2 u;for i=0:0.1:1Dx1 = -2*x2+u;Dx2 = 2*x1;% Cost function inside the integral
syms g;g = u^2;% Hamiltonian
syms p1 p2 H;H = g + p1*Dx1 + p2*Dx2;% Costate equations
Dp1 = -diff(H,x1);Dp2 = -diff(H,x2);% solve for control u
du = diff(H,u);sol_u = solve(du, 'u');% Substitute u to state equations
Dx1 = subs(Dx1, u, sol_u);% convert symbolic objects to strings for using 'dsolve'
eq1 = strcat('Dx1=',char(Dx1));eq2 = strcat('Dx2=',char(Dx2));eq3 = strcat('Dp1=',char(Dp1));eq4 = strcat('Dp2=',char(Dp2));sol_h = dsolve(eq1,eq2,eq3,eq4);%% use boundary conditions to determine the coefficients
conA1 = 'x1(0) = 3-i';conA2 = 'x2(0) = 3-i';conA3 = 'x1(1) = 0.5-(0.5)*i';conA4 = 'x2(1) = 0.5-(0.5)*i';sol_a = dsolve(eq1,eq2,eq3,eq4,conA1,conA2,conA3,conA4);% plot solutions
figure(1);ezplot(sol_a.x1,[0 1]); axis([0 1 -4 3]);hold on;end
When I run the above code for every value i=0,0.1,…,1 separately, it works, but for "For Loop" it won't work.
Best Answer