MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.

Slides:



Advertisements
Similar presentations
Introduction to MATLAB
Advertisements

EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Introduction to MATLAB 7
Introduction to MATLAB 7 for Engineers
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Introduction to MATLAB
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Part 1 Chapter 2 MATLAB Fundamentals
MATLAB Fundamentals.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Introduction to MATLAB
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
Chapter 1 An Overview Of MATLAB. The default MATLAB Desktop. Figure 1.1–1 1-2 More? See pages 6-7.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
INTRODUCTION TO MATLAB LAB# 01
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
A Brief introduction to MatLAB ($50 - $99 for students)
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Engineering Analysis – Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
EGR 115 Introduction to Computing for Engineers Introduction to MATLAB Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers.
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
BASICS OF MATLAB Engr.Mian Shahzad Iqbal Lecturer,Telecom Department University of Engineering & Technology Taxila.
An Introduction to Programming in Matlab Emily Blumenthal
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Copyright © The McGraw-Hill Companies, Inc. This work is only for non-profit use by instructors in courses for which this textbook has been adopted.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MATLAB The name of MATLAB stands for matrix laboratory. Starting a MATLAB Session -On Microsoft® Windows® platforms, start the MATLAB program by double-clicking.
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to MATLAB for Engineers, Third Edition
Basic operations in Matlab
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Lecture 2 Introduction to MATLAB
INTRODUCTION TO MATLAB
Announcements P3 due today
Presentation transcript:

MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group

MATLAB Window Command line History Workspace tab (active here) Current directory contents variables in workspace and their values

MATLAB Help  To obtain help with any known MATLAB command just type… >> help command_name  To search the help files just select MATLAB Help from the top level menu, hit the F1 key, or type… >> doc

Symbol Operation MATLAB form ^ exponentiation: a b a^b * multiplication: ab a*b / right division: a/b a/b \ left division: b/a a\b + addition: a + b a + b - subtraction: a - b a - b Scalar Arithmetic Operations

Entering Commands and Expressions  MATLAB retains your previous keystrokes.  Use the up-arrow key to scroll back back through the commands.  Press the key once to see the previous entry, and so on.  Use the down-arrow key to scroll forward. Edit a line using the left- and right-arrow keys the Backspace key, and the Delete key.  Press the Enter key to execute the command.

Command Description ans Temporary variable containing the most recent answer. eps Specifies the accuracy of floating point precision. i,j The imaginary unit  1. Inf Infinity. NaN Indicates an undefined numerical result. pi The number . Special Variables and Constants

Precedence Operation FirstParentheses, evaluated starting with the innermost pair. SecondExponentiation, evaluated from left to right. ThirdMultiplication and division with equal precedence, evaluated from left to right. FourthAddition and subtraction with equal precedence, evaluated from left to right. Order of Precedence

>> 8 + 3*5 ans = 23 >> 8 + (3*5) ans = 23 >>(8 + 3)*5 ans = 55 >>4^2­12­ 8/4*2 ans = 0 >>4^2­12­ 8/(4*2) ans = 3 Examples of Precedence

>> 3*4^2 + 5 ans = 53 >>(3*4)^2 + 5 ans = 149 >>27^(1/3) + 32^(0.2) ans = 5 >>27^(1/3) + 32^0.2 ans = 5 >>27^1/3 + 32^0.2 ans = 11 Examples of Precedence (continued)

CommandDescription clc Clears the Command window. clear Removes all variables from memory. clear v1 v2 Removes the variables v1 and v2 from memory. exist(‘var’) Determines if a file or variable exists having the name ‘ var ’. quit Stops MATLAB. Commands for managing the work session

who Lists the variables currently in memory. whos Lists the current variables and sizes, and indicates if they have imaginary parts. :Colon; generates an array having regularly spaced elements.,Comma; separates elements of an array. ;Semicolon; suppresses screen printing; also denotes a new row in an array....Ellipsis; continues a line. Commands for managing the work session (continued) CommandDescription

Practice…  Find the circumference and area of a circle of radius = 2.5 mm  Find the surface area and volume of a sphere of radius 17.2 mm  Use help to learn the difference between the built-in MATLAB functions cos() and cosd()

Arrays…

How to Create Arrays  You can use the colon ( : ) operator together with the comma and semicolon to create arrays  Colon – create a sequence of numbers in a row  Comma – separate listed elements of a single row, can also use an empty space  Semicolon – separates rows >> p = [1:3;4,5,6;7 8 9] p = >> r = [5:5:35;35:-5:5] r =

Array Functions  You can find the size of an array with the size() function  You can find the length of a vector with the length() function  Read the help entries for each of these functions >>p = [5:5:35;35:-5:5]; >>size(p) ans = 27 >>r = [4,5 6]; >>length(r) ans = 3

Concatenation  You can easily create an array by concatenating two or more existing arrays >> p = ones(3,1) p = 1 >> q = zeros(3,1) q = 0 >> s = [p q q p] s =

