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.

采纳的回答

Star Strider
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 个评论
Frederik Bjerregaard
I managed to get the code working using plot rather than surf. Thank you for your help
Star Strider
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 CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by