This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Mech 221: Computer Lab 1 Hand in the solutions to the three questions in the lab at the end of the lab. Success in many kinds of engineering requires skill at numerical approxi- mation. The computer labs this term build this skill. This lab in particular will build your understanding of and experience with numerical integration techniques. Some MATLAB commands you already know will be reinforced and some new commands introduced. Lab Learning Goals: After completing this lab, you should be able to use MATLAB to: generate vectors and define functions create plots using either the Command Window or the plot Graphical User Interface write a simple program, save it as an .m file, run it, edit it, and run it again perform numerical integration to a given accurancy Question 1: Defining functions and plotting Let us review how to define and plot functions in MATLAB. We will also show you how to interface with the plot GUI (Graphical User Interface). You may want to refer to the list of MATLAB commands given in the pre-lab in case you are not familiar with any of the commands used in this lab. Let us start with a few examples. First, we will plot the function f ( x ) = x 2 over the interval [0,1]. Here are some instructions on how to do that: Create a vector x of length 11 containing values between 0 and 1 using the linspace command as follows: >> x = linspace(0,1,11) The vector x corresponds to the end points of 10 equally spaced subin- tervals of [0,1]. 1 Now we will map these points to the function f ( x ) = x 2 and store the values in the vector y . Enter the following command: >> y = x.^2 Dont forget the dot in the command above, which tells MATLAB to take the square of each entry of x . The command y= x.*x also works, but x*x will give an error message (it tries to use matrix multiplication and the dimensions of the vector x do not allow this). We now have defined our function on the interval. Its time to plot it! We will plot it as a thin red line, with our data points indicated by xs. Type in the following command: >> plot(x,y,r-x) You should now have a window on your screen that contains a plot of y = x 2 from x = 0 to x = 1. The next example consists in plotting two different functions on the same axis. Specifically, we will plot the functions f 1 ( x ) = sin( x ) and f 2 ( x ) = sin( x 2 ) from x = 0 to x = 2 , using equally spaced points on the x-axis. The distance between successive points on the...
View Full Document
- Spring '10