Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am a completely new to Mathematica, so my question and code may look silly. I need to find a equation of motion of a point particle of mass m moving in a potential V(x) = A abs[x]^n. (n=1,2,3,4,5,6. m=1, A=1, x(0)=2, x'(0)=0 ) I tried to find the equation using Euler method, so I made code below. I know somethings wrong with my code, but I have no idea. How can I make one equation with various n values? Can someone help me?

T = 0.5*x'[t]^2;
V = Abs[x[t]];
L = T - V
-Abs[x[t]] + 0.5 Derivative[1][x][t]^2
EulerEquations[%, x[t], t]
-Derivative[1][Abs][x[t]] - 1. (x^\[Prime]\[Prime])[t] == 0
NDSolve[{x''[t] + Abs'[x[t]] == 0, x[0] == 2, x'[0] == 0}, x, {t, 0,10}]
NDSolve::ndnum: Encountered non-numerical value for a derivative at t == 0.`. >>

And, additionally I need to plot the period as a function of n. How can I construct the plot?

share|improve this question

closed as off-topic by m_goldberg, Sjoerd C. de Vries, bobthechemist, István Zachar, ubpdqn Mar 4 at 12:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Sjoerd C. de Vries, bobthechemist, István Zachar, ubpdqn
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Homework, huh ? Look here –  Sektor Mar 3 at 1:42
    
@Sektor That Q has been deleted since, ailee has no rep to see it. –  István Zachar Mar 4 at 11:05
    
@IstvánZachar Well, it was there when I posted the comment, so she had time to read it :) –  Sektor Mar 4 at 11:06
    
@Sektor I only put my comment here in case ailee wonders why the link points to an empty page or "page not found" :) –  István Zachar Mar 4 at 13:43
    
@IstvánZachar Yeah, but she still got 3 replies :D Anywayz - have a nice day :) –  Sektor Mar 4 at 13:47

3 Answers 3

Not sure if I understand the physics of the problem. But to use the EulerEquations, you need to call the VariationalMethods. Below is my edit of your code:

Needs["VariationalMethods`"]
lim = 6
T = 0.5*x'[t]^2
V = Sqrt[x[t]^2]^n
L = T - V
eqn[n_] = EulerEquations[L, x[t], t]
soln = x[t] /. (First@
NDSolve[{eqn[#], x[0] == 2, x'[0] == 0}, x, {t, 0, 10}] &) /@ Range@lim
Plot[soln[[#]], {t, 0, 10}] & /@ Range@lim

Is it what you are looking for?

share|improve this answer
    
Thanks!! I didn't know I needed to use VariationaltMethods. :) –  ailee Mar 3 at 3:22

You can just solve it like we do by hand. Finding equation of motion from Lagrangian is really only one line:

Clear[x, t, n]
ke = 0.5*x'[t]^2;
pe = Sqrt[x[t]^2]^n;
lagrangian = ke - pe;
eq = D[D[lagrangian, x'[t]], t] - D[lagrangian, x[t]] == 0;
sol[p_] := x[t] /. First@NDSolve[{eq /. n -> p, x[0] == 2, x'[0] == 0}, x, {t, 0, 10}]
Plot[Evaluate[sol[2]], {t, 0, 10}]

Mathematica graphics

share|improve this answer

It seems you're back =) Well the problem with your code up there is that Mathematica is not expanding "Abs'[x[t]]" as well as you're missing "Needs["VariationalMethods`"]".

One variant is to "Trick" Mathematica a bit and change the potential energy term in the Lagrangian by writing $\left|x\right|$ as $\sqrt{x^2}$. Using "EulerEquations" you could then do something like this:

(* Remove all definitions but not symbols *)
ClearAll["Global`*"] 
(* Remove all symbols *)
Remove["Global`*"]
(* Load Packages *)
Needs["VariationalMethods`"]
T = 1/2*x'[t]^2;
(* Note this is where we "trick" Mathematica *)
V = Sqrt[x[t]^2]^n;
L = T - V;
eqms = EulerEquations[L, x[t], t];
solutions =
  x /. NDSolve[{eqms /. {n -> #}, x[0] == 2, x'[0] == 0}, 
      x, {t, 0, 10}] & /@ Range[1, 6];
GraphicsColumn[
 Plot[
    #[t],
    {t, 0, 10},
    AspectRatio -> 0.25,
    Frame -> True,
    PlotStyle -> {Thick, Black}
    ] & /@ Flatten@solutions,
 ImageSize -> 500,
 Spacings -> 0
]

Output

But this workaround seems unsatisfactory. On the other hand one can force the Evaluation of "Abs'[x[t]]" in the Lagrangian using the ComplexExpand method:

(* Remove all definitions but not symbols *)
ClearAll["Global`*"] 
(* Remove all symbols *)
Remove["Global`*"]
(* Load Packages *)
Needs["VariationalMethods`"]
T = 1/2*x'[t]^2;
(* Note this is where we "trick" Mathematica *)
V = Abs[x[t]]^n;
L = T - V;
eqms = ComplexExpand@EulerEquations[L, x[t], t];
solutions =
  x /. NDSolve[{eqms /. {n -> #}, x[0] == 2, x'[0] == 0}, 
      x, {t, 0, 10}] & /@ Range[1, 6];
GraphicsColumn[
 Plot[
    #[t],
    {t, 0, 10},
    AspectRatio -> 0.25,
    Frame -> True,
    PlotStyle -> {Thick, Black}
    ] & /@ Flatten@solutions,
 ImageSize -> 500,
 Spacings -> 0
]

ComplexExpand basically forces Mathematica to assume $x(t)$ is real valued.

share|improve this answer
    
Sorry about that. We often get spam and random answers, and your previous version did look like one :) I've undeleted it now. –  rm -rf Mar 3 at 9:27
    
@rm-rf And I flagged it. Sorry. –  belisarius Mar 4 at 5:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.