Solving differential equation with varying Constant
Show older comments
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
on 20 Dec 2018
1 vote
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
Torsten
on 20 Dec 2018
If you can use G in your file, I see no reason why you shouldn't be able to use it in the modified file.
P K
on 20 Dec 2018
madhan ravi
on 20 Dec 2018
+1 works perfectly!
P K
on 20 Dec 2018
madhan ravi
on 20 Dec 2018
tspan=[...];
initial_conditions=[...];
[t,x]=ode45(@eqn,tspan,initial_conditions);
plot(t,x(:,1)) % to plot the solutions
function [dUdt]=eqn(t,U)
G=....your values;
H=....your values;
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
P K
on 20 Dec 2018
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.
Categories
Find more on Loops and Conditional Statements 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!