Plotting the solution to a system of ODE on an interval that doesn't contain the initial time
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Cris19
el 1 de En. de 2020
Comentada: Star Strider
el 2 de En. de 2020
I have to plot the solution to the system of ODEs
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257819/image.png)
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].
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257820/image.png)
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
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257821/image.png)
For the second interval
I used a similar function, but in in w:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257822/image.png)
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
?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257823/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257824/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257825/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257826/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257827/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257828/image.png)
0 comentarios
Respuesta aceptada
Star Strider
el 1 de En. de 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');
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!