Hi there!

I've got a problem that is similar to one that I asked previously about the travel for an electron. This task requires me to plot the travel of an electron that enters a circle, with radius 1, with the initial coordinates of (1,0) and exits at (0,1). I'm supposed to use ode45 with the 'Events' options in combination with my function xvel, that (I'm not that sure of) detects when the x-coordinate is 0. I know that the initial velocity for the elextron is 0 along the y-axis, but unknow along the x-axis.

I've decided to write the coordinates in the form [x;y;u;v]. (x,y) = the position of the electron, (u,v)=the velocity of the electron.

tguess=[1;0;-1.7;0];

options=odeset('Events',@xvel);

[t,ux]=ode45(@particle,[0 2],tguess,options);

theta=linspace(0,2.*pi);

xp=cos(theta);

yp=sin(theta);

plot(xp,yp);

hold on

plot(ux(:,1),ux(:,2));

hold on

function [val,stopp,dir] = xvel(t,xv)

val=xv(1);

stopp=0;

dir=[-1;0];

end

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

I've also been given the function yprime from the task. When I plot the figure, it's supposed to look like the figure below (excluding the dotted lines!).

But my figure turns out to look something like this.

I'm not that experienced with 'Events', which is why I'm struggling a lot here. I suspect that the problem lies around that area.

I appreciate any advice/ help I can get!