How I can plot input signal in ode45
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
hossen hassanzadth
le 12 Oct 2023
Réponse apportée : Sam Chak
le 13 Oct 2023
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 commentaires
Réponse acceptée
Star Strider
le 12 Oct 2023
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 commentaire
Dyuman Joshi
le 12 Oct 2023
Just a small suggestion to preallocate u and negate the output dx if it is needed.
Plus de réponses (1)
Sam Chak
le 13 Oct 2023
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 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!