PLOTTING FUNCTION GRAPH USING JAVASCRIPT (Part 2)
Note: Please wait a bit for the math formulas to appear.(I use MathJax.js)
When t = 0 the mass was pulled so that the spring stretches a segment of $x_0=6$ (cm) then it was loosed. It's known that: \[\omega = \sqrt{\frac{k}{m}}= \frac{\pi}{4} = 0.7854 \quad\text{(rad/s)}\] and the length of the way for one direction movement (from left to right or from right to left) decreasing steadily to 0.8 (cm).
a- Establish the equation of motion x = x(t)
b- Using LaTeX to draw this oscillating graph.
A. Two half periods at the beginning
We will use the horizontal x axis in the direction of elongation of the spring. x = 0 is the position of the mass when it is not pulled or compressed.
The condition for the mass to stop is: \[\text{velocity }v=0\quad\text{and}\quad\ |x| \le d\quad\text{where }d = \frac{\mu.m.g}{k}\] If $x_0 > 0$ then initially the mass moves in the opposite direction to the positive direction of the x-axis: \[m.\frac{d^2x}{dt^2} = -k.x + F_{cl}\] Divide by m and change \[\frac{F_{cl}}{m} = \frac{F_{cl}}{k}.\frac{k}{m} = \omega^2.d\] \[\to\quad \frac{d^2x}{dt^2} + \omega^2.(x-d) = 0\] By using $X_1 = x-d$ we get the familiar equation of simple harmonic oscillation \[\frac{d^2X_1}{dt^2} + \omega^2.X_1 = 0\] \[\to\quad X_1 = x_1 - d = A_1.\cos(\omega.t + \varphi_1)\] \[\to\quad x_1 = A_1.\cos(\omega.t + \varphi_1) + d \quad\text{(1)}\] From the condition:
when t = 0 there should be $x(0) = x_0; v(0) = 0$, we get: \[\varphi_1 = 0; A_1 = x_0 - d\ \quad\text{(2)}\] The equation of motion (1) can be used only for the first time segment from t = 0 to $t_1$. We can find $t_1$ as below: \[v(t)= 0 \quad\to\quad -\omega.A_1.\sin(\omega.t) = 0\] \[\to\quad t_1 = \frac{\pi}{\omega}\] In the second time segment, the mass moves in the direction to the positive direction of the x-axis. \[m.\frac{d^2x}{dt^2} = -k.x - F_{cl}\] So we have \[\frac{d^2X_2}{dt^2} + \omega^2.X_2 = 0\] \[\to\quad x_2 = A_2.\cos(\omega.t + \varphi_2) - d \quad\text{(2)}\] B.Now we have to prove the following general formulas \[\varphi_i = \varphi_{i-1} \quad\text{(a)}\] \[A_i = A_{i-1} - 2.d \quad\text{(b)} \] Proving (a)
The function x (t) is a continuous function and, at the moment $t_i$ the function $x_{i}$ "changes" to $x_{i+1}$, so at this moment we have: \[x_{i} = x_{i+1} \quad\text{(c)}\] \[v_{i} = v_{i+1} \quad\text{(d)}\] \[v(t_i) = -\omega.A_i.sin(\omega.t_i + \varphi_i) = 0 \quad\to\quad \omega.t_i + \varphi_i = k.\pi \quad\text{(e)}\] From (d): \[\sin(\omega.t_i + \varphi_i) = \sin(\omega.t_i + \varphi_{i+1}) \quad\text{(f)}\] From (e) and (f): \[\sin(k.\pi) = \sin(k.\pi -\varphi_i + \varphi_{i+1})\] \[\to\quad -\varphi_i + \varphi_{i+1} = 0 \quad\text{OR}\quad\pi\] For $x_i$ and $x_{i+1}$ have the same sign, we must choose 0 ie \[\varphi_i = \varphi_{i+1} = \varphi_1 = 0 \quad\text{(aa)}\] From (aa) we can find the turning moments: \[t_2 = 2.\frac{\pi}{\omega}, t_3 = 3.\frac{\pi}{\omega} \quad\text{...}\]
Despite this difference, the formula for amplitude remains unchanged because: \[2.A_{i+1} = |x_i| - |x_{i+1}| = |x_i| - (2.d -|x_i|)\] \[\to\quad A_{i+1} = |x_i| - d = (A_i - d) - d = A_i - 2.d\]
Using overleaf.com for compiling tex file to pdf file:
Using Inkscape for converting pdf file to svg file:
Latex source code
No problem when we use Javascript
Here is the same graph created by javascript (Part 1)
\ newcommand {\ ww} {0.7854}
\ newcommand {\ xx} {30}
\ newcommand {\ dd} {2}
ymin = -31, ymax = 32,
then there are only integers and so the straight tail is gone.
An example for floating point error in Latex
6 - (2*7+1)* 0.4 + 0.4 is not equal to 0.4
After using of function round:
\FPeval{\xt}{round(\storeresult,2)};
all wrong results are corectted.
The latex code for this example:
\FPeval{\xt}{round(\xti,2)};
1- Problem 1:
A spring-mass system on horizontal surface performs damped oscillation due to Coulomb frictional force \[F_{cl}=\mu mg\].When t = 0 the mass was pulled so that the spring stretches a segment of $x_0=6$ (cm) then it was loosed. It's known that: \[\omega = \sqrt{\frac{k}{m}}= \frac{\pi}{4} = 0.7854 \quad\text{(rad/s)}\] and the length of the way for one direction movement (from left to right or from right to left) decreasing steadily to 0.8 (cm).
a- Establish the equation of motion x = x(t)
b- Using LaTeX to draw this oscillating graph.
2- Solution:
A. Two half periods at the beginning
We will use the horizontal x axis in the direction of elongation of the spring. x = 0 is the position of the mass when it is not pulled or compressed.
The condition for the mass to stop is: \[\text{velocity }v=0\quad\text{and}\quad\ |x| \le d\quad\text{where }d = \frac{\mu.m.g}{k}\] If $x_0 > 0$ then initially the mass moves in the opposite direction to the positive direction of the x-axis: \[m.\frac{d^2x}{dt^2} = -k.x + F_{cl}\] Divide by m and change \[\frac{F_{cl}}{m} = \frac{F_{cl}}{k}.\frac{k}{m} = \omega^2.d\] \[\to\quad \frac{d^2x}{dt^2} + \omega^2.(x-d) = 0\] By using $X_1 = x-d$ we get the familiar equation of simple harmonic oscillation \[\frac{d^2X_1}{dt^2} + \omega^2.X_1 = 0\] \[\to\quad X_1 = x_1 - d = A_1.\cos(\omega.t + \varphi_1)\] \[\to\quad x_1 = A_1.\cos(\omega.t + \varphi_1) + d \quad\text{(1)}\] From the condition:
when t = 0 there should be $x(0) = x_0; v(0) = 0$, we get: \[\varphi_1 = 0; A_1 = x_0 - d\ \quad\text{(2)}\] The equation of motion (1) can be used only for the first time segment from t = 0 to $t_1$. We can find $t_1$ as below: \[v(t)= 0 \quad\to\quad -\omega.A_1.\sin(\omega.t) = 0\] \[\to\quad t_1 = \frac{\pi}{\omega}\] In the second time segment, the mass moves in the direction to the positive direction of the x-axis. \[m.\frac{d^2x}{dt^2} = -k.x - F_{cl}\] So we have \[\frac{d^2X_2}{dt^2} + \omega^2.X_2 = 0\] \[\to\quad x_2 = A_2.\cos(\omega.t + \varphi_2) - d \quad\text{(2)}\] B.Now we have to prove the following general formulas \[\varphi_i = \varphi_{i-1} \quad\text{(a)}\] \[A_i = A_{i-1} - 2.d \quad\text{(b)} \] Proving (a)
The function x (t) is a continuous function and, at the moment $t_i$ the function $x_{i}$ "changes" to $x_{i+1}$, so at this moment we have: \[x_{i} = x_{i+1} \quad\text{(c)}\] \[v_{i} = v_{i+1} \quad\text{(d)}\] \[v(t_i) = -\omega.A_i.sin(\omega.t_i + \varphi_i) = 0 \quad\to\quad \omega.t_i + \varphi_i = k.\pi \quad\text{(e)}\] From (d): \[\sin(\omega.t_i + \varphi_i) = \sin(\omega.t_i + \varphi_{i+1}) \quad\text{(f)}\] From (e) and (f): \[\sin(k.\pi) = \sin(k.\pi -\varphi_i + \varphi_{i+1})\] \[\to\quad -\varphi_i + \varphi_{i+1} = 0 \quad\text{OR}\quad\pi\] For $x_i$ and $x_{i+1}$ have the same sign, we must choose 0 ie \[\varphi_i = \varphi_{i+1} = \varphi_1 = 0 \quad\text{(aa)}\] From (aa) we can find the turning moments: \[t_2 = 2.\frac{\pi}{\omega}, t_3 = 3.\frac{\pi}{\omega} \quad\text{...}\]
Proving (b)
According to (1) and (2) \[X_1 = x_1 - d = A_1.\cos(\omega.t + \varphi_1)\] \[X_2 = x_2 + d = A_1.\cos(\omega.t + \varphi_2)\] So if we move up the original point to the point x = d or move it down to x = - d then the graph for $X_1$ or $X_2$ is a part of sinusiodal function (**)
from $2.i\frac{\pi}{\omega}$ to $(2.i+1).\frac{\pi}{\omega}$
or from $(2.i+1).\frac{\pi}{\omega}$ to $(2.(i+1).\frac{\pi}{\omega}$ \[\to\quad 2.A_i = |x_{i-1}| + |x_i] \quad\text{(See the graph above)}\] According to the work-energy theorem we have: \[\frac{1}{2}.k.x_i^2 = \frac{1}{2}.k.x_{i-1}^2 - F_{cl}.(|x_{i-1}|+|x_i|)\] \[\to\quad |x_{i-1}| - |x_i| = 2.d\] \[\to\quad\ 2.A_{i-1} - 2.A_i = |x_{i-2}| + |x_{i-1}] - (|x_{i-1}| + |x_i|) = 4.d\] \[\to\quad A_i = A_{i-1} - 2.d \quad\text{(i= 2,3,4 ...)}\] Because $A_1 = x_0 - d$, we get: \[A_i = x_0 - d - (i-1).2d = x_0 - (2.i-1).d \quad\text{(i= 1,2,3, ...)}\] Finally we have: \[x_i = (x_0 - (2.i-1).d).\cos(\omega.t) + (-1)^{i-1}.d \quad\text{(i= 1,2,3, ...)}\]
C. Half periods at the end of oscillation
* According to (**) and the graph above: \[|x_1| = A_2 + d\ \quad\to\quad |x_{i-1}| = A_i + d \] $x_{i-1}$ is the displacement at the beginning of sinusoidal moving with the amplitude $A_i$ So \[A_i + d = x_0 - (2.i-1).d + d > d\] is the condition for the i-th time segment to be performed.
* Let's consider the case when the mass begins a half period with \[ d < |x_i| < 2.d \] now the mass can not move beyond the position x = 0 and we have \[\frac{1}{2}.k.x_i^2 = \frac{1}{2}.k.x_{i-1}^2 - F_{cl}.(|x_{i-1}|-|x_i|)\] \[\to\quad |x_i| = 2.d - |x_{i-1}| \]
According to (1) and (2) \[X_1 = x_1 - d = A_1.\cos(\omega.t + \varphi_1)\] \[X_2 = x_2 + d = A_1.\cos(\omega.t + \varphi_2)\] So if we move up the original point to the point x = d or move it down to x = - d then the graph for $X_1$ or $X_2$ is a part of sinusiodal function (**)
from $2.i\frac{\pi}{\omega}$ to $(2.i+1).\frac{\pi}{\omega}$
or from $(2.i+1).\frac{\pi}{\omega}$ to $(2.(i+1).\frac{\pi}{\omega}$ \[\to\quad 2.A_i = |x_{i-1}| + |x_i] \quad\text{(See the graph above)}\] According to the work-energy theorem we have: \[\frac{1}{2}.k.x_i^2 = \frac{1}{2}.k.x_{i-1}^2 - F_{cl}.(|x_{i-1}|+|x_i|)\] \[\to\quad |x_{i-1}| - |x_i| = 2.d\] \[\to\quad\ 2.A_{i-1} - 2.A_i = |x_{i-2}| + |x_{i-1}] - (|x_{i-1}| + |x_i|) = 4.d\] \[\to\quad A_i = A_{i-1} - 2.d \quad\text{(i= 2,3,4 ...)}\] Because $A_1 = x_0 - d$, we get: \[A_i = x_0 - d - (i-1).2d = x_0 - (2.i-1).d \quad\text{(i= 1,2,3, ...)}\] Finally we have: \[x_i = (x_0 - (2.i-1).d).\cos(\omega.t) + (-1)^{i-1}.d \quad\text{(i= 1,2,3, ...)}\]
C. Half periods at the end of oscillation
* According to (**) and the graph above: \[|x_1| = A_2 + d\ \quad\to\quad |x_{i-1}| = A_i + d \] $x_{i-1}$ is the displacement at the beginning of sinusoidal moving with the amplitude $A_i$ So \[A_i + d = x_0 - (2.i-1).d + d > d\] is the condition for the i-th time segment to be performed.
* Let's consider the case when the mass begins a half period with \[ d < |x_i| < 2.d \] now the mass can not move beyond the position x = 0 and we have \[\frac{1}{2}.k.x_i^2 = \frac{1}{2}.k.x_{i-1}^2 - F_{cl}.(|x_{i-1}|-|x_i|)\] \[\to\quad |x_i| = 2.d - |x_{i-1}| \]
Despite this difference, the formula for amplitude remains unchanged because: \[2.A_{i+1} = |x_i| - |x_{i+1}| = |x_i| - (2.d -|x_i|)\] \[\to\quad A_{i+1} = |x_i| - d = (A_i - d) - d = A_i - 2.d\]
3- Plotting graph using LaTeX
Using overleaf.com for compiling tex file to pdf file:
Using Inkscape for converting pdf file to svg file:
Latex source code
\documentclass[crop,tikz]{standalone}
\usepackage[utf8]{vietnam}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amsxtra,amssymb,amsthm,latexsy m,amscd,amsfonts}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{tkz-tab}
\usepackage{graphics}
\begin{document}
\newcommand{\ww}{0.7854}
\newcommand{\posit}{1}
\newcommand{\xx}{6}
\newcommand{\dd}{0.4}
\newcommand{\negat}{(-1)*\xx}
\begin{tikzpicture}\small
\begin{axis}[trig format plots=rad, axis line style=gray,
samples=120,
width=12.0cm,height=9.0cm,
xmin=0, xmax=9*pi,
ymin=-13, ymax=13,
axis equal,
axis x line=center,
axis y line=center,
xlabel=$t$,ylabel=$x$]
\ifnum\posit = 0
\addplot [->,semithick,cyan,mark=none]coordinates {(0,0) (0,\negat)}
node[right, pos= .5]{$x_{0}$};
\else
\addplot [->,semithick,cyan,mark=none]coordinates {(0,0) (0,\xx)}
node[right, pos= .5]{$x_{0}$};
\fi
\foreach \i in {0,1,...,2000} {
\pgfmathparse{(\xx - (2*\i+1)*\dd + \dd)> \dd ? 1:0}
% With latex code, sometimes the above command line gives a unwanted horizontal line
% at the end of graph.
% This straight line is actually a half cycle of sinusoidal oscillation with very
% small amplitudes. And this looks like it only happens when the value of x_0
% satisfies the condition |x_0| = N.(2d) + d (6 = 7.(2.0.4) + 0.4) (N=1,2,3,4...).
% This type of error is called "Floating point error".
% To fix it in Latex we need to use the round function,
% like in Javascript we already have the command lines
% mm1 = x0-(2*i+1)*d + d;
% mm = Math.round(mm1 * 100) / 100;
\ifthenelse{\pgfmathresult=1}{
\ifnum\posit = 0{
\addplot[red,domain=(\i)*pi/\ww:(\i+1)*pi/\ww,semithick]
{(abs(\xx) -(2*\i+1)*\dd)*cos(\ww*x + pi) - ((-1)^(\i))*\dd};
}
\else{
\addplot[blue,domain=(\i)*pi/\ww:(\i+1)*pi/\ww,semithick]
{(\xx-(2*\i+1)*\dd)*cos(\ww*x) + ((-1)^(\i))*\dd};
}
\fi
}
{}
}
\end{axis}
\end{tikzpicture}
\end{document}
Graph with floating point error
No problem when we use Javascript
Here is the same graph created by javascript (Part 1)
4- Remarks
If multiply $ x_0 $ and $ d $ by five times:\ newcommand {\ ww} {0.7854}
\ newcommand {\ xx} {30}
\ newcommand {\ dd} {2}
ymin = -31, ymax = 32,
then there are only integers and so the straight tail is gone.
An example for floating point error in Latex
6 - (2*7+1)* 0.4 + 0.4 is not equal to 0.4
After using of function round:
\FPeval{\xt}{round(\storeresult,2)};
all wrong results are corectted.
The latex code for this example:
\documentclass[crop,tikz]{standalone} \usepackage{tikz} \usepackage{fp} \newcommand{\ww}{0.7854} \newcommand{\posit}{1} \newcommand{\xx}{6} \newcommand{\dd}{0.4} \begin{document} \begin{tikzpicture} \foreach \i in {0,1,...,7} { \FPeval{\negat}{(-1)*\i + 8} \pgfmathparse{\xx - (2*\i+1)*\dd + \dd}\edef\storeresult{\pgfmathresult} \node at (0,\negat) {\storeresult}; } \foreach \i in {0,1,...,7} { \FPeval{\negat1}{(-1)*(\i+1)} \pgfmathparse{\xx - (2*\i+1)*\dd + \dd}\edef\storeresult{\pgfmathresult} \FPeval{\xt}{round(\storeresult,2)}; \node at (0,\negat1) {\xt}; } \end{tikzpicture} \end{document}
4- Final corrected code
To round $x(t_i)$ to 2 digits after the dot, in latex I use:\FPeval{\xt}{round(\xti,2)};
\documentclass[crop,tikz]{standalone} \usepackage[utf8]{vietnam} \usepackage[utf8]{inputenc} \usepackage{amsmath,amsxtra,amssymb,amsthm,latexsy m,amscd,amsfonts} \usepackage{pgfplots} \usepackage{tikz} \usepackage{tkz-tab} \usepackage{graphics} \usepackage{fp} \begin{document} \newcommand{\ww}{0.7854} \newcommand{\posit}{1} \newcommand{\xx}{6} \newcommand{\dd}{0.4} \newcommand{\negat}{(-1)*\xx} \begin{tikzpicture}\small \begin{axis}[trig format plots=rad, axis line style=gray, samples=120, width=12.0cm,height=9.0cm, xmin=0, xmax=9*pi, ymin=-13, ymax=13, axis equal, axis x line=center, axis y line=center, xlabel=$t$,ylabel=$x$] \foreach \i in {0,1,...,200} { \pgfmathparse{\xx - (2*\i+1)*\dd + \dd}\edef\storeresult{\pgfmathresult}% \FPeval{\xt}{round(\storeresult,2)}; \pgfmathparse{ \xt > \dd ? 1 : 0}; \ifthenelse{\pgfmathresult=1}{ \ifnum\posit = 0{ \addplot[red,domain=(\i)*pi/\ww:(\i+1)*pi/\ww,semithick] {(abs(\xx) -(2*\i+1)*\dd)*cos(\ww*x + pi) - ((-1)^(\i))*\dd}; } \else{ \addplot[blue,domain=(\i)*pi/\ww:(\i+1)*pi/\ww,semithick] {(\xx-(2*\i+1)*\dd)*cos(\ww*x) + ((-1)^(\i))*\dd}; } \fi } {\breakforeach} } \end{axis} \end{tikzpicture} \end{document}