need help with ode45

조회 수: 2 (최근 30일)
Joey
Joey 2014년 4월 30일
편집: Joey 2014년 4월 30일
so im trying to solve some diff eqs (xC, y, and w) and thus far i have:
% with xE=0 (no elites): egalitarian society
% xC(0) = 100 , xE(0) = 0 , y(0) = l , w(0) = 0
d = 6.67e-6; %depletion rate
am = .01; %normal (min) death rate
aM = .07; %famine (max) death rate
bC = .03; %commoner birth rate
bE = .03; %elite brith rate
s = .0005; %subsistence salary per capita
p = .005; %threshold wealth per capita
g = .01; %regeneration rate of nature
l = 100; %nature carrying capacity
k = 1; %inequality factor
n = (aM-bC)/(aM-am);
XM = (g*(l/2)^2)/(n*s); % max carry capacity
options = odeset('RelTol',1e-4);
[t, xC] = ode45('xC3',[0 1000],100,options);
[t, y] = ode45('y3a',[0 1000],l,options);
[t, w] = ode45('w3a',[0 1000],0,options);
CC = min(1,w./wth)*s.*xC; % consumption rate commoners
CE = min(1,w./wth)*k.*s.*xE; % consumption rate elite
wth = p.*xC+k*p.*xE; % wealth threshold
aC = am+max(0,1-CC/(s.*xC)).*(aM-am); % death rate commoners
aE = am+max(0,1-CC/(s.*xE)).*(aM-am); % death rate elite
and my functions for the ode45 are:
function dxC = xC3(t,xC)
dxC = bC.*xC-aC.*xC; % commoner population
end
function dy = y3a(t,y)
dy = g.*y.*(l-y)-d.*xC.*y; % natural resources
end
function dw = w3a(t,w)
dw = d.*xC.*y-CC-CE; % accumulated wealth
end
I'm getting these errors:
Undefined function or variable 'bC'.
Error in xC3 (line 2)
dxC = bC.*xC-aC.*xC; % commoner
population
Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); % ODE15I
sets args{1} to yp0.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir,
y0, f0, odeArgs, odeFcn, ...
Error in fig3a (line 22)
[t, xC] = ode45('xC3',[0
1000],100,options);
I don't know why it says bC is undefined because I clearly define it, and I don't know what the other errors mean.
  댓글 수: 1
Joey
Joey 2014년 4월 30일
I know it's alot to look at, but ANY help is appreciated

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

답변 (2개)

ragesh r menon
ragesh r menon 2014년 4월 30일
The problem here is that the scope of variable "bC" is outside the definition of function. Either define them locally by defining within the function xC3 or make them global by declaring
global bC ..............
  댓글 수: 2
Joey
Joey 2014년 4월 30일
i globalized every variable but it still gave me the same errors
Jan
Jan 2014년 4월 30일
@joey: In the code you have posted, bC is not defined as a global inside e.g. the function xC3. So what does "I've globalized every variable" exactly mean?

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


ragesh r menon
ragesh r menon 2014년 4월 30일
See, you are not defining aC in the function xC3 and this aC is again dependent on the state xC
aC = am+max(0,1-CC/(s.*xC)).*(aM-am); % death rate commoners
Instead of writing 3 separate function write them in one because dXc and dy are depedent on xC
function ... = xC3(t,x1,x2)
CC = ....; % consumption rate commoners
CE = ....; % consumption rate elite
wth =....; % wealth threshold
aC =..... % death rate commoners
aE = ....; % death rate elite
dxC = bC.*x1-aC.*x1; % commoner population
dy = g.*x2.*(l-x2)-d.*x1.*x2; % natural resources
dw = d.*x1.*x2-CC-CE; % accumulated wealth
end
I just copied and pasted your code..Modify accordingly.
  댓글 수: 3
Jan
Jan 2014년 4월 30일
@Joey: Exactly. Simply try it and read doc ode45.
Joey
Joey 2014년 4월 30일
편집: Joey 2014년 4월 30일
heres what it says now:
Undefined function or variable 'p'.
Error in test (line 3)
wth = p.*z(1)+k*p.*xE; % wealth threshold
Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

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

카테고리

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