MATLAB Week 2 Notes Scalar Variables, and Vectors: X 2 Abc 5 2+x W 2 Pi
MATLAB Week 2 Notes Scalar Variables, and Vectors: X 2 Abc 5 2+x W 2 Pi
Overview
year =
year =
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
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=
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