Plotting the solution to a system of ODE on an interval that doesn't contain the initial time

8 views (last 30 days)
I have to plot the solution to the system of ODEs
with the initial conditions on the time interval [100,200]. I am trying to do this somehow unusual, by solving the system separately on [0,100] and [100,200].
For the interval [0,100] I used the function in z:
function dzdt=odefunzz(t,z)
dzdt=zeros(2,1);
dzdt(1)=z(2);
dzdt(2)=-z(1);
end
the command lines
tspan = [0 100];
z0 = [0.1 0.1];
[t,z] = ode45(@(t,z) odefunzz(t,z), tspan, z0);
plot(t,z(:,1),'r',t,z(:,2),'b');
and I easily obtained the plotting of the solution on .
For the second interval I used a similar function, but in in w:
function dwdt=odefunww(t,w)
dwdt=zeros(2,1);
dwdt(1)=w(2);
dwdt(2)=-w(1);
end
and similar command lines,
tspan = [0 100];
w0 = [z(1) z(2)];
[t,w] = ode45(@(t,w) odefunww(t,w), tspan, w0);
plot(t,w(:,1),'r',t,w(:,2),'b');
Is the command
w0 = [z(1) z(2)];
correct? Because I need to use the values of the first solution as initial conditions for the system on . Or, more exactly, does the vector (at the end of the first command lines) represent the values of the solution z (found on the interval ) at the endpoint ?

Accepted Answer

Star Strider
Star Strider on 1 Jan 2020
Is the command
w0 = [z(1) z(2)];
correct?
No, not if you want to do what you indicated. If you want to use the last values of ‘z’ as the intiial conditions for ‘w’, I would do something like this instead:
odefunzz = @(t,z) [z(2); -z(1)];
odefunww = @(t,w) [w(2); -w(1)];
tspan = [0 100];
z0 = [0.1 0.1];
[t,z] = ode45(@(t,z) odefunzz(t,z), tspan, z0);
figure
plot(t,z(:,1),'r',t,z(:,2),'b');
tspan = [100 200];
w0 = [z(end,1) z(end,2)];
[t,w] = ode45(@(t,w) odefunww(t,w), tspan, w0);
figure
plot(t,w(:,1),'r',t,w(:,2),'b');

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!