Вы находитесь на странице: 1из 4

For Evaluation Only.

Copyright (c) by VeryPDF.com Inc


Edited by VeryPDF PDF Editor Version 2.2

MATLAB Week 2 Notes


Scalar Variables, and Vectors
Notes: MATLAB commands and output are shown in a bold red font

Overview

● Scalar variables are just one-dimensional vectors


● Vectors are one of the most important data structures in MATLAB
● Can be used to quickly and easily write certain commands that are tedious in other languages

Scalar Variables (Gilat 1.6)


A scalar variable is a numerical value or a computational expression. Variables are useful in many
circumstances. 1.) They simplify complex expressions. 2.) Allow generic code to be written. 3.)
Provide a nice way to assign meaning to a complex expression (i.e. built-in comments). The syntax for
defining a scalar is variable name = a numerical value, or a computable expression (Note: variable
names are case sensitive). Here are some examples:
x=2
abc = 5*2+x
w = 2*pi

Vectors (Gilat 2.1)


Row vector (you can use spaces or commas as delimiters)
Space delimiter (go to next column)
>> year = [1984 1985 1986 1987 1988]

year =

1984 1985 1986 1987 1988

Comma delimiter (go to next column)


>> year = [1984,1985,1986,1987,1988]

year =

1984 1985 1986 1987 1988

Column vector (use a semicolon to signify the end of a row)


Semicolon delimiter (go to next row)
>> pop = [126; 127; 130; 145]

pop =

126
127
130
145
Automatically generating vectors (Gilat 2.1)
A useful feature in MATLAB is the ability to generate vectors via a shorthand syntax.
>> mydata=[1:10]

mydata =

1 2 3 4 5 6 7 8 9 10

>> mydata=[1:2:10]

mydata =

13579

>> mydata=[2:2:10]

mydata =

2 4 6 8 10

Element-by-element operations (Gilat 3.4 – 3.6)


In traditional programming languages to evaluate a function/expression for a number of different
inputs, we would probably use a loop. Since MATLAB was built with vector calculations in mind, we
don't need to explicitly program a loop. This is where you're thinking: “Get to the point Paul and stop
drowning me with Computer Science jargon”. So, let's look at an example.
A ball is thrown from the roof of a building, and it's height is given by the following equation.
y(t) = 1000 + 70*t -16.1*t2
What is the height at t = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10?
This is easy to code in MATLAB. First, we need to define a time vector.
>> t=[0:10]

t=

0 1 2 3 4 5 6 7 8 9 10
Now we just type in the equation for the height of the ball.
>> y=1000+70*t-16.1*t.^2

y=

Columns 1 through 8:
1000.000 1053.900 1075.600 1065.100 1022.400 947.500 840.400 701.100
Columns 9 through 11:
529.600 325.900 90.000

If you look closely at the previous command, you will see that there is a dot (.) before the carat (^).
This tells MATLAB to perform an element-by-element calculation. In other words, calculate the
height of the ball for every time in the vector t. In general, if you want to perform element-by-element
calculations with vectors, you need to put a dot (.) before any multiplication, division, or power (^)
operators that involve vectors.
Now a few sample problems from your textbook (#'s 3.2, 3.5, and 3.7):
>> % 3.2
>> x=[-2.5:0.5:3]

x=

Columns 1 through 11
-2.5000 -2.0000 -1.5000 -1.0000 -0.5000 0 0.5000 1.0000 1.5000 2.0000 2.5000
Column 12
3.0000

>> y=(x.^2+1).^3.*x.^3

y=
1.0e+004 *
Columns 1 through 11
-0.5954 -0.1000 -0.0116 -0.0008 -0.0000 0 0.0000 0.0008 0.0116 0.1000 0.5954
Column 12
2.7000

>> % 3.5
>> h=0.9;
>> k=12.5;
>> x=[1 2 3 4];
>> y=[0.9 0.8 0.7 0.6];
>> z=[2.5 3 3.5 4];
>> T=(x.*y.*z)/(h+k)^(k/5)+k*exp(z./x+y)./(z.^h)

T=

164.2004 46.3924 26.1889 17.7944


>> % 3.7
>> % a.)
>> n=[1:1:100];
>> sum(1./(n.^2))

ans =

1.6350

>> pi^2/6

ans =

1.6449

>> % b.)
>> n=[1:1:1000];
>> sum(1./(n.^2))

ans =
1.6439

>> pi^2/6

ans =

1.6449

>> % c.)
>> n=[1:1:10000];
>> sum(1./(n.^2))

ans =

1.6448

>> pi^2/6

ans =

1.6449

Вам также может понравиться