How can I use odeset and some variable parameters within ode45?

I would like to solve the following ODE function:
*******************
function dc=reaction(t,c,s,q,w) % c is concentartion variable for all three substances.
dc = zeros(3,1); % a column vector
dc(1)=s*(c(2)-(c(2)*c(1))+c(1)-q*(c(1)^2));
dc(2)=(-c(2)-c(1)*c(2)+c(3))/s;
dc(3)=(c(1)-c(3))*w;
****************
where s, q and w are variables that could be changed / defined in the main code.
*******************
clc,clf,
c0=[25.00 1.000 20.00]; % initial values
tspan=[0 15]; % time range
s=1; q=1; w=0.2; % parameters
options = odeset('RelTol',1e-8);
[t,c] = ode45(@reaction,tspan,c0,options,[],s,q,w);
%[t,c] = ode45(@reaction,tspan,c0,[],s,q,w,options);
Non of the above ode45 syntaxes work and I get an error message saying:
??? Error using ==> reaction
Too many input arguments.
Error in ==> odearguments at 110
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in ==> concentration_mh2 at 21
[t,c] = ode45(@reaction,tspan,c0,[],s,q,w,options);
I wonder what is the correct syntax in this case, I even tried evoking the reaction function by an anonymous function in the main code but it did not work fro me!
I appreciate if one can suggest me the right approach.

 Accepted Answer

[t,c] = ode45(@(t,c) reaction(t,c,s,q,w),tspan,c0,options);

3 Comments

Thanks! It works without any error but still can not see any change in the results by decreasing RelTol value. It means something is wrong while I am expecting to get more discrete solution points when the RelTol is decreased (e.g. from 1e-3 to 1e-8)
If you want more samples, use
options = odeset('MaxStep',1e-2);

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!