LECTURE 02 - Lecture 2 MEEN 357 Engineering Analysis for...

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

View Full Document Right Arrow Icon
Lecture 2 MEEN 357 Engineering Analysis for Mechanical Engineers 1 Lecture-02-MEEN357-2009.doc RMB Lecture 2 Programming with MATLAB Chapter 3 of the textbook. Scripts and Functions MATLAB is a powerful programming language as well as an interactive computational environment. Files that contain code in the MATLAB language are called m-files . You create m-files using a text editor, then use them as you would any other MATLAB function or command. MATLAB comes with a pretty good editor that is tightly integrated into the environment. There are two kinds of m-files: Scripts , which do not accept input arguments or return output arguments. They operate on data in the workspace. Functions , which can accept input arguments and return output arguments. Internal variables are local to the function. There is no need to compile either type of m-file. You simply type in the name of the file (without the extension) in order to run it. Important Points : 1. An m-file should be saved in the path in order to be executed. The path is just a list of directories (folders) in which MATLAB will look for files. Use menus to see and change the path. 2. MATLAB is picky about how m-files are named. For example, if you put a space in the file name, it will not execute. Script m-file Section 3.1 of the textbook. Scripts are the simplest kind of m-file because they have no input or output arguments. They are useful for automating series of MATLAB commands, such as computations that you have to perform repeatedly from the command line. In the MATLAB command window, select File/New/m-file. o You can put any sequence of commands into a script file and save it. o If you type the name of the file at the command line, each of the commands in the script file are executed in sequence - this will give exactly the same result as typing each command in the command window. One important type of statement in an m-file is a comment, which is indicated by a percent sign % .
Background image of page 1

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

View Full DocumentRight Arrow Icon
Lecture 2 MEEN 357 Engineering Analysis for Mechanical Engineers 2 Lecture-02-MEEN357-2009.doc RMB o Any text on the same line after a percent sign is ignored (unless % appears as part of a string in quotes). Example: Create the m-file scriptdemo.m %Example 3.1 of the textbook clc clear all g=9.81;m=68.1;t=12;cd=.25; v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t) At the command window type: >> scriptdemo and MATLAB outputs v = 50.6175 The commands in a script are literally interpreted as though they were typed at the prompt. Example (A more complicated one that displays multiple graphs): Construct an m-file multigraph.m with the script % plot several types of graph theta = [0: .02:1]*pi; subplot(221), polar(theta,exp(-theta)) subplot(222), semilogx(exp(theta)) subplot(223), semilogy(exp(theta)) subplot(224), loglog(exp(theta)) You should use the MATLAB help utility to understand the role of each command in this file. The output is the set of four figures
Background image of page 2
Lecture 2 MEEN 357 Engineering Analysis for Mechanical Engineers
Background image of page 3

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

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

Page1 / 13

LECTURE 02 - Lecture 2 MEEN 357 Engineering Analysis for...

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

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