Problems solving this differential equation for a Pendulum with Mathematica

In summary, The conversation discusses difficulties with solving a specific differential equation in Mathematica. The user shares their code and asks for help in identifying the issue. Another user suggests adjusting the initial conditions and provides a solution for differentiating between the two functions in a plot.
  • #1
Lambda96
189
65
TL;DR Summary
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

Bildschirmfoto 2023-06-28 um 18.54.02.png


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

Bildschirmfoto 2023-06-28 um 18.58.49.png

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

Bildschirmfoto 2023-06-28 um 19.04.34.png

I am a beginner in Mathematica, so I do not know what I have done wrong in the two formulas?
 
Physics news on Phys.org
  • #2
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]==...
 
  • #3
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.

Bildschirmfoto 2023-06-28 um 21.44.11.png

Is there any way that I can show in the plot which function is which?
 
  • #4
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"]
 
  • #5
Thanks DrClaude for your help, this is exactly what I was looking for 👍
 
  • Like
Likes berkeman

Related to Problems solving this differential equation for a Pendulum with Mathematica

1. How do I set up the differential equation for a simple pendulum in Mathematica?

To set up the differential equation for a simple pendulum in Mathematica, you can use the following code:```mathematicag = 9.81; (* acceleration due to gravity *)l = 1; (* length of the pendulum *)theta[t_] := θ[t]; (* angular displacement as a function of time *)eqn = theta''[t] + (g/l) Sin[theta[t]] == 0; (* differential equation *)```This sets up the differential equation \( \theta''(t) + \frac{g}{l} \sin(\theta(t)) = 0 \).

2. How do I solve the pendulum differential equation numerically in Mathematica?

To solve the pendulum differential equation numerically, you can use the `NDSolve` function:```mathematicasol = NDSolve[{eqn, theta[0] == θ0, theta'[0] == ω0}, theta, {t, 0, T}];```Here, `θ0` is the initial angular displacement, `ω0` is the initial angular velocity, and `T` is the time span for the solution.

3. How can I plot the solution of the pendulum's motion in Mathematica?

After solving the differential equation numerically, you can plot the solution using the `Plot` function:```mathematicaPlot[Evaluate[theta[t] /. sol], {t, 0, T}, PlotLabels -> "Angular Displacement"]```This will give you a plot of the angular displacement \(\theta(t)\) over time.

4. How do I handle small-angle approximation for the pendulum in Mathematica?

For the small-angle approximation where \(\sin(\theta) \approx \theta\), you can simplify the differential equation:```mathematicaeqnSmallAngle = theta''[t] + (g/l) theta[t] == 0;```This linearizes the equation, making it easier to solve analytically or numerically.

5. What are common issues when solving the pendulum differential equation in Mathematica?

Common issues include:1. Incorrect initial conditions: Ensure that the initial angular displacement and velocity are correctly specified.2. Stiffness of the equation: For large angles, the equation can become stiff and may require more sophisticated numerical methods.3. Syntax errors: Ensure that all functions and variables are properly defined and that Mathematica syntax is correctly followed.4. Plotting errors: Ensure that the solution is correctly substituted into the plot function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
490
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
592
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
19
Views
500
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
531
Back
Top