필터 지우기
필터 지우기

Getting an error solving differential equation "free variable must be included in vars"

조회 수: 12 (최근 30일)
Hi. I am trying to solve the equations : du/dt = k1*s - k2*u*v dv/dt = k3*s - k4*v
where s = floor(t/4)
Now i wrote the code :
syms T t u(t) v(t) u0 v0 Y
Du = diff(u);
Dv = diff(v); k1=1;k2=1;k3=1;k4=1; s=floor(t/4);
ode1 = Du == k1*s-k2*u*v;
ode2 = Dv == k3*s-k4*v;
[ode_vf, ode_subs] = odeToVectorField(ode1,ode2);
ode_fcn = matlabFunction(ode_vf, 'vars',{T,Y});
tspan = linspace(0, 20, 10);
icv = [0; 0]+sqrt(eps);
[t,y] = ode45(ode_fcn, tspan, icv);
figure(1)
plot(t, y)
grid
But i am getting the error :
Error using sym/matlabFunction>checkVarsSubset (line 234)
The free variable t must be included in the 'Vars' value.
>>> Error in sym/matlabFunction>checkVars (line 222) checkVarsSubset(vexpanded,funvars);
>>>Error in sym/matlabFunction (line 154) vars = checkVars(funvars,opts);
Please let me know what to do here !!1

채택된 답변

Walter Roberson
Walter Roberson 2017년 6월 25일
You have
ode_fcn = matlabFunction(ode_vf, 'vars',{T,Y});
but your ode_vf does not include any variable named T; it includes a variable named t (lower-case)
ode_fcn = matlabFunction(ode_vf, 'vars',{t,Y});
  댓글 수: 5
siddharth tripathi
siddharth tripathi 2017년 6월 25일
Thanks a lot. I was making a really silly mistake.
Can you please let me know how to plot "s" on the same graph ?
Hold on plot(t,s) doesn't work !

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by