Plot the travel for an electron by using ode45 (involving matrices)
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ibrahim Taha
el 22 de Mayo de 2020
Comentada: David Goodmanson
el 23 de Mayo de 2020
Hello there!
I've got an issue regarding using ODE45 to solve a system of matrices when plotting the distance an electron travels during a given time. I've been given tstart and tspan, but the figure doesn't seem to match up with what I'm supposed to get.
In this task, I have to solve the equation y'=A*x+b, where A,x and b are matrices and then plot how an electron travels. I've worked with ode45 quite some before, but I don't understand why it doesn't want to co-operate with me this time. (Maybe it's because of the use of matrices?).
function yprime = particle(t,y)
b=[0;0;1;0];
A=[0,0,1,0;0,0,0,1;0,0,0,1;0,0,-1,0];
yprime=A*y+b;
end
tstart=[-2;0;1;2];
tspan=[0 10];
[tres,s]=ode45(@particle,tspan,tstart);
plot(tres,s)
axis([-2 2 -2 2])
The codes above are what I've written. The figure above is what my code has plotted.
The figure below is what the figure is supposed to look like, only the dotted lines. Exclude the other line.
My question is how doesn't my code give the "correct" results, when I don't see what's wrong with what I've done.
Thanks for taking your time
EDIT: I forgot to add my figures and my code
2 comentarios
James Tursa
el 22 de Mayo de 2020
Please repost your code as text formatted with the CODE button. We can't copy and run pictures ...
Respuesta aceptada
David Goodmanson
el 22 de Mayo de 2020
Editada: David Goodmanson
el 22 de Mayo de 2020
Hi Ibrahim,
There is nothing wrong with using matrices in this situation, and the plot is fine, insofar as it is plotting what you asked for, which is x, y, vx and vy as functions of time. I am going to speculate that what you are looking for is not a plot of coordinates as a function of time but rather the electron's path in space as a function of x and y. If that's true then your example figure shows an electron that starts out at (1,0) with negative velocity. The code below uses initial conditions that give a version of that figure.
b=[0;0;1;0];
A=[0,0,1,0;0,0,0,1;0,0,0,1;0,0,-1,0]
tstart=[1;0;-2;0]; % new initial conditions
tspan=[0 2];
[tres,s]=ode45(@particle,tspan,tstart);
figure(1)
plot(tres,s)
th = linspace(0,2*pi,200);
figure(2) % the path
plot(s(:,1),s(:,2),'--',sin(th),cos(th))
axis equal
grid on
function yprime = particle(t,y)
b=[0;0;1;0];
A=[0,0,1,0;0,0,0,1;0,0,0,1;0,0,-1,0]
yprime=A*y+b;
end
2 comentarios
David Goodmanson
el 23 de Mayo de 2020
You are quite welcome. I think we have all had the frustrating experience of staring at code for days ...
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!