Pass out a dependent variable from ode15s without taking the derivative of it

1 view (last 30 days)
I have search for hours but i can't seem to figure out how it is possible to exstract a variable depending on x (x is derrived using the ode15s)
My solution to a system is (simplified)
function [y,t,rr] = simulation(t,initial)
options = odeset('RelTol', 1e-5, 'AbsTol', 1e-7);
[y,t,rr] = ode15s(@kinetics, t, initial, options);
end
The kinetics function is as follows (simplified)
function [dxdt,x,rr] = kinetics(t,x,init)
%code desribing i different phenomenas all depending on "x" e.g:
Ph1 = numaxG * x(1,1) * x(8,1) / (KSPG + x(1,1) + (x(1,1)^2)/KiPG);
%constants like numaxG has been defined..
rr(1,1) = Ph1 * Ph5 * Ph7 * Ph10 * Ph16 * Ph19a;
rr(2,1) = Ph2 * Ph6 * Ph8 * Ph11 * Ph17 * Ph19b * Ph21;
rr(3,1) = Ph3;
rr(4,1) = Ph12 * Ph9;
rr(5,1) = Ph14;
rr(6,1) = 0;
rr(7,1) = 0;
% some other code describing dxdt
end
right now rr is also integrated using the solver, but i would like it to get as an ouput without it being solved. For me this is difficult as it is dependent on x which is derived.
  4 Comments
Andreas Skovhøj
Andreas Skovhøj on 27 Mar 2020
no but that is my problem.. i know that i should only use two but i added "rr" because i need it as a output from the function.

Sign in to comment.

Accepted Answer

darova
darova on 27 Mar 2020
You should calculate rr variable after solving ode45
An example
function main
[t,u] = ode45(@f,[0.1 3],[1 1]);
u1 = u(:,1); % extract u1
u2 = u(:,2); % extract u2
rr1 = u1 - u2 ...
end
function du = f(t,u)
du(1,1) = u(2);
du(2,1) = sin(u(1))/t;
end
  7 Comments
darova
darova on 27 Mar 2020
Edited: darova on 27 Mar 2020
Here is an idea
Your current kinetics.m
function [dxdt,rr,x] = kinetics(t,x,p,init,t_load,a,mode)
% long code
% computations with 'x' and other parameters
rr(1:7,1) = ... % calculations
dxdt = ... % derivatives
end
Write an additional function
function par = mycalc(p,x)
% long code
% computations with 'x' and other parameters
% ... return all needed results for further calculation as struct
par.Ph1 = Ph1;
par.Ph2 = Ph2;
end
function dxdt = kinetics(t,x,p,init,t_load,a,mode)
par = mycalc(p,x)
rr(1:7,1) = ... % calculations
dxdt = ... % derivatives
end
And after solving (having x and all needed parameters) you can just
par = mycalc(p,x);
rr = par.rr;
I don't like to use structures since each parametr should be written (you have a lot of them). But it's the only idea i have for your case
I saw this line at the end
dxdt = rr'*st + vol';
Once you solved your equations you have x. You can calculate dxdt (using diff possibly)
So it possible to calculate rr
rr = inv(st)*(dxdt-vol)

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!