Integration of a vector inside function for ode45
12 次查看(过去 30 天)
显示 更早的评论
I am trying to solve a set of differential equations using ode45 command. The problem is I want to integrate one of the variables of the differential equations. For that I need to save the values/solution of that variable in a vector inside the function. I am unable to store x(1) as a vector. In the code, I need to integrate x(1) for which I have used cumtrapz but its not helping . My differential equations arre x1dot=x2 ;x2dot=-9.81*sin(x1)+u where dot means diffeentiation with respect to time. For u, e, ei and de are as defined in the code. Kindly help.
function dxdt=slmc(t,x)
dx1dt=x(2);
e=-x(1);
de=-x(2);
ei=0.01*cumtrapz(e);
u=75*e+5*ei+25*de;
dx2dt=-9.81*sin(x(1,:))+u;
dxdt=[dx1dt;dx2dt];
end
t=0:0.01:10;
in=pi/2;
indxdt=0;
[t,x]= ode45(@(t,x) slmc(t,x),t,[in indxdt]);
plot(t,x(:,1))
0 个评论
采纳的回答
Torsten
2022-9-7
In the code below, x(3) = integral_{tau=0}^{tau=t} -0.01*x(1) dtau
t=0:0.01:10;
in=pi/2;
indxdt=0;
[t,x]= ode45(@(t,x) slmc(t,x),t,[in indxdt 0]);
plot(t,x(:,3))
function dxdt=slmc(t,x)
dx1dt=x(2);
e=-x(1);
de=-x(2);
ei = x(3);
u=75*e+5*ei+25*de;
dx2dt=-9.81*sin(x(1,:))+u;
dxdt=[dx1dt;dx2dt;-0.01*x(1)];
end
11 个评论
Paul
2022-9-9
The initial() command is plotting the IC response of the plant, not the closed loop system.
The closed loop system formed by G and c1 is not the same as that implemented in test_pid2. In the latter, the control input is
u=50*e+30*ei+16*de = -(50*x1 + 30*intx1 + 16*x2) % that 16*x2 was probably meant to be 16*x1dot
whereas in the former the control is
u = -(50*y + 30*inty + 16*ydot) = -(50*x2 + 30*intx2 + 16*x2dot)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!