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.

 采纳的回答

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.
.

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by