Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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 Kommentare
James Tursa
James Tursa am 22 Mai 2020
Please repost your code as text formatted with the CODE button. We can't copy and run pictures ...
Ibrahim Taha
Ibrahim Taha am 22 Mai 2020
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)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

David Goodmanson
David Goodmanson am 22 Mai 2020
Bearbeitet: David Goodmanson am 22 Mai 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 Kommentare
Ibrahim Taha
Ibrahim Taha am 23 Mai 2020
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 am 23 Mai 2020
You are quite welcome. I think we have all had the frustrating experience of staring at code for days ...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by