Plot the travel for an electron by using ode45 (involving matrices)

2 次查看(过去 30 天)
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 个评论
James Tursa
James Tursa 2020-5-22
Please repost your code as text formatted with the CODE button. We can't copy and run pictures ...
Ibrahim Taha
Ibrahim Taha 2020-5-22
Oh, my mistake!
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)

请先登录,再进行评论。

采纳的回答

David Goodmanson
David Goodmanson 2020-5-22
编辑:David Goodmanson 2020-5-22
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 个评论
Ibrahim Taha
Ibrahim Taha 2020-5-23
God bless you, David. Now I finally understand why you changed the initial coordinates. Thanks for saving me from another day of not getting anywhere with my code.
David Goodmanson
David Goodmanson 2020-5-23
You are quite welcome. I think we have all had the frustrating experience of staring at code for days ...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by