Skip navigation
The Australian National University

Laboratory 5 - Input/Output, Plotting and Numerical Methods

Aim

The aim of this lab is to get you familiar with input/output, plotting and numerical methods in Matlab.

Preparation

Please go through the slides for lectures 13-14. These cover input/output operations and plotting. As well, read chapters 8 and 11 of the textbook. Try out the examples covered in the lectures and the text book.

Also, go through the slides for lectures 15-16. These cover numerical methods. As well, read chapter 15 of the textbook. Try out the examples covered in the lectures and the text book.

Tools

The only tool you will need is the matlab programming environment. To use this, you will need to logon to a computer in one of the Computer Science labs, open up a terminal window and type the following from the command line:

matlab

Instructions - Input/Output and Plotting

Write matlab programs that perform the following tasks. Make sure that you save your answers in scripts. Run the scripts using the matlab tool. The following questions involve the use of plots and input/output.
  1. Given the following six functions: y1 = cos(x);
    y2 = 2*cos(x);
    y3 = 3*cos(x);
    y4 = 4*cos(x);
    y5 = 5*cos(x);
    y6 = 6*cos(x);
    Plot the above functions on a single plot, for values of x varying from 0 to pi, in increments of pi/100. Given appropriate x and y labels, and a title to your plot. Include a boxed legend to identify the functions.
  2. Save your workspace in a file called plots.mat. Now, clear the workspace and command window. Load the file that you just save and see what you get.
  3. Now, have a separate plot for each function, thereby generating six plots. Include all six plots in a single figure. Use the subplot function. Give each one a title and labels for the x and y axis. Have six different colors for the six plots. Use the set command for this purpose. The plot function returns a handle that you need to store in a variable. This is then passed to the set function.

Instructions - Numerical Methods

Exercise 1

The file data1.txt contains two columns of numbers. The first column represents the data for a vector x and the second column for a vector y. Read the file and create these vectors.

Create a new vector x1 having values in the range 1 to 120, with increments of 0.2. This is a vector with closer spacing and larger range than x.

Now, perform the following tasks.

  1. Find the y1 values corresponding to the x-values in x1, using linear interpolation. On the same figure, plot the original x and y values as red circles and x1 and y1 as a black line. What do you observe about the visible range of the new values?
  2. Repeat the experiment using cubic interpolation. What is different now in the plot and why?
  3. Now, use polyfit to find the coefficients of the third order polynomial that best fits the points represented by x and y. Use polyval to evaluate that curve at the x1 points. Plot as before.
  4. Approximate the derivative dxy = dy/dx for the vectors x and y using the diff function. Plot the dxy against x, using the backward, forward and central difference approximations. Note that for the central difference, you will need to find the mid-points of x.
  5. Find yp, the cummulative sum of the elements in dxy and add this to the above plot (numerical integration). What do you notice about the two curves? Why is this so?

Exercise 2

Consider a system represented by the following second-order ODE:

      d2y/dt2 + 2cdy/dt + y = h(t)
    
This can be re-written as a system of two first order ODE, with the substitution:
      y1 = y 
      y2 = dy/dt
    
The system of equations now reduces to:
      dy1/dt = y2
      dy2/dt = -2cy2 - y1 + h(t)

      with intial conditions y1(0) = a and y2(0) = b.
    
Solve this system using ode45, given the following:
      c = 0.15
      h(t) = sin(pi*t/5), 0<=t<=5
	   = 0 , t > 5

      y1(0) = 1 
      y2(0) = 0.
      The time interval is 0<=t<=35.
    
Plot the value of y1(t) for the given time interval.

Updated:  Thu Jan 17 13:56:50 EST 2013 / Responsible Officer:   JavaScript must be enabled to display this email address. / Page Contact:   JavaScript must be enabled to display this email address.