How to call multiple functions in ode 45?

조회 수: 18 (최근 30일)
gorilla3
gorilla3 2017년 11월 14일
댓글: gorilla3 2017년 11월 23일
I'm trying to solve 2 systems of differential equations using ode45, however I get the error code:
Error using odearguments (line 21) When the first argument to ode45 is a function handle, the tspan argument must have at least two elements.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in CBF_2006_f_v1 (line 49)
[ABP,P1,P2,xq,xm,xc,xm1,Cav] = ode45(@vsa, @aut, tspan, var0);
There are 2 systems: 4 equations each. The unknowns are (V_sa,P1,P2,xq,xm,xc,xm1,Cav), all the rest are parameters that have been declared at the beginning of the code.
The code main code that recalls the functions is:
%Parameter declaration .... (Rsa,V_sa_b,P_ic ...)
%%Diff eq system
ABP= linspace(40,170,131);
delta=deltaCa_p;
for i=1:1:length(ABP)
tspan=[0 100];
var0=[12; 97.6; 49.67; 0; 0; 0; 0; 0.205];
[ABP,P1,P2,xq,xm,xc,xm1,Cav] = ode45(@vsa, @aut, tspan, var0);
end
The 2 functions are stored in separate files and their code is:
function dvdt = vsa(ABP,var,delta,R_la,R_sa_b,V_sa_b,R_sv,P_ic,P_v,k_ven,P_v1)
dvdt = [(0.5*delta*(1- ((cosh(4*(var(6)+var(5)-var(4))/delta))-1)/(cosh(4*(var(6)+var(5)-var(4))) +1)))*(var(2)-P_ic) + var(8)*(( (ABP(i) -var(2))/(R_la+0.5*(R_sa_b*(V_sa_b/var(1))^2)) - (var(2)-var(3))/(R_sv+0.5*(R_sa_b*(V_sa_b/var(1))^2)) - (var(2)-P_ic)*(0.5*delta*(1- ((cosh(4*(var(6)+var(5)-var(4))/delta))-1)/(cosh(4*(var(6)+var(5)-var(4))) +1)))) /var(8)) ;
( (ABP(i) -var(2))/(R_la+0.5*(R_sa_b*(V_sa_b/var(1))^2)) - (var(2)-var(3))/(R_sv+0.5*(R_sa_b*(V_sa_b/var(1))^2)) - (var(2)-P_ic)*(0.5*delta*(1- ((cosh(4*(var(6)+var(5)-var(4))/delta))-1)/(cosh(4*(var(6)+var(5)-var(4))) +1)))) /var(8);
((var(2)-var(3))/(0.5 *(R_sa_b*(V_sa_b/var(1))^2) +R_sv)-(var(3)-P_v)/R_lv)*1/(1/(k_ven*(var(3)-P_ic-P_v1))) ;
0.5*delta*(1- ((cosh(4*(var(6)+var(5)-var(4))/delta))-1)/(cosh(4*(var(6)+var(5)-var(4))) +1))] ;
V_sa= var(:,1);
P1= var(:,2);
P2 = var(:,3);
Cav=var(:,8);
xq= var(:,4);
xc= var(:,5);
xm= var(:,6);
xm1= var(:,7);
dvdt=[V_sa;P1;P2;Cav; xq;xc;xm;xm1;Cav];
end
and
function dxdt= aut(ABP,var,G_q,R_sa_b,V_sa_b,Pa_co2,Pa_co2_b,tau_co2,eps,u,tau2,tau1)
dxdt=[ (-var(4)+G_q*( ( (var(2)- var(3))/(0.5 *(R_sa_b*(V_sa_b/var(1))^2) + R_sv) ) -q_b /q_b) )/tau_q ;
(-var(5) +0.3+3*tanh(Pa_co2/Pa_co2_b -1.1))/tau_co2;
var(7);
(eps*u-tau2*var(7)-var(6))/tau1^2 ];
V_sa= var(1);
P1= var(2);
P2 = var(3);
xq= var(4);
xc= var(5);
xm= var(6);
xm1= var(7);
dxdt=[P1;P2;xq;xc;xm;xm1];
end
Could you please help me find the mistakes?

채택된 답변

Star Strider
Star Strider 2017년 11월 14일
I would nest them into one function:
bothfcns = @(ABP, var) [@(ABP,var) vsa(ABP,var,delta,R_la,R_sa_b,V_sa_b,R_sv,P_ic,P_v,k_ven,P_v1); @(ABP,var) aut(ABP,var,G_q,R_sa_b,V_sa_b,Pa_co2,Pa_co2_b,tau_co2,eps,u,tau2,tau1)];
Then use ‘bothfcns’ as the function argument to ode45.
NOTE I cannot run your code to test this, so I am listing this as UNTESTED CODE.
  댓글 수: 29
Torsten
Torsten 2017년 11월 23일
If you save the complete code in one file, the first part also has to be included in a function. So start your file with
function main
ABP= linspace(40,170,131);
for i=1:length(ABP)
[T,Y] = ode45(@(t,y)ABP(t,y,ABP(i)),[0 100], [12 97.6 49.67 0.205 0 0 0 0]);
end
function dvdt = first(t,par,abp)
...
and name your file "main.m".
Best wishes
Torsten.
gorilla3
gorilla3 2017년 11월 23일
Doing that gives me the error: "unable to define function 'main' because it has the same name as the file"

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by