How can simulate The system with Transfer function in ODE45?
Show older comments
Hi guys, I have the trouble with solving the ODE with a trasfer function.
I would like to simulate using ode solver in matlab as following block diagram:

where
and the plant
is described by:
.Kis the LQ gain.
Here is the my code.
clear all
close all
% Set Parameters for Simulation
cal_step = 0.001;
end_time = 200;
tspan = [0:cal_step:end_time];
x0 = [10;0];
%% Set Parameters of the a(s)%%
alpha = 100;
beta = 0.1;
up_num = [alpha 0];
under_num = [beta 1];
[A,B,C,D] = tf2ss(up_num, under_num);
b_s = tf(up_num, under_num);
%% Set Parameters of the Plant %%
m = 2; k = 0.5; c = 0.05;
A_plant = [0 1; -k/m -c/m];
B_plant = [0; 1/m];
C_plant = eye(2);
D_plant = [0;0];
%% Calculate the LQ Gain %%
Q = eye(2);
R = 1;
P_riccati = icare(A_plant, B_plant, eye(2), 1);
K_ipd = -inv(R)*transpose(B_plant)*P_riccati;
%% Make the Structure for the ode function %%
para.A = A;
para.B = B;
para.C = C;
para.D = D;
para.A_plant = A_plant;
para.B_plant = B_plant;
para.K_ipd = K_ipd;
%% Simulation ode45 and simulink %%
[t x_plant] = ode45(@(t,x_plant) odeplant(t, x_plant, para), tspan, x0);
sim('simulink_model')
simresult = ans;
x_simulink = simresult.x.Data;
t_simulink = simresult.x.Time;
%% Plot the Result %%
figure(1)
subplot(3,1,1)
plot(t_simulink, x_simulink)
title("Simulink")
subplot(3,1,2)
plot(tspan,x_plant)
title("M file Program")
subplot(3,1,3)
plot(tspan,x_plant-x_simulink)
title("Error between M file and Simulink")
function dxdt = odeplant(t,x,para)
A_plant = para.A_plant;
B_plant = para.B_plant;
A = para.A;
B = para.B;
C = para.C;
D = para.D;
K_ipd = para.K_ipd;
u = K_ipd*x;
u_dash = C*integral(@(tau) exp(A*(t-tau))*B.*u, 0, t) + D.*u; % Calculating the transfer function by convolution
dxdt = A_plant*x+B_plant*u_dash;
end
This is the result figure:

In my code, I calculate the trasfer function as a ordinary differential equation.
Thank you!
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


