Back to Problems

 

Mathematica Code for the Electron Animation

 

 

H.Tahsiri

Please note:The following Mathematica codes are not written in an efficient way. 
It is highly fragmented on purpose, with the style of " type a line/get a result".


Clear[z,v,given,sol,eq1,eq2,diffequ,vplot,yplot,y,t,phaseplot,ta]

 

diffequ=y''[t]+y[t]

     y[t] + y''[t]

 

sol=NDSolve[{diffequ==0,y[0]==2, y'[0]==0},y,{t,0,10}]

     {{y -> InterpolatingFunction[{{0., 10.}}, <>]}}

 

yplot=Plot[y[t]/.sol,{t,0,6.3},PlotStyle->RGBColor[0,0,1]];

vplot=Plot[y'[t]/.sol,{t,0,6.3},PlotStyle->RGBColor[0,1,1]];
[Graphics:animateleccodegr2.gif]

Draw a ring of charge

xx[ta_]= Sin[ta];
yy[ta_]=.2 Cos[ta];
p[ta_]={xx[ta],yy[ta]}


     {Sin[ta], 0.2 Cos[ta]}

 

c=ParametricPlot[{p[ta]},{ta,0,2Pi},AspectRatio->Automatic,
Compiled->False,Axes->True,
PlotStyle->{{Thickness[0.01],RGBColor[1,0,0]}}];

Display text[Graphics:animateleccodegr2.gif]

Clear[pp,cont]
cont=	 Epilog->{{Text["y[t] = ",{1.8,2.4}]},
{Text[y[t].05/.sol[[1]],{3.6,2.4}]},
{Text["v[t] = ",{5.8,2.4}]},{Text[y'[t]/.sol[[1]],{7.5,2.4}]}}

     Epilog -> {{Text[y[t] = , {1.8, 2.4}]}, 
      
        {Text[0.05 InterpolatingFunction[{{0., 10.}}, <>][t], {3.6, 2.4}]}, 
      
        {Text[v[t] = , {5.8, 2.4}]}, {Text[InterpolatingFunction[{{0., 10.}}, <>][t], 
      
          {7.5, 2.4}]}}

Animate the electron

pp=Do[Show[c,vplot,yplot,
Graphics[{Text[FontForm["y(t)",{"CourierB",10}],{1.5,1.5}],
Text[FontForm["v(t)",{"CourierB",10}],{3.5,1.5}],
PointSize[.03],Point[  {0, y[t]/.sol[[1]] }  ]   ,
PointSize[0],Point[  {0,y'[t]/.sol[[1]] }  ] ,
{PointSize[.02],Point[  {t,(y[t])/.sol[[1]]}   ] }  } ] ,
cont,PlotRange->{{-2,7.6},{-2,2}},
AxesLabel->{"time","height"},
Ticks->{{0,1,2,3,4,5,6},{{-2,-.1},{-1,-.5},{0,0},{1,.5},{2,.1}}}],
{t,0,6.3,.2}];

[Graphics:animateleccodegr2.gif]

Back to Problems