Solving several ODEs in ONE m-file

Firstly, I made it to solve a ODE with following code.
function ndot = n_t(t,n)
ndot = A*(x*n+a)^2*exp(-B/(x*n+a)*(b-n))*(x+a)/(x+b);
end
[t1,y1]=ode15s(@n_t, [10^-12,1], 0);
A,B,a,b are constant and so is x(=4.5).
But, now, I want to solve not ONE ODE but 61 ODEs with
x = 1.5 : 0.1 : 7.5
Should I make 61 m-files to solve this problem? If not, please let me know. Thank you.

 Accepted Answer

See the documentation section on Passing Extra Parameters
Changing ‘n_t’ to an anonymous function:
n_t = @(t,n,x) A*(x*n+a)^2*exp(-B/(x*n+a)*(b-n))*(x+a)/(x+b);
xv = 1.5 : 0.1 : 7.5;
for k = 1:numel(xv)
[t1{k},y1{k}]=ode15s(@(t,n)n_t(t,n,xv(k)), [10^-12,1], 0);
end
works and produces the 61 cell arrays for ‘t’ and ‘y1’. The cell arrays are necesssary because ‘tspan’ is a range rather than a vector of specific times, so each iteration will be a different length.
I used the anonymous function for convenience. The function version would be:
function ndot = n_t(t,n,x)
ndot = A*(x*n+a)^2*exp(-B/(x*n+a)*(b-n))*(x+a)/(x+b);
end
to produce the same result.
.

2 Comments

Sincerely, thanks for your help!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Tags

Community Treasure Hunt

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

Start Hunting!