Implementation of Integral Cost function in Matlab

14 ビュー (過去 30 日間)
Telema Harry
Telema Harry 2022 年 8 月 18 日
編集済み: VBBV 2022 年 8 月 18 日
Please, I need ideas how to simulate the model in the attached document. writing code for equation 39 - 41 is trivial, however, I am not sure how to write the code for equation 42.
I implemented the code below for one time step and assuming that the control input u = 0.1. The question is it correct to compute the optimal cost function like this or there is a better way. Please, find attached the model.
tspan = [0,1];
x0 = 1;
u = 0.1;
tau = 1;
xn = 1;
[time, dxdt, J] = plant_dynamics(tspan,x0,u, tau, xn);
%% System Dynamics
function [time, dxdt, J] = plant_dynamics(tspan,x0,u, tau, xn)
[time, dxdt] = ode23(@solve_ode,tspan,x0);
J = xn + integral_cost(dxdt, u);
function dx = solve_ode(t,x)
A = 1 + tau/12000;
B = 1 + 0.25 * sin(2*pi*t/3000);
dx = A*x + B * u;
end
function int_J = integral_cost(dxdt, u)
x = dxdt;
integral_J = x.^2 + u.^2;
int_J = trapz(integral_J);
end
end

採用された回答

VBBV
VBBV 2022 年 8 月 18 日
編集済み: VBBV 2022 年 8 月 18 日
tspan = [0,1];
x0 = 1;
u = 0.1;
tau = 1;
xn = 1; % noise
[time, dxdt, J] = plant_dynamics(tspan,x0,u, tau, xn);
subplot(211)
plot(time,dxdt); title('Plant response')
subplot(212)
plot(time,J);title(' cost function (J) varying with noise input ')
%% System Dynamics
function [time, dxdt, J] = plant_dynamics(tspan,x0,u, tau, xn)
[time, dxdt] = ode23(@solve_ode,tspan,x0);
for k = 1:length(dxdt)
J(k,:) = (rand(1)*xn *dxdt).^2 + integral_cost(dxdt, u,xn); % add noise here
end
function dxdt = solve_ode(t,x)
A = 1 + tau/12000;
B = 1 + 0.25 * sin(2*pi*t/3000);
dxdt = A*x + B * u ;
end
function int_J = integral_cost(dxdt, u,xn)
x = dxdt;
integral_J = x.^2 + u.^2;
int_J = trapz(integral_J,xn);
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by