{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
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 Matlab’s 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:
Image of page 1

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

View Full Document Right Arrow Icon
2 CME 100 ans = -6 4 4 ans = 0 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
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern