The special value of and e are built into Matlab They can be
The special value of and e are built into Matlab. They can be conveniently acessed by the built-in variables pi and e(as long as you haven’t overwritten them!).
a) Make a plot connecting the coordinates: (2, 5), (3,17), (4.5, 8), (5.5, 8) and (2, 17) by a line.
b) Plot a circle with the radius r = 2, knowing that the parametric equation of a circle is [x(t), y(t)] = [rcos(t), rsin(t)] for t = [0,2]
Solution
Row vectors: there are many ways of creating a vector
Explicit list
>> x=[0 1 2 3 4 5]; % What happens if you skip the semicolon?
>> x=[0,1,2,3,4,5]; % Inserting commas doesnt change anything
Using a:increment:b
>> x= 0:0.2:1; % same as x=[0 0.2 0.4 0.6 0.8],
>> x= a:x:b; % x=[a,a+x, a+2x, a+3x, . . . , b]
% that is, vector from a to b in increments of size x
% What happens if x is not a integer divisor of b-a?
Using linspace(a,b,n)
>> x= linspace(0,1,6); % vector containing 6 points on interval [0,1]
>> a=0;b=1;n=6; % Set variables
>> x= linspace(a,b,n); % vector containing n points on interval [a,b]
% Note: spacing is x = 1/(n 1)!
Using for loops
>> for i=1:10 % First example of a for loop. Note: 1:10=1:1:10
>> x(i)=i; % What happens if you skip semicolon??
>> end
>> a=0;b=1;n=10; delx=(b-a)/n; % Set variables
>> for i=1:n+1
>> x(i)=a+delx*(i-1); % index of x has to be an integer > 0
>> end
How long is the vector?
>> length(x)
>> d=size(x) % What are the entries in the matrix d?
2
Column vectors
Explicit list
>> x=[0;1;2;3;4]
Transposing a row vector
>> x=[0 1 2 3 4]’ % Vectors are matrices. A’=transpose(A)
Matrices
Explicit list
>> A=[1 1 1; 2 0 -1; 3 -1 2; 0 1 -1];
Special matrices
>> x=zeros(1,4), y=zeros(4,1)
>> x=ones(1,4), y=ones(4,1)
