Global variables to plot variables from ODE45 function

Hello,
the following code stands representative for a bigger code, in which i want to plot some variables over time that are calculated in the odefunction but are gonna be passed to the ODE solver. Is there any way to set these variables as global variables, so that I can plot them outside of the ode Function?
Like in this case to plot the variable "a", which is calculatet inside the function.
tspan= 0:.01:10;
x=1;
[t,x] = ode45(@(t,x) odefcn(x,t),tspan,x);
figure;
plot(t,a);
function [dxdt] = odefcn(x,t)
a=cos(t)*x;
dxdt = x*a;
end

Respuestas (1)

Torsten
Torsten el 7 de Sept. de 2023
Editada: Torsten el 7 de Sept. de 2023
t < pi/2 is necessary:
syms t x(t)
eqn = diff(x,t) == x^2*cos(t);
conds = x(0)==1;
dsolve(eqn,conds)
ans = 
tspan= 0:.01:10;
x=1;
[t,x] = ode45(@(t,x) odefcn(x,t),tspan,x);
Warning: Failure at t=1.562403e+00. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (3.552714e-15) at time t.
a = zeros(size(t));
for i = 1:numel(t)
[~,a(i)] = odefcn(x(i),t(i));
end
figure;
plot(t,a);
function [dxdt,a] = odefcn(x,t)
a=cos(t)*x;
dxdt = x*a;
end

6 comentarios

Marlon
Marlon el 8 de Sept. de 2023
Editada: Marlon el 8 de Sept. de 2023
Thank you for the answer.
I tried to implement this in the real code, however i always get the error
">> Zustandsraum_Pacjeka_App
Index exceeds the number of array elements. Index must not exceed 1.
Error in Zustandsraum_Pacjeka_App>odefcn (line 72)
rv = [x(4)-lv*x(6)*sin(x(3)); %x(3): PSI, x(4): XV', x(6): PSI'
Error in Zustandsraum_Pacjeka_App (line 26)
[~,a(i)] = odefcn(x(i),t(i));"
I tried to implement it like this, perhaps you could have a look at it :) :
(..)
[t,x] = ode45(@(t,x) odefcn(x,t) ,tspan,x );
a = zeros(size(t));
for i = 1:numel(t)
[~,a(i)] = odefcn(x(i),t(i));
figure;
plot(t,a);
end
function [dxdt,a] = odefcn(x,t)
(..)
vrv = [cos(x(3)+delta) sin(x(3)+delta) 0;
-sin(x(3)+delta) cos(x(3)+delta) 0;
0 0 1]*rv;
a=vrv(1);
Torsten
Torsten el 8 de Sept. de 2023
Editada: Torsten el 9 de Sept. de 2023
What is rv ? A column vector with three elements ?
Marlon
Marlon el 14 de Sept. de 2023
yes, exactly.
When I try to extract any variable that is dependent on state variables, I get the mentioned error message.
I noticed that It works to extract variables that are only dependent on the time and not on state variables, if I code it with "[~,a(i)] = odefcn(x,t(i));" and not "[~,a(i)] = odefcn(x(i),t(i));"
However I also need to extract variable that are calculated with state variables..
If you want to extract vrv as a 3x1 vector, use
a = zeros(3,numel(t));
for i = 1:numel(t)
[~,a(:,i)] = odefcn(x(i,:),t(i));
end
Marlon
Marlon el 14 de Sept. de 2023
Thanks for the quick answer, but when I try to plot the first row of vrv vs time
I get following error:
Error using plot
Vectors must be the same length.
Error in Zustandsraum_lin (line 62)
plot(t,vrv(:,1))
Change the code according to your needs:
t = 0:1:100;
x = [t.',t.^2',t.^3']
a = zeros(3,numel(t));
for i = 1:numel(t)
[~,a(:,i)] = odefcn(x(i,:),t(i));
end
plot(t,a(2,:))
function [dxdt,vrv] = odefcn(x,t)
rv = [1;1;1];
delta = 0;
vrv = [cos(x(3)+delta) sin(x(3)+delta) 0;
-sin(x(3)+delta) cos(x(3)+delta) 0;
0 0 1]*rv;
end

Iniciar sesión para comentar.

Categorías

Más información sobre Function Handles en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Preguntada:

el 7 de Sept. de 2023

Comentada:

el 14 de Sept. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by