Problems solving this differential equation for a Pendulum with Mathematica

4 replies · 5K views · last reply
Lambda96
Messages
233
Reaction score
77
TL;DR
Mathematica does not solve the differential equations
Hi,

unfortunately, I have problems that Mathematica does not solve the differential equation. The task is as follows and it is about the task c

Diagram of a simple pendulum hanging from a pivot, with length l, angle phi from the vertical, and gravity g pointing downward.


In the Mathematica Notebook, the following was written for task c

"You can use the following two lines of code to produce the solutions of the exact and approximated differential equations with boundary conditions. After that, write a plot[ ] function to sketch the two together."

exact[t_] := Evaluate[phi[t] /. NDSolve[..., phi[t], {t, 0, 20}]];
approx[t_] := Evaluate[phi[t] /. DSolve[..., phi[t], t]];I then tried to calculate the differential equation for equation (4) using the two codes and got the following

Mathematica notebook screenshot: input lines defining exact[t_] with NDSolve and approx[t_] with DSolve for phi[t] on 0 to 20, followed by error messages about constraints, ReplaceAll, invalid variable, Solve and DSolve.

I then tried solving the differential equation using only NDSolve and get the following.

Mathematica notebook screenshot: input NDSolve of phi''[t] + 2 gamma phi'[t] + Sin[phi[t]] = 0 with phi[0] = Pi/2, phi'[0] = 0 over t 0 to 20, followed by a warning that the number of constraints is not equal to the differential order plus discrete variables, and an output showing the equation with the constraints.

I am a beginner in Mathematica, so I do not know what I have done wrong in the two formulas?
 
As the error messages say, your initial conditions are wrong. You have
Code:
phi[0]==0
and
Code:
phi[0]==Pi/2
which is not meaningful. You should have the first derivative of phi for one of those.
Code:
phi'[0]==...
 
Thanks Dale for your help and for looking over my code 👍

I have now tried plotting the differential equations again, but this time with phi'[0]=0 and with ##\gamma=0.1## and got the following.

Mathematica notebook showing two damped pendulum solutions plotted against t from 0 to 20, one blue and one orange; both oscillate with decreasing amplitude, and the orange curve drifts out of phase with the blue.

Is there any way that I can show in the plot which function is which?
 
Lambda96 said:
Is there any way that I can show in the plot which function is which?
There are different options. Try for instance
Code:
Plot[{exact[t],approx[t]},{t,0,20},PlotLegends->"Expressions"]
 
Thanks DrClaude for your help, this is exactly what I was looking for 👍