How to use ODE45 with multiple initial conditions?

5 次查看(过去 30 天)
I"ve created a function that uses ode45 to draw solution curves for an equation in the x1x2-plane this phase portrait is supposed to be on the same plot as a direction field as well as plot the initial conditions. My coding for the phase portrait is as follows:
function [ output_args ] = phasePortrait(A,tmin,tmax,x1init,x2init)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
directionField(A)
hold on
f= @(t,y) [A(1,1).*y(1) + A(1,2).*y(2);A(2,1).*y(1) + A(2,2).*y(2)];
[t,y]=ode45(f,[tmin, tmax],[x1init, x2init]);
hold on
plot(y(:,1),y(:,2))
hold on
plot(x1init, x2init)
axis(-2.5,2.5,-2.5,2.5)
title('Phase Portrait')
xlabel('x1(t)')
ylabel('x2(t)')
end
*When I call the function I get an error saying that I have vectors of different lengths for the intial conditions and the results. How do I fix this or am I imputing something wrong?*
tmin=0
tmax=20
A=[-1 2;-2 -1]
x1init=[-2.5 1.5 2.5 -2.5]
x2init=[0 -2.5 0 2]
figure
phasePortrait(A,tmin, tmax, x1init, x2init)

回答(1 个)

Torsten
Torsten 2015-10-6
Call ode45 four times in a loop, first for [x1init(1) x2init(1)] as initial condition, then for [x1init(2) x2init(2)] as initial condition and so on.
Best wishes
Torsten.

类别

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