Solving differential equation with varying Constant

조회 수: 4 (최근 30일)
P K
P K 2018년 12월 20일
댓글: Torsten 2018년 12월 20일
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

답변 (1개)

Torsten
Torsten 2018년 12월 20일
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
P K
P K 2018년 12월 20일
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 2018년 12월 20일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by