MATLAB Lesson 1
% Matrices and Vectors
clc, clearvars
x = 1:10 % Create a 1X10 matrix
% Clear variable from memory whos % to check the details of x
clearvars
% Clear command Window, clc clears all the text from the Command Window, resulting in a clear
screen. After running clc, you cannot use the scroll bar in the Command Window to see
previously displayed text. x’ % x transpose
clc
% Naming variables with showing output
you = 25, me = 10
% Naming variables without showing output
you = 25; me = 10;
x= linspace(20,50) % ask MATLAB to draw 100 terms from 20 to 50
x= linspace(0,100,101) % as MATLAB to draw 101 terms from 0 to 100 matrix
A = [ 1 3 ; 2 -10] % returns a two by two matrix as follows
B = [ 1 3 ; 2 -10 ; 88 99] % returns a matrix as follows
y = [12 50 -8 -100] A + 2 % returns a matrix by adding 2 to every element.
y = [12, 50, -8, -100]
every element.
rows by 2 cols, it cannot
B= zeros(5)
B=zeros(2,8)
clc, clearvars
y = [12 50 -8 -100]
y.^2
X=eye(3) %returns a 3x3 matrix I
clc, clearvars
A = ones(3) % create a 3X3 matrix with all 1
A = ones(3, 1) % create a 3 rows by 1 col matrix with all 1
clc, clearvars
clc, clearvars A = [ 5 3 4.2; 5 9 88]
A = [ 5 3 4.2; 5 9 88]
A (2,3) % to call the value for matrix A for the 2nd row and the 3rd col
% Changing a value from the matrix
clc, clearvars
clc, clearvars A = [ 5 3 4.2; 5 9 88]
A = [ 5 3 4.2; 5 9 88] A (1,1)=100
nd
row
clc, clearvars
A = [ 5 3 4.2; 5 9 88]
nd
col
Exercise [MaxVal, I] = max(y) %return the index of max value
a) What is the maximum value of the follo
b) What is the minimum of the fu
c) At what x-value does the maximum y-
d) What is y(20.7)
x_maxval = x(I)
Answer
clc, clearvars
x=linspace(0,5); % to create 100 terms from 0 to 5
y= (-(x-3).^2) + 10 %
plot(x,y) % x,y clc, clearvars
x=linspace(0,5);
y= @(x) (-(x-3).^2) + 10
y(20.2)
max (y)
min(y)