How do i use ode23 if i have a spline function?

I am working on a program in which we must use ode23 to solve Newton's Law of Cooling where k is a changing value interpolated a a specific time in a data table of tvalues and k values. I have used cubic splines to get k-values but now i need to solve the differential equation dT/dt = k(T-Ta). I am given T and Ta as well. I have written for the spline,
xx = linspace(1, tf);
pp = spline(t,k);
yy = ppval(pp,xx);
what do i need to plug in to get a plottable solution with ode23?

 Accepted Answer

Torsten
Torsten on 1 Apr 2022
Edited: Torsten on 1 Apr 2022
Ta = 273.15 + 10;
fun = @(tt) ppval(spline(t,k),tt);
fun_ode23 = @(t,T) fun(t)*(T-Ta);
T0 = 273.15 + 50;
tspan = [0,1];
[t,T] = ode23(fun_ode23,tspan,T0)
plot(t,T)

2 Comments

Thank you. In the case of this project, the data runs from time 1-10. t from 0 to 1 is constant k=-0.3. t after 10 is constant k=-0.2. i understand if the stopping time is between zero and one i can replace fun(t) with the constant value. do i do the same for after 10 seconds?
Ta = 273.15 + 10;
T0 = 273.15 + 50;
tspan = [0,20];
fun_k = @(tt) ppval(spline(t,k),tt);
[t,T] = ode23(@(t,T)fun_ode23(t,T,Ta,fun_k),tspan,T0);
plot(t,T)
function dTdt = fun_ode23(t,T,Ta,fun_k)
if t <=1
k = -0.3;
elseif t > 1 && t <=10
k = fun_k(t);
else
k = -0.2;
end
dTdt = k*(T-Ta);
end

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Asked:

on 1 Apr 2022

Edited:

on 2 Apr 2022

Community Treasure Hunt

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

Start Hunting!