October 5, 2004 --- Class 11 --- Running Adapt, Oscillatory Motion, Conservation of Energy in the Euler Algorithms Activities: Comparing secant and false position methods We has some more comments about the comparison of these methods but these comments and the output have been moved into notes for previous class. Solving for the height To find the height at which the ball must be dropped, a root finding algorithm is added to the code. It is based on the secant method. The secant method can be unstable, so the loop is terminated upon a maximum number of iterations or when the desired accuracy is obtained. This is done in the code adapt.c. The secant method is based on a linear fit to the function. The linear fit is used to estimate where the function vanishes. This estimate is used in subsequent estimates. It is easy to implement since you only need the value of the function, not its derivative. We used the code with different starting values and also adjusted the accuracy to which we want v(height)/v_naive(height) = 0.99 The code keeps adjusting the height from which it drops the object until the ratio of the velocity at which the object hits the ground, to the velocity it would have had if the acceleration were the constant g is 0.99. Chapter 5---Oscillatory Motion We used the code ~sg/chap5/sho2.c and executable sho2 to calculate the motion of a simple harmonic oscillator. This code is based on the Euler method. We found that energy is not conserved. We will see in the class after next that with the Euler-Cromer algorithm, the energy varies during the motion of the body, but it is not monotonically increasing as it is for the Euler method. The error is order dt for these methods, but the error for the Euler method is unstable, i.e., the errors grow with time. We can use awk to compare our numerical integration of the equations of motion with the exact answer. Here is an awk script check.a that returns the error for the case when omega=3 BEGIN{omega=3.0} {t=$1; y=$2; yexact=cos(omega*t); print t, (y-yexact) } The script may be found in ~sg/chap5/check.a. I like to use suffix .a to indicate when a file contains awk commands. Note that when you put awk commands in a file, you don't need all those confusing quotes to keep the shell from interpreting various characters like "{" and "}". An alternative is to omit the BEGIN line and define omega in the awk command. To define omega call awk this way: awk -f check2.a omega=3. filename In the same directory, you will find a shell script plot2.csh for putting two axis graphs on the same page. The -s in the second command keeps axis from trying to clear the page at the beginning of the second plot. The parentheses concatenate the output of the two axis commands so they are joined together in the pipe to the plot filter. For homework #3, the Euler-Richardson and Runge-Kutta methods will be used. Conservation of Energy in Euler algorithms By calculating the energy after a single step for the harmonic oscillator, we were able to show that for Euler algorithm the change in energy is proportional to the current energy, thus it is increasing exponentally. For the Euler-Cromer algorithm, the change in energy is proportional to the difference between potential and kinetic energy. (This should be derived by the student.) Since for a harmonic oscillator the time average of kinetic and potential energies is the same, the Euler-Cromer algorithm conserves energy on average, i.e., the energy ocsillates around the correct mean value.