How to have Ode45 display an answer to an array input?

Say I have an ode dx/dt=x^n - x, where n=[1 2 3 4 5] How do I get the ode solver to display an output for every value in the array ‘n’? I was successfully able to solve the ode for a single n value using the m-file function, but all must be included, and I can’t figure out how to do that. Thanks!

Answers (1)

I changed the ODE, but the way to solve an equation for several parameters should become clear.
function main
nt = 11;
a = 1;
b = 2;
tspan = linspace(a,b,nt)
x0 = 1;
n = [1 2 3 4 5];
X = zeros(numel(n),nt);
T = tspan;
for i=1:numel(n)
n_actual = n(i);
fun = @(t,x) 1/x^n_actual;
[t,x] = ode15s(fun,tspan,x0);
X(i,:) = x;
end
plot(T,X)
end
Best wishes
Torsten.

Asked:

on 29 Aug 2018

Edited:

on 30 Aug 2018

Community Treasure Hunt

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

Start Hunting!