Download presentation
Presentation is loading. Please wait.
Published byTheodora Christine Cummings Modified over 9 years ago
1
Copyright © 2005. The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm III Chapter 2 Numeric Arrays PowerPoint to accompany
2
Specification of a position vector using Cartesian coordinates. Figure 2.1–1 2-2 The vector p can be specified by three components: x, y, and z, and can be written as: p = [x, y, z]. However, MATLAB can use vectors having more than three elements.
3
This week Creating vectors Creating vectors row vs column vectors row vs column vectors colon (:) operator colon (:) operator linspace( ) linspace( ) vector math operations vector math operations finding roots of polynomials finding roots of polynomials simple 2D plotting simple 2D plotting Matrices Matrices creating and using script files creating and using script files
4
What is a vector? A series of values stored in a single variable A series of values stored in a single variable x = [1, 2, 5, 2, 5]; row vector x = [1, 2, 5, 2, 5]; row vector y = [1; 2; 5; 2; 5]; column vector y = [1; 2; 5; 2; 5]; column vector Only one dimension of data Only one dimension of data Two dimensions = Matrix Two dimensions = Matrix The term Array refers to both vectors or matrices The term Array refers to both vectors or matrices
5
Vectors can be used to represent A 3 dimensional force vector f = [3, 2, 9]; A 3 dimensional force vector f = [3, 2, 9]; An evenly spaced series of values An evenly spaced series of values t = [0:0.001:1]; t = [0:0.001:1]; 1000 samples spaced 1 millisecond apart 1000 samples spaced 1 millisecond apart A math formula applied to a series of values A math formula applied to a series of values wave = cos(2*pi*t); wave = cos(2*pi*t); A polynomial p = [1, 3, -2, 4] A polynomial p = [1, 3, -2, 4] representing x 3 + 3x 2 – 2x + 4 representing x 3 + 3x 2 – 2x + 4
6
Creating Vectors Typing values in square brackets Typing values in square brackets v = [1, 2, 3]; v = [1, 2, 3]; u = [1; 4; 9]; u = [1; 4; 9]; Using the colon operator Using the colon operator t = [1:0.1:10]; (1, 1.1, 1.2,... 9.9, 10) t = [1:0.1:10]; (1, 1.1, 1.2,... 9.9, 10) Using linspace Using linspace x = linspace(1,50, 200); ( 200 points from 1 to 50) x = linspace(1,50, 200); ( 200 points from 1 to 50) Using transpose ( ' ) to convert row to column Using transpose ( ' ) to convert row to column u = u'; turns u from column to row u = u'; turns u from column to row
7
Vector Math operations Addition, subtraction Addition, subtraction z = u + v; must have same dimensions z = u + v; must have same dimensions Multiplication, division by scalar Multiplication, division by scalar p = 3*u; q = v/5; p = 3*u; q = v/5; Element-by-element multiplication Element-by-element multiplication r = u.*v; dot operator is a must, often forgotten r = u.*v; dot operator is a must, often forgotten
8
Polynomial Roots Use vectors when you need to find roots of a polynomial Use vectors when you need to find roots of a polynomial A polynomial p = [1, 3, -2, 4] A polynomial p = [1, 3, -2, 4] representing f(x) = x 3 + 3x 2 – 2x + 4 representing f(x) = x 3 + 3x 2 – 2x + 4 The roots of a polynomial are the values of x such that f(x) = 0 The roots of a polynomial are the values of x such that f(x) = 0 Very useful in many math problems Very useful in many math problems
9
Plotting with Vectors The idea is to create a vector of x-values The idea is to create a vector of x-values Use a math formula to create y-values Use a math formula to create y-values or just enter x and y values manually or just enter x and y values manually Then plot(x,y); Then plot(x,y); use title('my plot title'); xlabel('text'); ylable('stuff'); to complete the plot use title('my plot title'); xlabel('text'); ylable('stuff'); to complete the plot
10
Matrices A matrix can be entered directly using ; to start new lines: A matrix can be entered directly using ; to start new lines: M = [1, 2, 3; 4, 5, 6; 7, 8, 9]; M = [1, 2, 3; 4, 5, 6; 7, 8, 9]; A matrix can be constructed from vectors: A matrix can be constructed from vectors: M = [ u', v' ]; M = [ u', v' ]; Matrices can be added, scaled, or element-by- element operated on just as vectors can Matrices can be added, scaled, or element-by- element operated on just as vectors can The colon (:) operator can select parts of a matrix The colon (:) operator can select parts of a matrix
11
Script files Matlab problem solutions should be developed in a script file to allow easy execution and debugging Matlab problem solutions should be developed in a script file to allow easy execution and debugging Create an m-file in your local workspace Create an m-file in your local workspace Put matlab statements in the file, save Put matlab statements in the file, save At the matlab command line, type the filename (without.m) and the statements in the file execute. At the matlab command line, type the filename (without.m) and the statements in the file execute. Great for all kinds of problem solving Great for all kinds of problem solving
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.