MATLAB: I am trying to answer the following word problem and I am running in to issues developing a function

differential equationsode45

Transcribing this Differential equation is proving difficult for me: dP/dt = (2-0.1t)P
if P(0) = 1000, find a population at t = 5.
Doing this manually is quite easy. My answer is P(5) = 6.31 x 10^6.
Doing it in Matlab is challenging because I am not well versed in it.
My function in 'func1.m': function dP = func1(t, C) dP(1)=C*exp(2*(t)-0.05*(t)^2);
My call: C =1000; t0 = 5; y0 = 0; tspan = [0 100]; [t, C] = ode45('func1', tspan, t0, y0, pyargs()); plot(t, C)
Any assistance would be greatly appreciated.

Best Answer

dP/dt = (2-0.1t)P as code:
function dP = finc1(t, P)
dP = (2 - 0.1 * t) * P;
end
And the function to integrate it:
P0 = 1000;
t0 = 0;
tEnd = 5;
tspan = [t0, tEnd];
[t, P] = ode45(@func1, tspan, P0);
plot(t, P)
Provide the function to be integrated as function handle with a leading @..., not as string. The latter works for backward compatibility with R5.3, such the it is outdated for almost 20 years now.
Related Question