Using ode45 to plot

조회 수: 22 (최근 30일)
Bob
Bob 2016년 8월 15일
답변: Star Strider 2016년 8월 15일
Equations:
df/dt= 4f(t) - 3f(t)p(t)
dp/dt= -2p(t) + f(t)p(t)
Question: Useode45to plot some representative examples of solution trajectories on the time scale[0;5], starting at the following points: (0.5,1), (1.0,5), (1,1), and (1.5,1.5).[Hint.When you define your differential equation,f is y(1)and p is y(2). This will stretch the axes of your graph; that's okay.]
Code:
syms f(t) p(t);
figure; hold on
for c = 0:0.1:5
g = @(f, p) 4*f - 3*f*p;
h = @(f,p) -2*p + f*p;
[f,p] = ode45(g,h, [0,5], c);
end
plot(f, p)
axis tight;
Error: Error using odearguments (line 21) When the first argument to ode45 is a function handle, the tspan argument must have at least two elements.
Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in Project_5_2 (line 23) [f,p] = ode45(g,h, [0,5], c);
  댓글 수: 1
Bob
Bob 2016년 8월 15일
Current code:
syms f(t) p(t);
figure; hold on
for c = 0:0.1:5
g = @(f, p) 4*f - 3*f*p;
[f,p] = ode45(g, [0,5], c);
end
for c = 0:0.1:5
h = @(f,p) -2*p + f*p;
[f,p] = ode45(h, [0,5], c);
end
plot(f, p)
axis tight;
I am getting a graph but I am not sure if it is right?

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2016년 8월 15일
Your ‘g’ and ‘h’ functions are not correct. They have to be functions of time and a vector of functions. I tweaked your code a bit. See if this does what you want:
% MAPPING: x(1) = f, x(2) = p
gh = @(t,x) [4*x(1) - 3*x(1).*x(2); -2*x(2) + x(1).*x(2)];
tspan = linspace(0, 5, 250);
figure; hold on
for c = 0:0.1:5
[t,x] = ode45(gh, tspan, [c; c]);
plot(x(:,1), x(:,2))
end
axis tight

추가 답변 (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