필터 지우기
필터 지우기

Extracting a data vector from a looped ode45

조회 수: 1 (최근 30일)
Frederik Bjerregaard
Frederik Bjerregaard 2021년 12월 8일
댓글: Star Strider 2021년 12월 8일
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
Frederik Bjerregaard 2021년 12월 8일
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에서 Solver-Based Nonlinear Optimization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by