フィルターのクリア

How to solve ODE for varying input using ode23 ?

2 ビュー (過去 30 日間)
Md Shahidul Alam
Md Shahidul Alam 2016 年 8 月 25 日
コメント済み: Torsten 2016 年 8 月 26 日
I'm trying to solve a RC circuit using ode23. For my case the input is varying with time. Here u is my input vector.
% function
function vdot = diff3(t,v)
u = [1 2 3 4 5] ;
vdot(1) = -40*v(1) + 20*v(2) + 20*u(t);
vdot(2) = 20*v(1) - 20*v(2) - v(3);
vdot(3) = 0.1*v(2) -1000*v(3);
vdot = [vdot(1);vdot(2);vdot(3)];
end
% main program
tspan = 1:5;
x0 = [0 0 0]; % initial conditions
[t,x] = ode23('diff3', tspan, x0);
tt = length(t);
for i = 1:tt
vo(i) = x(i,1) - x(i,2);
end
plot(t, vo)
title('Transient analysis of RLC')
xlabel('Time, s'), ylabel('Output voltage')
In case of fixed value of u = 5, there is no error. As my input is varying MATLAB is throwing some error. _
Attempted to access u(1); index must be a positive integer or logical.
Error in diff3 (line 9)
vdot(1) = -40*v(1) + 20*v(2) + 20*u(t);
Error in ode23 (line 256)
f(:,2) = feval(odeFcn,t+h*A(1),y+f*hB(:,1),odeArgs{:});
Error in rcckt (line 5)
[t,x] = ode23('diff3', tspan, x0);_
So how can I solve ODE for varying input? Any kind of suggestion will be really helpful. Thanks in advance!

採用された回答

Torsten
Torsten 2016 年 8 月 25 日
% function
function vdot = diff3(t,v)
u = [1 2 3 4 5] ;
tu = [1 2 3 4 5];
u_at_t = interp1(tu,u,t);
vdot(1) = -40*v(1) + 20*v(2) + 20*u_at_t;
vdot(2) = 20*v(1) - 20*v(2) - v(3);
vdot(3) = 0.1*v(2) -1000*v(3);
vdot = [vdot(1);vdot(2);vdot(3)];
end
Best wishes
Torsten.
  2 件のコメント
Md Shahidul Alam
Md Shahidul Alam 2016 年 8 月 25 日
Thank you very much for your kind reply and solution. I changed it little bit to access different state input.
function vdot = diff3(t,v)
u = [1 2 3 4 5;
6 7 8 9 10;
11 12 13 14 15] ;
tu = [1 2 3 4 5];
u_at_t(1,:) = interp1(tu,u(1,:),t);
u_at_t(2,:) = interp1(tu,u(2,:),t);
u_at_t(3,:) = interp1(tu,u(3,:),t);
vdot(1) = -40*v(1) + 20*v(2) + 20*u_at_t(1,:);
vdot(2) = 20*v(1) - 20*v(2) - v(3) + u_at_t(2,:);
vdot(3) = 0.1*v(2) -1000*v(3) + u_at_t(3,:);
vdot = [vdot(1);vdot(2);vdot(3)];
end
Torsten
Torsten 2016 年 8 月 26 日
Since interp1 only returns a scalar in each case,
function vdot = diff3(t,v)
u = [1 2 3 4 5;
6 7 8 9 10;
11 12 13 14 15] ;
tu = [1 2 3 4 5];
u_at_t(1) = interp1(tu,u(1,:),t);
u_at_t(2) = interp1(tu,u(2,:),t);
u_at_t(3) = interp1(tu,u(3,:),t);
vdot(1) = -40*v(1) + 20*v(2) + 20*u_at_t(1);
vdot(2) = 20*v(1) - 20*v(2) - v(3) + u_at_t(2);
vdot(3) = 0.1*v(2) -1000*v(3) + u_at_t(3);
vdot = [vdot(1);vdot(2);vdot(3)];
end
also works.
Best wishes
Torsten.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by