Array Index  You may refer to a single element of an array by using the array index corresponding to the row and column where the number is located in the array >>w = [5:5:35;35:-5:5] ans = >>w(2,3) ans = 25  If the array is a vector, you need only specify the column number since there is only a single row and the row number is assumed to be 1 >>u = [ ]; >>u(2) ans = 7

Array Addressing  You can refer to ranges of elements in an array by using the standard index format elements = array(rows,columns)  Example >>w = [2:2:10;10:-2:2]; w = >>w(1:2,3:4) ans = try this >>w(:,5)

Array Addressing Other examples of how to address an arbitrary selection of rows & columns from an array >> r = [5:5:35;35:-5:5] r = >> r(:,2:5) ans = >> r(:,[2,5]) ans =

Example – Linear Indexing What happens when a single index is used to address elements of a matrix? >> r r = >> r(2) ans = 35 >> r(5) ans = 15 >> r([2 7 13]) ans =

Assignment You can store the results of a calculation in a specified location in an array for i = 1:10, s(i,1) = sqrt(5*i); end >> s = Note: the semicolon causes screen output to be suppressed! Only after the loop completes is the output specifically requested by typing the variable name

More Advanced Topics: Scripts, User-Defined Functions, Conditionals, Loops, Files

1.In the interactive mode, in which all commands are entered directly in the Command Window, or… 2.By running a MATLAB commands stored in a script file. This type of file contains MATLAB commands, so running it is equivalent to typing all the commands— one at a time—at the Command Window prompt. You can run the file by typing its name at the command line. You can execute commands in MATLAB in two ways:

What happens when you type func_name() 1.MATLAB first checks to see if func_name() is a variable and if so, displays its value. 2.If not, MATLAB then checks to see if func_name() is one of its own commands, and executes it if it is. 3.If not, MATLAB then looks in the current directory for a file named func_name.m and executes func_name() if it finds it. 4.If not, MATLAB then searches the directories in its search path, in order, for func_name.m and then executes it if found.

The MATLAB Script Editor / Debugger comments MATLAB commands save & execute

Keep in mind when using script files:  The name of a script file must begin with a letter, and may include digits and the underscore character, up to 31 characters  Do not give a script file the same name as a variable  Do not give a script file the same name as a MATLAB command or function; you can check to see if a command, function or file name already exists by using the exist() command

MATLAB User-Defined Functions function [output variables] = function_name (input variables)  Note: [output variables] is NOT an array  You may use multiple output variables separated by commas, but each will be output individually  Example: Working Session >> [s,t] = two_out(3,5) s = t = Function Definition function [p,q]=two_out(x,y) p = 17*x^2/y; q = y*x/17*x;

MATLAB Plotting >> x = [0:pi/10:2*pi]; >> y = sin(x); >> plot(x,y,'g.-','MarkerSize',15,'LineWidth',2) >> title('Example Plot of y = sin(x)'); >> xlabel('x (units)'); >> ylabel('y = sin(x)')

Plot Layout with subplot() >> x = [0:pi/10:2*pi]; >> y1 = sin(x); y2 = cos(x); >> subplot(2,1,1) >> plot(x,y1,'g.-','MarkerSize',15,'LineWidth',2) >> xlabel('x (units)'); >> ylabel('y_1 = sin(x)') >> subplot(2,1,2) >> plot(x,y2,'g.-','MarkerSize',15,'LineWidth',2) >> xlabel('x (units)'); >> ylabel('y_2 = cos(x)')

Conditional Statements if x < 3 color = ‘r’; else if x < 7 color = ‘b’; else if x < 11 color = ‘c’; else color = ‘m’; end if x < 3 color = ‘r’; elseif x < 7 color = ‘b’; elseif x < 11 color = ‘c’; else color = ‘m’; end

for Loops  Standard structure… for loop variable = min:inc:max statements end  Basic example… for k = 5:10:35 x = k^2 end k = 5, 15, 25, 35 x = 25, 225, 625, 1225 Note: if inc is not specified, it defaults to unity

Importing Data from a Text File  Using the load command… >> load subject_data.dat; >> s = load(‘subject_data.dat’); Reads ASCII data in columns Creates array named subject_data.dat Reads ASCII data in columns Creates array named s

Importing Data from a Text File (cont.)  Using the dlmread command… >> RESULT = DLMREAD(FILENAME,DELIMITER,R,C); Reads delimited ASCII data starting at row R and column C Creates array named RESULT

Importing Data from an Excel File [num,txt] = xlsread(‘excel_filename.xls’)  numeric data put into num variable  text data put into txt variable

Saving Data to ASCII File  Using the save command… >> save filename var1 var2… –ASCII –tabs; For example… >> B = [2 6; 5 7; 8 4]; >> save array_B.dat B –ASCII –tabs; Saves array B in the ASCII file array_B.dat Columns of B become tab-delimited columns of array_B.dat

Saving Data to Binary File  Using the save command… >> save filename var1 var2… For example… >> B = [2 6; 5 7; 8 4]; >> save array_B B Saves array B in the binary file array_B.mat Advantage: binary files load faster than ascii