need help with ode45

1 view (last 30 days)
Joey
Joey on 30 Apr 2014
Edited: Joey on 30 Apr 2014
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 Comment
Joey
Joey on 30 Apr 2014
I know it's alot to look at, but ANY help is appreciated

Sign in to comment.

Answers (2)

ragesh r menon
ragesh r menon on 30 Apr 2014
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 Comments
Joey
Joey on 30 Apr 2014
i globalized every variable but it still gave me the same errors
Jan
Jan on 30 Apr 2014
@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?

Sign in to comment.


ragesh r menon
ragesh r menon on 30 Apr 2014
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 Comments
Jan
Jan on 30 Apr 2014
@Joey: Exactly. Simply try it and read doc ode45.
Joey
Joey on 30 Apr 2014
Edited: Joey on 30 Apr 2014
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, ...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!