Solving differential equation with varying Constant

1 view (last 30 days)
Dimensions
G= 1x100, H=1X100;
I want to solve these 7 eqns. I am using ODE45. I am able to solve these equations for fixed T and Q. But i want to solve it for varying T and Q. Thats why i am using for loop. Can some one explain where am I wrong?. I have tried myself but unable to figure it out.
function [dUdt]=eqn(t,U)
dUdt=zeros(7,1);
K=0.003;
for i=1:100
T=G(1,i);
Q=H(1,i);
end
dUdt(1)=2*K*T(i)*(U(2)-U(1));
dUdt(2)=-2*K*U(2)*T(i);
dUdt(3)=K*T(i)*(2*Q(i)-U(3))-U(3)*K*(U(1)+2*U(2));
dUdt(4)=K*U(3)*T(i)-U(4)*K*(U(1)+2*U(2));
dUdt(5)=K*Q(i)*(U(1)+2*U(2))-2*K*U(5)*T(i);
dUdt(6)=K*U(3)*(U(1)+2*U(2))+2*K*U(5)*T(i)-K*U(6)*T(i);
dUdt(7)=K*U(6)*T(i)+U(4)*K*(U(1)+2*U(2));
end

Answers (1)

Torsten
Torsten on 20 Dec 2018
function [dUdt]=eqn(t,U)
dUdt=zeros(7,1);
K=0.003;
t_inter=0:99;
T_actual=interp1(t_inter,G,t);
Q_actual=interp1(t_inter,H,t);
dUdt(1)=2*K*T_actual*(U(2)-U(1));
dUdt(2)=-2*K*U(2)*T_actual;
dUdt(3)=K*T_actual*(2*Q_actual-U(3))-U(3)*K*(U(1)+2*U(2));
dUdt(4)=K*U(3)*T_actual-U(4)*K*(U(1)+2*U(2));
dUdt(5)=K*Q_actual*(U(1)+2*U(2))-2*K*U(5)*T_actual;
dUdt(6)=K*U(3)*(U(1)+2*U(2))+2*K*U(5)*T_actual-K*U(6)*T_actual;
dUdt(7)=K*U(6)*T_actual+U(4)*K*(U(1)+2*U(2));
end
  8 Comments
P K
P K on 20 Dec 2018
Thank you @All . I tried with G=rand(1,10) and H=rand(1,10). It worked. I would find out why it is not working with MY G AND H.
Torsten
Torsten on 20 Dec 2018
As a quick and dirty solution, add the line
global G H
in "eqn" as well as in the function of your program where you define G and H.

Sign in to comment.

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!