% ';' at the end of the line suppresses output
scalar=9.81;
rowvector=[4 1 5 6];
columnvector=[4;1;5;6];
t=[0:.5:10] % equally spaced vector from 0 to 10 with step of 0.5
clc % clears comand window (console)
clear % clear workspace (remove all variables)
Comments must start with '%' symbol
A=[1 2; 3 4] % simple 2 x 2 matrix
eyematrix = eye(6); % creates the 6 X 6 identity matrix.
zeromatrix = zeros(4,4); % creates an 4 x 4 matrix of zeros.
onematrix = ones(4,4); % creates an 4 x 4 matrix of ones.
randomuniform = rand(4,4); % creates a 4 x 4 matrix with entries from a uniform distribution
randomnormal = randn(4,4); % creates a 4 x 4 matrix with entries from a normal distribution
WARNING: Indexing in MATLAB starts with 1 !
thirdelement = rowvector(3) % get 3th element
B(1,1) = 123 % matrix access
submatrix = B(:,1:2) % slicing
A(1,:)=[]; % removing from matrix
B=[1 2; 3 4]
A=[1 2; 3 4]
C=A*B; % standard matrix multiplication
C=A.*B; % multiplication of corresponding elements in each matrix
sin; cos; tan; arctan; log; ln; exp ...
sinewave = sin(rowvector)
x = [0:.01:2*pi]
y = sin(x)
plot(x, y)
'Hold on' - MATLAB keeps plotting to same chart
hold on
x = [0:.01:2*pi]
y = cos(x)
plot(x, y)
xlabel('x'); % assigns X axis label
ylabel('sin(x)'); % assigns Y axis label
title('t MATLAB Plot'); % sets chart title
legend('sin(x)','Location','SouthWest'); % displays legend
mesh, waterfall, surf, meshz - 3D plotting functions
subplot - creates composite chart; e.g. subplot(2,2,1) creates chart containing 4 (2x2) sub-charts and plots to the first one
x=linspace(-3,3,50);
y=x;
[x,y]=meshgrid(x,y);
z=-5./(1+x.^2+y.^2);
subplot(2,2,1)
mesh(z); title('Mesh Plot')
subplot(2,2,2);
waterfall(z); title('Waterfall Plot');
hidden off;
subplot(2,2,3);
surf(z); title('Surf Plot');
subplot(2,2,4);
meshz(z); title('Meshz');
for m=1:100
num = 1/(m+1)
end
i=100;
while i > 10
i = i - 1;
end
% and: a & b
% or: a | b
% not-equal: a ~= b
% equal a==b
i=6; j=21;
if i >5
k=i;
elseif (i>1) & (j==20)
k=5*i+j;
else
k=1;
end
% definition (must be at the end of the script)
function ave = average(x)
ave = sum(x(:))/numel(x);
end
% calling
z = 1:99;
ave = average(z)