Extracting a data vector from a looped ode45
2 次查看(过去 30 天)
显示 更早的评论
Hi. I have a problem where i have to solve a system of equations numerically. I want to plot the maximum value for each result I get with the the a variable i am using (it is changing each iteration). However, when i try to plot it it seems it only uses the first result and variable.
clc; clear;close all;
% Defining parameters and initial conditions
f = 0.35;
r = [0.1:0.1:1.5];
zeta = 0.25;
Theta0 = [0.30, 0];
tspan = [0 20];
% Solver options
opts = odeset('RelTol',1e-6,'AbsTol',[1e-9 1e-9]);
% Solver
for i = 1:length(r)
[t,theta] = ode45(@(t,theta) odefcn(t,theta,f,r(i),zeta), tspan,Theta0);
Amp=max(theta(:,1));
plot(r,Amp);hold on;
end
I want to plot is so i have a line showing the change in Amp dependent on the change in r.
0 个评论
采纳的回答
Star Strider
2021-12-8
I cannot run this because ‘odefcn’ is over the horizon.
I would do something like this —
% Defining parameters and initial conditions
f = 0.35;
r = [0.1:0.1:1.5];
zeta = 0.25;
Theta0 = [0.30, 0];
tspan = linspace(0, 20, 50);
% Solver options
opts = odeset('RelTol',1e-6,'AbsTol',[1e-9 1e-9]);
% Solver
for i = 1:length(r)
[t,theta] = ode45(@(t,theta) odefcn(t,theta,f,r(i),zeta), tspan,Theta0);
Amp(:,i)=max(theta(:,1));
end
figure
surf(t, r, Amp)
grid on
It may be necessary to reverse ‘t’ and ‘r’ in the surf call so that the dimensions will match appropriately.
.
3 个评论
Star Strider
2021-12-8
As always, my pleasure!
(I remembered just now that it saves a point and not a vector, so that is appropriate.)
.
更多回答(0 个)
另请参阅
类别
在 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!