Returning several dataset using ode45

5 ビュー (過去 30 日間)
Frederik Bjerregaard
Frederik Bjerregaard 2021 年 12 月 7 日
Hi. I have a problem where I want an ode45 function to return 5 dataset using the code
clc; clear; close all;
% Defining parameters and initial conditions
f = 0.35;
r = [0.3:0.3:1.5];
zeta = 0.25;
Theta0 = [0.30, 0];
tspan = [0 10];
% Solver options
opts = odeset('RelTol',1e-6,'AbsTol',[1e-9 1e-9]);
% Solver
[t,theta] = ode45(@(t,theta) odefcn(t,theta,f,r,zeta), tspan, Theta0);
plot(theta(:,1),theta(:,2));
As you can see, r is a parameter in the function and I want to create a plot for each of the values for r. The code is running fine when using a single value for r but breaks when using a vector.

採用された回答

VBBV
VBBV 2021 年 12 月 7 日
for i = 1:length(r)
[t,theta] = ode45(@(t,theta) odefcn(t,theta,f,r,zeta), tspan,Theta0);
plot(theta(:,1),theta(:,2));hold on;
end
Use a for loop and try it
  3 件のコメント
Frederik Bjerregaard
Frederik Bjerregaard 2021 年 12 月 8 日
No it still gives me an error. Just to be sure I have incorporated it correctly, was this what you meant?
clc; clear;close all;
% Defining parameters and initial conditions
f = 0.35+0.1;
r = [0.3:0.3:1.5];
zeta = 0.25;
Theta0 = [0.30, 0];
tspan = [0 1000];
% 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,zeta), tspan,Theta0);
plot(theta(:,1),theta(:,2));hold on;
end
I will just include the function file as well for good measure
function dthetadt = odefcn(t,theta,f,r,zeta)
dthetadt = zeros(2,1);
dthetadt(1)=theta(2);
dthetadt(2)=f.*sin(r.*t)-2.*zeta.*theta(2)-sin(theta(1));
end
Frederik Bjerregaard
Frederik Bjerregaard 2021 年 12 月 8 日
I got it working now. Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by