Modeling concentration using ODE
Show older comments
I'm trying to model a dye concentration but I cannot make to run and I'm stuck. It gives an error but it will not tell what is wrong. Can any one help thanks?
clear all;
data = load('test_data.dat');
time = size(data,1);
u = data(:,2);
tspan = [1:1:time];
y0 = data(1,3);
f = -0.25;
g = 0.25;
T = 10.0;
[t,y]=ode45(@model,tspan,y0,f,g,T,U);
plot(t, y(:,end), '*-b', 'LineWidth', 1);
function dydt = model(t,y,f,g,T,u)
if t-1 <= T
u = 0;
else
t_back = t-T;
n = floor(t_back); u =(n);
end
dydt = f*y + g*u;
return;
end
9 Comments
Image Analyst
on 26 Sep 2021
Well we're also stuck. I formatted your post as code but you forgot to attach 'test_data.dat' so we're stuck. We'll check back later for it, after you read this:
Stark Volt
on 26 Sep 2021
Image Analyst
on 26 Sep 2021
What is option? It says it's not defined. If I use options instead of option, I just get more errors.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 20;
%(the .mlx file)
%Dye concentration
clear all;
data = load('test_data.dat');
time = size(data,1); % simulation time
input = data(:,2); % measured top concentration
rslt = data(:,3); % measured bottom concentration
tspan = [1:1:time]; % time variable
inc = data(1,3); % initial bottom concentration
%model parameters
fparameter = -0.25;
gparameter = 0.25;
delay = 10.0;
options = odeset('RelTol', 1e-3);
[t,y]=ode45(@model,tspan,inc,option);
plot(tspan, obsv, '-b', 'LineWidth', 0.5); hold on;
plot(tspan, input, '-g', 'LineWidth', 0.5);
plot(t, y(:,end), '-.m', 'LineWidth', 1);
legend('rslt', 'input');
xlabel('time(second)'); ylabel('Concentration');
grid;
%Predefined function (the .m file)
function dydt = model(t,y,parameter,input)
% MODEL ordinary differential equation (ODE) model:
% dy/dt = f*y(t) + g*u(t-T)
dxdt = 0;%return variables
if t-1 <= parameter(3) %delay the initial concentration
u_input = 0;
else
t_back = t-parameter(3);
n = floor(t_back); u_input = input(n);
end
dydt = f*y(t) + g*u(t-T)
return;
end
Stark Volt
on 26 Sep 2021
Star Strider
on 26 Sep 2021
@Stark Volt — Do you want to estimate parameters of the model using the data?
This can be relatively straightforward, however the system of differential equations must be defined in terms of the parameters. It is not obvious to me what those are.
.
Stark Volt
on 26 Sep 2021
Star Strider
on 27 Sep 2021
I cannot understand what you are doing well enough to attempt an actual answer.
See Coefficient estimation for a system of coupled ODEs for an illustration of an approach that works. (There are several others as well, all using the same essential approach.)
Put your system in that sort of format, and use your data. If you have problems with it, post back here and I will do my best to get your code to work correctly.
.
Stark Volt
on 28 Sep 2021
Star Strider
on 29 Sep 2021
Please provide a symbolic (preferably LaTeX) version of the differential equation system you’re working with, and the parameters you want to estimate. If you have a PDF of a paper describing what you want to do, that would be even better. (Attach / upload the PDF, not a link to it, because thery’re usually behind a paywall, preventing me from accessing them if I don’t subscribe to the journal.)
Also, putting if blocks in the differential equation function is going to cause problems. Numerical differential equation integration functions don’t do well when integrating across the discontinuities that the if blocks cause.
I don’t understand the reason ‘parameter’ is nowhere represented in the differential equation.
I’ll do my best to see if I can get the differential equation (or system of them) to work with my code and your data.
.
Answers (1)
Sulaymon Eshkabilov
on 29 Sep 2021
You had better use this data importing fcn:
data = readmatrix('test_data.dat');
Make sure that the data file (test_data.dat) is present in your current directory or change to the directory where the data file (test_data.dat) is residing.
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!