Solving a system of ODE
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello,
How can I solve a system of two ODE's as follows,
dNcdt= Nc(t)*Kgr- dc*Nc(t)*Ni(t)
dNidt= ai*Nc(t) - di*Ni(t)
To obtain Nc(t) and Ni(t).
Thanks in advance.
채택된 답변
Star Strider
2017년 2월 14일
0 개 추천
It is easier to let the Symbolic Math Toolbox do the algebra:
syms ai dc di Kgr Nc(t) Ni(t) t Y
Eq1 = diff(Nc) == Nc(t)*Kgr- dc*Nc(t)*Ni(t);
Eq2 = diff(Ni) == ai*Nc(t) - di*Ni(t);
[odesys, vrs] = odeToVectorField(Eq1, Eq2);
odefcn = matlabFunction(odesys, 'Vars',[t Y ai dc di Kgr])
odefcn =
function_handle with value:
@(t,Y,ai,dc,di,Kgr)[ai.*Y(2)-di.*Y(1);Kgr.*Y(2)-dc.*Y(1).*Y(2)]
The rest should be relatively straightforward for you to complete. To solve it numerically, begin with ode45, and if your equation turns out to be ‘stiff’ because of a wide variation in parameter magnitudes, and ode45 has problems, use ode15s or one of the other stiff solvers appropriate to your system.
댓글 수: 8
Thanks for your answer but I can not understand what Y means?
I really can not understand the code or how to complete the solution.
My problem is to solve the following ODE:
dqpldt=fc*Rc*Nc(t)+ fi*Ri*Ni(t)+fch*Rch*Nch0+ fih*Rih*Nih0-Ke*qpl(t) ;
So I need to get Nc(t) and Ni(t) to substitute in the model equation in order to solve it analytically and get qpl(t).
Star Strider
2017년 2월 14일
My pleasure.
I probably should have been a bit more explicit. The ‘vrs’ output of the odeToVectorField call, indicates that ‘Y(1)=vrs(1)’ and ‘Y(2)=vrs(2)’, so ‘Y(1)=Ni’ and ‘Y(2)=Nc’.
An analytic solution may not be possible, since your equation is nonlinear, and only a few nonlinear equations have analytic solutions. When I use dsolve:
NiNc = dsolve(Eq1, Eq2, Nc(0) == Nc0, Ni(0) == Ni0)
the result is:
Warning: Explicit solution could not be found.
> In dsolve (line 201)
NiNc =
[ empty sym ]
You could use other solvers, perhaps Wolfram Alpha (free online) or Maple, but I would be surprised if you could get an analytic solution. I could not get one using Wolfram Alpha with your system (link).
Thanks a lot.
What if I try to solve the model equation numerically or try to graph the solution?
I mean if I have the values of the parameters in the following equation:
dqpldt=fc*Rc*Nc(t)+ fi*Ri*Ni(t)+fch*Rch*Nch0+ fih*Rih*Nih0-Ke*qpl(t) ;
The parameters are:
fc; Rc; Nc0; fi*Ri; Ni0; fch*Rch*Nch0; fih*Rih*Nih0; Ke; ai; di; dc; Kgr
Can I solve the equation numerically/ or graph the solution while I do not have Nc(t) or Ni(t) but I only have dNc/dt and dNi/dt?
Star Strider
2017년 2월 14일
My pleasure.
You have defined more parameters than in your original system of differential equations, so I will let you decide how best to pass them to odefcn in my initial Answer. If you have a new equation or set of equations, you will have to use the Symbolic Math Toolbox to create the vector field and MATLAB anonymous function to use in your numerical integration code.
You can easily solve it numerically with one of the ODE solvers. Define your parameters with their values, then the ODE function call would be:
odefcn = @(t,Y,ai,dc,di,Kgr) [ai.*Y(2)-di.*Y(1); Kgr.*Y(2)-dc.*Y(1).*Y(2)];
ai = ...;
dc = ...;
di = ...;
Kgr = ...;
tspan = [0 10];
initcond = [Nc0 Ni0];
[t,y] = ode45(@(t,Y) odefcn(t,Y,ai,dc,di,Kgr), tspan, initcond);
figure(1)
plot(t, y)
grid
xlabel('Time')
ylabel('N')
legend('Ni(t)', 'Nc(t)')
Choose the appropriate values for ‘tspan’ (time span or time vector for the integration).
Try ode45 first. As I mentioned, you may need a stiff solver such as ode15s rather than ode45 if ode45 seems to ‘get stuck’ and take more than a few minutes to integrate your equations.
Esraa Abdelkhaleq
2017년 2월 15일
편집: Esraa Abdelkhaleq
2017년 2월 15일
The parameters are for the full model equation (dqpl/dt) in addition to the ODE system (dNc/dt, dNi/dt).
When I try to run the code you wrote in your comment, an error appears:
Error using odearguments (line 110)
Inputs must be floats, namely single or double.
Error in ode45 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in Untitled5 (line 11)
[t,y] = ode45(@(t,Y) odefcn(t,Y,ai,dc,di,Kgr), tspan, initcond);
In my previous comment, I did not want to solve the ODE system numerically but the full model equation (dqpl/dt):
dqpldt=fc*Rc*Nc(t)+ fi*Ri*Ni(t)+fch*Rch*Nch0+ fih*Rih*Nih0-Ke*qpl(t) ;
But I do not have Nc(t) or Ni(t), I have instead dNc/dt and dNi/dt. So I wondered If I can write a code including the ODE system (dNc/dt and dNi/dt) together with the full model equation (dqpl/dt) and try to at least graph the solution for qpl(t).
I wish I could explain my problem.
Thanks for your time.
Star Strider
2017년 2월 15일
My pleasure.
I have no idea what could be throwing that error. Be sure that the parameters you are passing to ‘odefcn’ are numeric single- or double-precision values, and not symbolic.
Analytic solutions do not exist for ‘Nc(t)’ and ‘Ni(t)’, so you cannot get an analytic solution for ‘qpl(t)’ in your differential equation. You have to include it as the third differential equation in your system of differential equations, just as I did for the first two, then use the Symbolic Math Toolbox to create a function from it that the numeric ODE solvers can use. That should work.
Esraa Abdelkhaleq
2017년 2월 15일
OK. Thanks a lot for your help.
Star Strider
2017년 2월 15일
My pleasure.
Out doing stuff for 3 hours (life intrudes) so just got back to this topic.
This code:
syms ai dc di fc fch fi fih Ke Kgr Nc(t) Ni(t) Rc Rch Ri Rih t Y Nc0 Nch0 Nih0 Ni0 qpl(t)
Eq1 = diff(Nc) == Nc(t)*Kgr- dc*Nc(t)*Ni(t);
Eq2 = diff(Ni) == ai*Nc(t) - di*Ni(t);
Eq3 = diff(qpl) == fc*Rc*Nc(t)+ fi*Ri*Ni(t)+fch*Rch*Nch0+ fih*Rih*Nih0-Ke*qpl(t) ;
[odesys, vrs] = odeToVectorField(Eq1, Eq2, Eq3)
odefcn = matlabFunction(odesys, 'Vars',[t Y ai dc di fc fch fi fih Ke Kgr Nc0 Nch0 Nih0 Ni0 Rc Rch Ri Rih])
produces:
odesys =
ai*Y[2] - di*Y[1]
Kgr*Y[2] - dc*Y[1]*Y[2]
Nch0*Rch*fch - Ke*Y[3] + Nih0*Rih*fih + Rc*fc*Y[2] + Ri*fi*Y[1]
vrs =
Ni
Nc
qpl
odefcn = @(t,Y,ai,dc,di,fc,fch,fi,fih,Ke,Kgr,Nc0,Nch0,Nih0,Ni0,Rc,Rch,Ri,Rih) [ai.*Y(2)-di.*Y(1);Kgr.*Y(2)-dc.*Y(1).*Y(2);-Ke.*Y(3)+Nch0.*Rch.*fch+Nih0.*Rih.*fih+Rc.*fc.*Y(2)+Ri.*fi.*Y(1)];
The ‘vrs’ output you can interpret as:
Y(1) = Ni
Y(2) = Nc
Y(3) = qpl
You have to supply all the values for the parameters, so you can then integrate it with ode45 or ode15s (or other solver, depending on your requirements).
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Common Operations에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
