Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to MATLAB II - Modeling of Environmental Sciences | GEOG 410, Lab Reports of Geography

Material Type: Lab; Class: Modeling of Environmental Sciences; Subject: GEOGRAPHY; University: University of North Carolina - Chapel Hill; Term: Fall 2007;

Typology: Lab Reports

Pre 2010

Uploaded on 03/16/2009

koofers-user-62e
koofers-user-62e 🇺🇸

5

(1)

10 documents

1 / 7

Toggle sidebar

Related documents


Partial preview of the text

Download Introduction to MATLAB II - Modeling of Environmental Sciences | GEOG 410 and more Lab Reports Geography in PDF only on Docsity! Lab2 Introduction to Matlab II Date Due: Sep, 6th, 2008 Office Hour: 2:30 PM to 4:30 PM, Wednesday 1 Goal To learn how to make a simple 2-D plot in Matlab, solving polynomial equations, and create Script files, and then execute them in Matlab. 2 Plotting You will learn the most basic graphics commands. In this example, you will draw a simple line. In our course, we often put the results on a figure to see how the results distribute on the figure. It is more clear and evident. The Matlab commands for plotting are  plot creates a 2-Dline plot  axis changes the aspect ration of x and y axes  xlabel annotates the x-axis  ylabel annotates the y-axis  title puts a title on the plot In Lab 1, we learned how to assign a numerical value to a variable, as >>x=10 x= 10 In fact, a variable in Matlab can store for more than one value. For example, we can assign the integers from 1 to 10 to the variable x and y as >> x=1:10; >> y=1:10; The semicolon between the numbers indicates all integers between the two numbers. Matlab can not only perform many numerical computations, but also can create plots for you. This is called data visualization. In many cases, we do mot see a pattern in the data 1 by just look at the numbers, but if we plot it out on a paper, some features can jump out immediately. To create a plot, you need to start the plotting module. Click the ‘Start’ Menu on the lower left of the window, choose “MATLAB/Plot Tools” Fig 1. To start plot tools In the window on the upper-left named ‘Figure Palette’, choose 2D Axes Fig.2 The plot tools In the window on the right hand side, there is a button ‘Add data’, click it. A new window appears. 2 Line-styles: Make the same plot as above, but rather than displaying the graph as a curve, show the unconnected data points. To display the data points with small circles, use plot(x,y,’o’). Exercises 1 A simple sine plot: Plot 20,sin  xxy , taking 100 linearly spaced points in the given interval. Label the axes and put ‘Sine Plot created by yourname ’ in the title. Include all your commands and the Figure in your report. Tips: How to create a series of linearly spaced points? Matlab provides a function called “linspace(x,y,z)”, creates inclusive equally spaced data points between to two numerical values. The argument x is the value start value, and y is the end value, and z is the number of data points desired. For example, if you type >>x=linspace(1,100,11) You will get x = Columns 1 through 8 1.0000 10.9000 20.8000 30.7000 40.6000 50.5000 60.4000 70.3000 80.2000 90.1000 100.0000 Note: the numbers within the parenthesis of linspace() is separated by “,”, not “:”. Similarly you can take 100 data points between 0 and 2π as >> x=linspace(0,2*pi,100); Note: The is a “;” at the end. This “;” suppressed the output of the variable “x” on the command line. >> y=sin(x); 3 Solving Polynomial Equations We will learn how to get the roots of the polynomial. All we need to know is the coefficients of 1st, 2nd, 3rd, etc. ,order. The polynomial is written as: 000 1 1 1 1    xaxaxaxa n n n n  , and we can create a row vector: ],,,,,[ 01221 aaaaaaC nnn  . After this, we simply type ‘roots(C)’, the 5 solutions will show on the screen. We can try to get the solutions for the equation 043x2 x . From the equation, the vector C should be [1, 3, -4]. >> roots([1, 3, -4]) ans = -4 1 We then get the two roots of the equation. This is quite simple and easy. Exercises 2 Calculate the roots of the following polynomial equations:  0157x-2 2 x  013x3 23  xx  021-3x 2  Include all your commands and results in your report. 4 Creating Script Files A script is a collection of Matlab commands that are put together to achieve a more complicated computation. The file must be saved with a ‘.m’ extension to its name, thereby making it an M-file. A script file is executed by typing its name (without the ‘.m’ extension) at the command prompt. 1. To create a script file, go to the main menu of Matlab, and select File/New/M File A new editor window will appear; 2. Write the following commands in the editor: x=1:10; x=1:10; plot(x,y); xlabel(‘Observed ‘); ylabel(‘Modeled’); title(‘The 1:1 Line’); 3. Click save to save the commands into an m-file as lineplot.m 4. Get back to Matlab main command window and type the name of the m-file without the file extension. 5. >> lineplot 6. You will see that your plot come out ready. 6 Exercises 3 Create a m-file that accomplishes Exercises 1. Save you m-file as sinplot.m in your student directory and include your plot in your lab report. Tips When you find difficult in using a function, for instance, plot, you can type help plot to see the help file in MATLAB. 7
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved