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

Matlab Variables and Data Manipulation: Default Configuration, Naming, and Calculations - , Assignments of Computer Science

An introduction to using matlab for defining variables, performing calculations, and working with matrices. Topics covered include default window configuration, naming rules, scalar and vector assignments, simple data plots, and calculating with matlab. It also includes a practice exercise for testing allowed variable names.

Typology: Assignments

2009/2010

Uploaded on 02/24/2010

koofers-user-26o
koofers-user-26o 🇺🇸

10 documents

1 / 20

Toggle sidebar

Related documents


Partial preview of the text

Download Matlab Variables and Data Manipulation: Default Configuration, Naming, and Calculations - and more Assignments Computer Science in PDF only on Docsity! 1 CSCD 309 – Scientific Programming Module 2: The Matlab Environment (Chpt 2) © 2008-2009 Prentice Hall, Paul Schimpf All rights reserved . No portion of this presentation may be reproduced, in whole or in part, in any form whatsoever, without the express permission of the author. Students enrolled in this class are hereby granted permission to reproduce this presentation for personal use only. Default Window Configuration • clc will clear the command window • Octave opens with just a Command Window – commands are typed, interactively, into this window – once you hit enter, the command is processed – You can retrieve previous commands (including from previous sessions) using the up cursor, and can edit commands before entering them 2 Defining variables Scalar Row Vector Column Vector Note: "=" means "gets" or "is assigned" Matrix Assignment Operator • To define a variable, a, we might type a=1+2 – the expression on the RHS will be evaluated, and the result (3) will be assigned to the variable named "a" • In algebra the equation x=y+5 – means that both sides are the same – in Matlab (and many other programming languages) this is an instruction that tells the machine to store the result of the RHS calculation in a memory location called "x" • Is that really different? – Yes! For example, in algebra this statement must be false: x=x+1 – In Matlab that is perfectly legitimate, and replaces the value in variable x with a new value equal to x+1, which has the effect of adding 1 to whatever value x currently holds 5 Practice Exercise 2.2 • Which of the following names are allowed? note that you can test them with the isvarname– , iskeyword, and which commands – to get help on a command, type "help <command>" test Test if my-book my_book x x Thisisoneverylongnamebutisitstillallowed? 1stgroup group_one zzaAbc z34wAwy?12# sin log x x x x bad idea Calculations • You can use MATLAB like you’d use a calculator >> 9 + 10 • Order of precedence is algebraic - same as you’ve probably learned elsewhere • Same as your calculator – Parentheses first 5*(3+6) > 45 ans=19 – Exponentiation – Multiplication / division – Addition / subtraction • Whitespace is ignored = 5*3+6 => 21 5*3 + 6 => 21 6 Parentheses • Use only ( ) • { } and [ ] mean something different • Operators must be specified explicitly • Expressions are evaluated from left to right: 5*(3+4) not 5(3+4) 5*6/6*5 => 25 5*6/(6*5) => 1 Example: surface area of a cylinder r = radius r = 5 h = height h = 10 π r2 π r2 2π r * h )(222 2 hrrrhrSA +=+= πππ 7 Example: surface area of a cylinder )(222 2 hrrrhrSA +=+= πππ • Using Matlab as a glorified calculator is fine, but its real strength is in matrix manipulations Matrices • This is the basic data type of Matlab • Two-dimensional group of numbers arranged into rows and columns • A Single Value (Scalar) – is a matrix with one row and one column • Vector (One dimensional matrix) – is a matrix with a single row or a single column 10 Range Shortcuts The default increment is 1 but if , you want to use a different increment put it between the first and final values Autogenerated vector spacings • You can include mathematical operations inside a matrix definition statement: a = [0: pi/10: pi] • linspace(x1, x2, n) – generates a vector with n entries, linearly (equally) spaced from x1 to x2 (inclusive) >> linspace(1, 10, 3) ans = 1.0000 5.5000 10.0000 • logspace(x1, x2, n) – generates a vector with n entries, with logarithmically equal spacing from 10x1 to 10x2 >> logspace(1, 3, 3) ans = 10 100 1000 11 Mixed calculations • Matrices can be used in many calculations with scalars • Addition and subtraction are straightforward matrices (and vectors) must be the same size to be added or– subtracted – a scalar is added to or subtracted from each element of a matrix of vector • Multiplication (and division) are a little trickier – a scalar multiplies or divides every element of a matrix or vector – for matrices, * means to perform a matrix multiplication, which requires that the inner dimensions must agree – IOW, if matrix A is (m x n) and matrix B is (p x q), then A*B can only be performed if n==p, and the result is an m x q matrix – the same holds for vectors, the only distinction being that one of the dimensions is 1 – we'll talk about matrix division later Element-by-element • Element by element multiplies and divides between matrices are rare, but can be achieved with special operators: .* element-by-element multiply ./ element-by-element divide – the matrices must be the same size • Exponentiation: a^b – at least one of the two operands must be a scalar if a matrix is involved it must be square– , • Element-by-element exponentiation: a .^ b – if b is a scalar, then each element of a is raised to that power – if both operands are matrices or vectors, the dimensions much match 12 Repetitive Calculations • People experienced with general-purpose programming languages often struggle to take full advantage of Matlab's matrix capabilities – repetitive calculations can often be performed using matrix calculations – this is far more efficient than using a for loop • For example, assume you have a list of angles in degrees that you would like to convert to radians. – first put the values into a matrix – perform the calculation Matrix vs. For loop We’ll talk about programming constructs later 15 Scientific Notation It is important to omit blanks between the decimal number and the exponent . For example, MATLAB will interpret 6.022 e23 as two values (6.022 and 1023 ) Other Display Formats • Changing the display format affects all subsequent displays – format long results in 14 decimal digits – format bank results in 2 decimal digits – format short returns the display to the default 4 decimal digits 16 Really Big and Really Small • When numbers become too large or t ll t di loo sma o sp ay using the default format, they are displayed in scientific notation • You can force scientific notation for all numbers with: – format short e – format long e Common Scale Factor • For long and short formats, a common scale factor is applied to the entire matrix if some of the elements become very large, or very small This. scale factor is printed along with the scaled values. 17 Two other formats • format + displays only– the sign of the result (blank for zero) • format rat – approximates the result as a ratio of small integers. Saving Variables • You can save all or just some of your vars to a file • Matlab adds the extension .mat • Octave currently adds no extension (I suggest using .oct or .dat) • Matlab will start up in that directory when you double-click a .mat file • Octave doesn't presently do that, but you can change the default startup directory by editing the shortcut properties or putting a cd command in the startup file (c:\Program Files\Octave\.octaverc)
Docsity logo



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