tutorial_2

tutorial_2 - CME 100, Fall 2010 Vector Calculus for...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CME 100, Fall 2010 Vector Calculus for Engineers Prof. Eric Darve Matlab Tutorial 2 1. M-Files Matlab can execute a sequence of commands stored in a text file named with extension .m . Such files are called M-files. Much of your work with Matlab will be in creating and refining M-files. There are two types of M-files: script files and function files . (a) Script File : In the Matlab window, click File New M- file. This will open Matlabs M-file editor and type in the following commands. clc; x=[1 2 3]; y=[-7 2 1]; x+y dot(x,y) % Dot product cross(x,y) % Cross product Save the file as vector.m . Note that the clc command clears the screen before the output of the commands in the script are displayed. The % symbol denotes a comment and everything that follows will not be executed by Matlab. Now that we have a script file, there are three ways to execute it Type the name of the script file in the command window. In this case you type vector . Press F5 in the M-file editor. Click the icon shown below in the M-file editor window. The output of the script file is the following: 2 CME 100 ans =-6 4 4 ans = ans =-4-22 16 You can suppress the output of a command by appending a semicolon. Note that, in our script, we have suppressed it in order to view the results. We can also store the output of a command in another variable and use the disp command to display the results as follows: clc; x=[1 2 3]; y=[-7 2 1]; sum = x+y; prod1 = dot(x,y); prod2 = cross(x,y); disp(sum); disp(prod1); disp(prod2); (b) Function File : Unlike a script file, a function file can accept multiple input arguments and return multiple output arguments. Consider the following simple example that uses a function to normalize a vector x that the user inputs through...
View Full Document

This note was uploaded on 08/24/2011 for the course CME 100 at Stanford.

Page1 / 7

tutorial_2 - CME 100, Fall 2010 Vector Calculus for...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online