ode45 output incorrect - possible incorrect function definition?

조회 수: 1 (최근 30일)
Jess
Jess 2014년 5월 25일
답변: Star Strider 2014년 5월 25일
I have two functions, one to define the differential equation and one to perform ode45 and solve the equation numerically. However, ode45 seems to give a very suspicious output which after solving the equation by hand I can see clearly that it is incorrect. I feel that I have somehow defined the equation incorrect but cannot see how. If possible could someone just have a once over the code to see if I am missing something anywhere, thanks.
Function to define the differential equation:
function dndt = productdiffeqn(t,n,par)
a = par(1);
m = par(2);
c = par(3);
dndt = ((a*10*(c-n))/(m+c-n));
end
Function to solve it:
function ndata = productsol(par, tyear)
n0 = 0;
[tdata,ndata] = ode45(@productdiffeqn,[tyear],n0,[],[par]);
tdata
end
Called with:
productsol([1,1,1],[tyear])
tdata =
0
1
2
3
4
6
12
ans =
0
1.0007
0.9995
1.0000
0.9991
0.9994
0.9999
  댓글 수: 2
Star Strider
Star Strider 2014년 5월 25일
What differential equation did you start with?
We need to know that and other details of what you’re doing before we can figure out what’s wrong.
Jess
Jess 2014년 5월 25일
편집: Jess 2014년 5월 25일
dn/dt = (10a(c-n))/(m+c-n)
Constants a,c,m are all >0. Initial value of n: n(0) = 0.

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

답변 (1개)

Star Strider
Star Strider 2014년 5월 25일
The constants a, c, and m all have to be in your workspace, but you can solve your ODE with an anonymous function:
c = 1;
m = 5;
a = 7;
productdiffeqn = @(t,n) (10.*a.*(c-n))./(m+c-n);
tyear = [0 1 2 3 4 6 12];
n0 = 0;
[t, n] = ode45(productdiffeqn, tyear, n0);
figure(1)
plot(t, n)
grid
I don’t know what your constants are, but this looks similar to what you posted.

카테고리

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