Global variables to plot variables from ODE45 function
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Torsten
2023-9-7
编辑:Torsten
2023-9-7
t < pi/2 is necessary:
syms t x(t)
eqn = diff(x,t) == x^2*cos(t);
conds = x(0)==1;
dsolve(eqn,conds)
tspan= 0:.01:10;
x=1;
[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));
end
figure;
plot(t,a);
function [dxdt,a] = odefcn(x,t)
a=cos(t)*x;
dxdt = x*a;
end
6 个评论
Torsten
2023-9-14
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!