フィルターのクリア

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

2 ビュー (過去 30 日間)
Ibrahim Taha
Ibrahim Taha 2020 年 5 月 22 日
コメント済み: David Goodmanson 2020 年 5 月 23 日
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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by