Hi I am trying to solve two coupled ODEs simultaneously.
My equations are:
dT1/dz = -U*a1/(p1*q1*Cp1*L)*(T1 - T2) - Q*a1/(p1*Cp1)
dT2/dz = U*a2/(p2*Cp2*q2*L)*(T1 - T2)
I have tried programming Matlab in several different ways this is the code for the one I think I am the closest to solving.
function [z, T] = Term_project2
U= 280;
a=2*.45*3.14159;
p1= 0.5;
L= 8000;
Cp1= 10;
Q=284;
p2= 1;
Cp2= 15;
q1= 0.5;
q2= 1;
zspan=[0:40];
T0= [12 5];
dT(1) = -(U*a)/(L*p1*Cp1*q1)*(T1(z)-T2(z))-Q/(p1*Cp1);
dT(2) = (U*a)/(L*p2*Cp2*q2)*(T1(z)-T2(z))-Q/(p2*Cp2);
[z,T]= ode45(Term_project2(z,p1,p2,q1,q2,U,Q,Cp1,Cp2L),zspan,T0);
plot(z,T1(:,1),'-o',z,T2(:,2),'-.')
end
Errors: Unrecognized function or variable 't'.
Which doesn't make sense since I don't have t.

 채택된 답변

James Tursa
James Tursa 2021년 4월 29일
편집: James Tursa 2021년 4월 29일

0 개 추천

The derivative function needs to have the input arguments specified. In your current code above, you have no inputs specified.
For the ode45 call, you need to pass a function handle that has only two inputs, the independent variable and the current state.
So your derivative function should look something like this:
function dTdz = Term_project2(z,T,p1,p2,q1,q2,U,Q,Cp1,Cp2L) % pass in all needed inputs
dTdz(1,1) = -(U*a)/(L*p1*Cp1*q1)*(T(1)-T(2))-Q/(p1*Cp1); % use T(1) and T(2) for T1 and T2
dTdz(2,1) = (U*a)/(L*p2*Cp2*q2)*(T(1)-T(2))-Q/(p2*Cp2);
end
And your calling code should look something like this:
U= 280;
a=2*.45*3.14159;
p1= 0.5;
L= 8000;
Cp1= 10;
Q=284;
p2= 1;
Cp2= 15;
q1= 0.5;
q2= 1;
zspan=[0:40];
T0= [12 5];
% 1st argument to ode45 below needs to be a function handle
% that takes z and T as inputs
[z,T]= ode45(@(z,T)Term_project2(z,T,p1,p2,q1,q2,U,Q,Cp1,Cp2L),zspan,T0);
plot(z,T(:,1),'-o',z,T(:,2),'-.')

댓글 수: 3

Thank you so much for your help. This is how I have it written in Matlab, but I am told that there is not enough input arguments.
function dTdz = Term_project_soln(z,T,p1,p2,q1,q2,U,Q,Cp1,Cp2,L,a) % pass in all needed inputs
U= 280;
a=2*0.45*3.14159;
p1= 1.2041;
L= 1000;
Cp1= 10;
Q=284;
p2= 2400;
Cp2= 3962;
q1= (3.5*a);
q2= (0.1/1000);
zspan=[0:40];
T0= [12 5];
% 1st argument to ode45 below needs to be a function handle
% that takes z and T as inputs
dT(1,1) = -(U*a)/(L*p1*Cp1*q1)*(T(1)-T(2))-Q/(p1*Cp1); % use T(1) and T(2) for T1 and T2
dT(2,1) = (U*a)/(L*p2*Cp2*q2)*(T(1)-T(2))-Q/(p2*Cp2);
[z,T]= ode45(@(z,T)Term_project_soln(z,T,p1,p2,q1,q2,U,Q,Cp1,Cp2,L,a),zspan,T0);
plot(z,T(:,1),'-o',z,T(:,2),'-.')
end
James Tursa
James Tursa 2021년 4월 29일
You still have all your code mixed together. The function needs to be only the four lines I have written above. Nothing else. You could put that in a file called Term_project_soln.m
Then have your other code in a separate script file that sets the constants, calls ode45, and does the plotting.
Alyssa Garreau
Alyssa Garreau 2021년 4월 29일
Thank you.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

질문:

2021년 4월 28일

댓글:

2021년 4월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by