Asked by Hidde Kemperink
on 25 Oct 2019

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!

Answer by Stephan
on 25 Oct 2019

Edited by Stephan
on 25 Oct 2019

Accepted 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

Hidde Kemperink
on 25 Oct 2019

Hi stefan!

Thanks for your answer but it is mandatory for us to use ode45 not ode1.

Stephan
on 25 Oct 2019

Sign in to comment.

Answer by Daniel
on 25 Oct 2019

Edited by Daniel
on 25 Oct 2019

Hi Hidde,

I composed one functioning script from all the comments in order to not confuse you.

Hope this works! ;)

Cheers,

Daniel

%% Import Data

opts = delimitedTextImportOptions("NumVariables", 1);

opts.DataLines = [1, Inf];

opts.Delimiter = ",";

opts.VariableNames = "Tamb";

opts.VariableTypes = "double";

opts.ExtraColumnsRule = "ignore";

opts.EmptyLineRule = "read";

Tamb = readtable("Team_82.dat", opts);

Tamb = table2array(Tamb);

clear opts

%% Solution

odefunc = @(t,y) odeFunction1(t,y); %output of odeFunction1 has to be dy/dt

tspan = linspace(0,length(Tamb),length(Tamb));

y0 = [18; 20]; % initial conditions

[t_solution,y_solution] = ode45(odefunc, tspan, y0);

function [dydt] = odeFunction1(t,y)

Tamb = evalin('base','Tamb');

R1 = 0.4;

R2 = 0.3;

Rwin = 0.5;

Cint = 1.0 * 10^4;

Cwall = 5.0 * 10^2;

A = 1 / (Cwall*R2);

B = 1 / (Cwall*R1);

C = 1 / (Cint*R1);

D = 1 / (Cint*Rwin);

Twall = y(1,1); %these are your state variables inside the state vector y

Tint = y(2,1); % you have two states (Twall and Tint)

tspan = linspace(0,length(Tamb),length(Tamb));

Tamb_t = interp1(tspan,Tamb,t);

dydt = zeros(2,1); %your output time derivative of the state vector

dydt(1,1) = A*Tamb_t - A*Twall + B*Tint - B*Twall;

dydt(2,1) = C*Twall - C*Tint + D*Tamb_t - D*Tint;

end

Daniel
on 25 Oct 2019

Stephan
on 25 Oct 2019

I agree, but if we dont want to confse him, at first we should tell him that there is no inbuilt function ode_1

;-)

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 8 Comments

## Stephan (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487412-using-ode45-to-solve-differential-equation-with-time-dependent-variable#comment_760063

## Hidde Kemperink (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487412-using-ode45-to-solve-differential-equation-with-time-dependent-variable#comment_760065

## Daniel (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487412-using-ode45-to-solve-differential-equation-with-time-dependent-variable#comment_760074

## Hidde Kemperink (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487412-using-ode45-to-solve-differential-equation-with-time-dependent-variable#comment_760079

## Daniel (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487412-using-ode45-to-solve-differential-equation-with-time-dependent-variable#comment_760087

## Hidde Kemperink (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487412-using-ode45-to-solve-differential-equation-with-time-dependent-variable#comment_760090

## Hidde Kemperink (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487412-using-ode45-to-solve-differential-equation-with-time-dependent-variable#comment_760094

## Stephan (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487412-using-ode45-to-solve-differential-equation-with-time-dependent-variable#comment_760096

Sign in to comment.