MATLAB: Using Ode45 to solve differential equation with time dependent variable

deMATLABodeode45

Hi my name is Hidde and i am a first year IEM student,
I got an assignment where a part of it is to solve 2 differential equations and plot them in a graph. I just keep getting error and i cannot get the code to work.
I have some experience with matlab but i never used Ode45 before. I've read numerous tutorials and watched a bunch of video's but i really need help. Tamb is time dependant and changes every second.
OdeScript
% script for solving an ode45
clear all
clc
% define constants
R1 = 0.4;
R2 = 0.3;
Rwin = 0.5;
Cint = 1.0 * 10^4;
Cwall = 5.0 * 10^2;
Tamb = dlmread('Team_82.dat');
A = 1 / (Cwall*R2);
B = 1 / (Cwall*R1);
C = 1 / (Cint*R1);
D = 1 / (Cint*Rwin);
% time dependent window
tspan = linspace(0,1440,1441); % calculate from t=0 up to t=1440
y0 = [18 20]; % initial conditions
% define your ode-function
odefunc = @(t,dTempdt) odeFunction1(t,Twall, Tint, Tamb, A, B, C, D);
[t,dTempdt] = ode45(odefunc, tspan, y0);
% plot the results
plot(t,dTempdt)
OdeFunction
function [dTempdt] = odeFunction1(t,Twall, Tint, Tamb, A, B, C, D)
% set of ordinary differential equations
% input: A - constant
% B - constant
% C - constant
% D - constant
% t - the time variable
% Tint - state variable
% Twall - state variable
%output: dTempdt - the set of equations
% initialize the set of equations
dTempdt = zeros(2,1);
% define the set of equations
dTempdt(1) = A*Tamb - A*Twall + B*Tint - B*Twall;
dTempdt(2) = C*Twall - C*Tint + D*Tamb - D*Tint;
end
I am getting the folowing errors:
Undefined function or variable 'Twall'.
Error in odeScript1>@(t,dTempdt)odeFunction1(t,Twall,Tint,Tamb,A,B,C,D)
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in odeScript1 (line 31)
[t,dTempdt] = ode45(odefunc, tspan, y0);
Any help would be greatly appreciated!

Best Answer

% call the nested function
[t,dTempdt] = my_assignment;
% plot the results
plot(t,dTempdt)
function [t,dTempdt] = my_assignment
% define constants
R1 = 0.4;
R2 = 0.3;
Rwin = 0.5;
Cint = 1.0 * 10^4;
Cwall = 5.0 * 10^2;
Tamb = dlmread('Team_82.dat');
A = 1 / (Cwall*R2);
B = 1 / (Cwall*R1);
C = 1 / (Cint*R1);
D = 1 / (Cint*Rwin);
% time dependent window
tspan = linspace(0,1440,1441); % calculate from t=0 up to t=1440
y0 = [18 20]; % initial conditions
% define your ode-function
odefunc = @odeFunction1;
[t,dTempdt] = ode45(odefunc, tspan, y0);
function dTempdt = odeFunction1(t,y)
% set of ordinary differential equations
% input: A - constant
% B - constant
% C - constant
% D - constant
% t - the time variable
% Tint - state variable
% Twall - state variable
%output: dTempdt - the set of equations
% initialize the set of equations
Tamb_t = interp1(tspan,Tamb,t);
Twall = y(1);
Tint = y(2);
dTempdt = zeros(2,1);
% define the set of equations
dTempdt(1) = A*Tamb_t - A*Twall + B.*Tint(:,1) - B.*Twall;
dTempdt(2) = C*Twall - C*Tint + D*Tamb_t - D*Tint;
end
end