How I can plot input signal in ode45
조회 수: 4 (최근 30일)
이전 댓글 표시
I want to plot the control signal (u) when we solve the equation with ode45. how I can plot control signal (u) in the following code.
tspan = 0:1:10000;
y0 = [0.2,0.3];
[t,y] = ode45(@(t,y) odefcn(t,y), tspan, y0);
%
function dx = odefcn(t,x)
dx = zeros(2,1);
u=[2 -2]*x(1:2)+1;
dx(1)=x(1)-2*x(2)
dx(2)=-x(2)+u
end
댓글 수: 0
채택된 답변
Star Strider
2023년 10월 12일
Create a second output for ‘u’ (ode45 will ignore it during the integration) and then return it using a for loop after the integration finishes.
Try this —
tspan = 0:0.001:15;
y0 = [0.2,0.3];
[t,y] = ode45(@(t,y) odefcn(t,y), tspan, y0);
for k = 1:numel(t)
[dx,u(k)] = odefcn(t(k),y(k,:).');
end
figure
plot(t,y(:,1), 'DisplayName','y_1(t)')
hold on
plot(t,y(:,2), 'DisplayName','y_2(t)')
plot(t,u, 'DisplayName','u(t)')
hold off
grid
legend('Location','best')
% set(gca,'XScale','log')
function [dx,u] = odefcn(t,x)
dx = zeros(2,1);
u=[2 -2]*x(1:2)+1;
dx(1)=x(1)-2*x(2);
dx(2)=-x(2)+u;
end
I changed ‘tspan’ because with a long vector, the details of the initial transient disappear from the plot.
.
댓글 수: 1
Dyuman Joshi
2023년 10월 12일
Just a small suggestion to preallocate u and negate the output dx if it is needed.
추가 답변 (1개)
Sam Chak
2023년 10월 13일
Your initial query has already been addressed. To achieve step profile tracking using your designed gain matrix, you should multiply the reference input (xref) by a scaling factor (sf). In doing so, the blue curve will consistently attain its steady-state position .
tspan = 0:0.001:10;
x0 = [0.2, 0.3];
[t, x] = ode45(@odefcn, tspan, x0);
% Computing the control signal u from the ode solution
u = [2 -2]*x.' + (-0.5)*(1); % sf = -0.5; xref = 1;
plot(t, x(:,1), 'DisplayName', 'x_1(t)'), hold on
plot(t, x(:,2), 'DisplayName', 'x_2(t)')
plot(t, u, 'DisplayName', 'u(t)'), hold off, grid on
xlabel('Time, (seconds)')
title('Step Response')
legend('location', 'SE', 'fontsize', 12)
% Dynamics
function [dx, u] = odefcn(t, x)
dx = zeros(2,1);
xref = 1; % reference input
sf = -0.5; % scaling factor
u = [2 -2]*x + sf*xref; % control signal
dx(1) = x(1) - 2*x(2); % x'
dx(2) = - x(2) + u; % x"
end
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!