How to insert innitial condition of x(t) with relative of time

1 次查看(过去 30 天)
Now I'm generating DC motor code to get some value from a state space equation plant.
But I have 2 question from this.
  1. I need to know how can I insert the innitial condition of x(t) with realative of time since 0-1 second
  2. If x(t) is matrix 3x1 and it have 3 variable x1 = supply current, x2 = angular displacement, x3 = angular velocity. How can I get the value of x2 at 1 second
This is my code
function [xt,x1,x2,x3] = motor1(t,xt)
xt = zeros(3,1)
L = 0.3; %H inductance
R = 2.5; %ohm resistance
Kv = 0.0195; %Vs/rad electromotive constant
Kt = 0.0195; %Nm/A torque constant
J = 17.2*10^-7; %kgm^2 inertia
D = 1*10^-6; %N.m.rad/s friction constant
A_motor = [-R/L 0 -Kv/L; 0 0 1; Kt/J 0 D/J];
B_motor = [1/L;0;0];
c = [1 1 1];
C_motor = diag(c);
D_motor = [0;0;0];
x1 = xt(1);
x2 = xt(2);
x3 = xt(3);
%y = lsim(A_motor,B_motor,C_motor,D_motor,t)
%x0 = [s1;s2;s3]
% s1 = is
% s2 = ceta
% s3 = omega
% Create state space
motor = ss(A_motor,B_motor,C_motor,D_motor)
% Check eigenvalues
%pole=eig(A_motor)
% Tranfer function
%TF_motor = tf(motor)\
e_At = expm(A_motor);
S = @(t) (e_At*(B_motor)*12);
x = ((e_At)*(xt))+(integral(S,0,1,'ArrayValued',true))
dx = ((A_motor)*(x))+((B_motor)*12)
y = ((C_motor)*x)+((D_motor)*12)
CM=ctrb(A_motor,B_motor)
step(motor)

回答(1 个)

TED MOSBY
TED MOSBY 2024-9-20
Hi,
Initial Condition: You can specify the initial condition 'x0' as a 3x1 matrix. This matrix represents the initial values of your state variables: supply current, angular displacement, and angular velocity. You can then simulate the system over a specified time range.
Simulating the System: You can use MATLAB's "lsim" function to simulate the response of your system to a specified input over time. This function allows you to specify the initial condition and will provide the state trajectory over time.
Have a look at the below revised code:
function [xt, x1, x2, x3] = motor1(t, x0)
% Define system parameters
L = 0.3; % H inductance
R = 2.5; % ohm resistance
Kv = 0.0195; % Vs/rad electromotive constant
Kt = 0.0195; % Nm/A torque constant
J = 17.2e-7; % kgm^2 inertia
D = 1e-6; % N.m.rad/s friction constant
% State-space matrices
A_motor = [-R/L 0 -Kv/L; 0 0 1; Kt/J 0 -D/J];
B_motor = [1/L; 0; 0];
C_motor = eye(3); % Output all states
D_motor = [0; 0; 0];
% Create state-space system
motor = ss(A_motor, B_motor, C_motor, D_motor);
% Define input voltage as a step input (e.g., 12V)
u = 12 * ones(size(t));
% Simulate the system
[y, t_out, x] = lsim(motor, u, t, x0);
% Extract state variables at t = 1 second
[~, idx] = min(abs(t_out - 1)); % Find index closest to t = 1
x1 = x(idx, 1); % Supply current
x2 = x(idx, 2); % Angular displacement
x3 = x(idx, 3); % Angular velocity
% Output the state at each time step
xt = x;
end
t = linspace(0, 5, 1000); % Time vector from 0 to 5 seconds
x0 = [0; 0; 0]; % Initial condition: all states start at zero
[xt, x1, x2, x3] = motor1(t, x0);
disp(['Angular displacement at 1 second: ', num2str(x2)]);
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by