You now have the skill to plot graphs of functions in Matlab (Assignment 1) and solve nonlinear differential equations numerically (Assignment 2). This week, you will learn about Matlab scripts and while loops to animate the solution of the plucked string problem. As before, if you feel unchallenged by this assignment, feel free to work on the slightly more difficult problem in italics at the end.
Problem
A perfectly elastic string is held at its midpoint, pulled vertically a height of 1 unit, then released as shown in Figure 1. The solution to the subsequent motion of the string can be described by two "phantom" waves, one traveling to the right (the dotted one on Figure 1) and one traveling to the left (the dashed one) at speed v. Write a Matlab script to plot the two phantom waves and their sum, which represents the actual motion of the string, over one complete cycle of the string's motion. The plot should be arranged to show the pinned ends of the string at the left and right of the graph. Using the drop box on the Classes file server, hand in your script only (with your username as the script name before the ".m").
Solution
Start by writing two Matlab functions, left.m and right.m, that take an arbitrary array and shift each element in the appropriate direction one position (you must make sure the arrays wrap). To do this, make use of Matlab's ability to create new arrays out of old ones; if a and b are arrays, then c = [a b] is a new array that is the concatenation of a and b. Also, an element in an array can be referred to by its position: a(n) is the nth element of array a and d = [a(1:n)] is an array that holds the first n elements of array a. Test the left and right functions first to make sure they work correctly, and then generate arrays (using the linspace function) that give the appropriate phantom wave shapes over the entire domain of repetition (twice the physical domain, as discussed in lecture). Write a script to take the two arrays, add them, and plot all three arrays over the physical domain with line types as shown in Figure 1 (use the axis command to fix the axis bounds). Finally, add a counting variable and a while loop to your script so that the plot is redrawn once for each shift of the phantom waves throughout a cycle of vibration.
Challenge assignment
Prompt the user for the location of the pluck point (as percent length from the left end of the string) and the initial pluck amplitude. The subsequent motion of the string should be animated as described above.