Harmonic Oscillators Using Octave

This is the second computer homework assignment. You get to this page by calling up netmath as usual. You can, if you are an expert, do the whole exercise by clicking on octave and modifying the text that is there. Hoever, if you look up in the right-hand corner, there is an address...something like http://www.ma.utexas.edu/users/wfs/ (etc). Put your cursor up there, and by using delete and backspace, erase everything after /wfs/ and type in uhlen/hmwk2.html . Type return, and you should be looking at this page! The full web address is http://www.ma.utexas.edu/ users/uhlen/hmwk2.html (no period). Now go on. You go back and forth on the page by moving the bar in the right-hand margin. Move the cursor to the bar, click on it and drag. Practise this, as you will be doing it quite a bit. Also, controll p and controll n are emacs commands which also move you around.

If y is a function of t and satisfies the following differential equation y'' + .1* y' + 9*y = 0; We discussed in class how to change a second equation into a system with only first derivative, but with two variables y=x(1) and y'=x(2). Remember that we are using x (1) and x(2) instead of the more usual subscripts that you might find in a text. We are sticking with our usual notion that the dependent variable is "t".This is encoded in the matrix equation:


     y  = x(1),
     y' = x(2)

so eliminating y altogether:


    x(1)' = x(2)
    x(2)' = -9*x(1) - .1 * x(2)


In Octave we define a vector function 'pend' which encodes this, and then call the 'lsode' linear solver. The t is ranging from values 0 to 40, with 200 steps. Then we plot the solution vector [x(1),x(2)] as two plots against t. The [0.1,0.2] correspond to an initial value of X. The initial value is at the first value of t (in this case 0) of the range requested.

The plot will have 2 curves in this case, and the 'line 1' corresponds to x(1), the 'line 2' corresponds to x(2). Remember x = [y,y'] so that line 1 correpsonds to y and line 2 to y'. The "-@" says plot lines and points. Octave has complete documentation. Sample A:
function xdot = pend(x,t) xdot(1) = x(2); xdot(2) = - x(1) - 0.1*x(2); end sol=lsode( "pend",[0.1, 0.2], t = linspace(0,40, 200)); plot( t, sol,"-@")

To see the plot, double click with the right-hand mouse button and wait. YOu get rid of the plot window by clicking on dismiss. Print by clicking on config and sending the plot to a printer. Please don't print extra plots.

Now that you have called up the plot and looked at it by double clicking, make your own by changing parts of the equations, the initial condition, the step size and the interval in "t" you want graphed. Notice that the program has taken you by the equation you were working on to put the plot window up. First use the side bar to scroll back to the equation. All the parts in blue are easily changed. Click with the right mouse button to get the cursor in the blue area. Then erase the numbers there using backspace and delete keys (watch so as to keep the semi-colons and parentheses intact so the program runs) and put your own in. Don't forget * for multiply. Mistakes are no problem as you can fix them in the same way you erased the original numbers. Now double click to see the new plot (and scroll back to get to the equation after you click on dismiss to get back to this page). Double click, sometimes misfires and need repeating. But if the lower window says "sent to octave" and then "result received", and no plot window comes up, then you made a wrong call. Look over your typing carefully. This program uses emacs commands, but you can get away with very few of them. If case you make a real mess, delete the file (put your cursor on file, click, and then click on "close"). Call up netmath again, and start over! The lab proctors can help you with the basic commands if you feel very stuck. Remember that if I can do it, you probably can as well!

Here is another preprogramed equation. Here, since we are studying only second order equations, I have graphed two equations together. I just took the standard harmonic oscillator and put in one equation with no dampening and the other with dampening. We are using the same initial values. There are four curves in the solution because we are still graphing both the solutions and the derivatives of the solutions. This version just graphs the curves. The "@" is missing from the plot command. If you put it back in, you can see points. Try changing the "40" below to 20. What did this do? What happens if you erase the -.2*x(4) and put back the +.2*x(4) with a plus sign. This is the dampening term. How does it affect solutions? Notice that the program automatically resizes the x(t) scale so as to give a nice picture. So the original reference solution changed depending on the sign in the dampening term. It helps understand scales if you keep a reference solution. (See the second homework problem). Sample Program B:
function xdot = bruss(x,t) xdot(1) = x(2); xdot(2) = -x(1); xdot(3) = x(4) ; xdot(4) = -x(3) - .2* x(4); endfunction sol=lsode( "bruss",[0, 1,0,1], t = linspace(0,40, 200)); plot( t, sol)

Now we come to the fanciest version. This will graph three solutions together. I have given you three versions of the same equation. The first graphs all six curves without points, and the second graphs only the solutions (not the derivatives) with points. Sample Program C:
function xdot = bruss(x,t) xdot(1) = x(2); xdot(2) = -x(1)- sin(1.5*t); xdot(3) = x(4) ; xdot(4) = -x(3) - sin(1.2*t)); xdot(5) = x(6); xdot(6) = -x(5) - sin(1.1*t); endfunction sol=lsode( "bruss",[1,0,1,0,1,0], t = linspace(0,80, 400)); plot( t, sol) Sample Program C':
function xdot = bruss(x,t) xdot(1) = x(2); xdot(2) = -x(1) - sin(1.5*t); xdot(3) = x(4) ; xdot(4) = -x(3) - sin(1.2*t); xdot(5) = x(6); xdot(6) = -x(5) - sin(1.1*t); endfunction sol=lsode( "bruss",[1,0,1,0,1,0], t = linspace(0,80, 400)); plot( t, [sol(:,1)';sol(:,3)';sol(:,5)'], "@")

1. Using the program A for a single equation, graph the solution to the equation y" + 4.2 y = 0 with y(0) = 0 and y'(0) = .5 in the interval [0,10]. Identify which curve is the y and which curve is its derivative. Graph y" - 4.2 y = 0 in the same interval with the same initial conditions. Don't print this graph! But why does the minus sign make such a difference?  Descibe the graph in words.

2.Sample program B has one equation which has a dampening factor and one which does not. Change the dampening factor to have the wrong sign. Don't print this graph! But why does a sign make so much difference?Describe in words this graph. Now put back the the original dampening factor in the second equation, and add a larger dampening factor to the first equation. At about what value does the equation look overdampened (no longer oscillates?). Print this out,(be sure to give the value of the dampening factor you have chosen) and identify all four curves. The abstract value where overdampening starts is for the value 2 of the coefficient that was originally .2 in the second equation. Why do you see a smaller value for over dampening?

3. Sample Program C represents resonance. Pick three values for the frequency of the input wave which look interesting to you, print out the graph,identify each curve, and explain in your own words what is happening. You might want to explain what happens when the input frequency is lower than the  frequecny of the
homogeneous part of the equation. In all the eqamples here, the  frequency is higher.