MATLAB Tutorial

MATLAB as a calculator

123.234 + 234.345
ans =
  357.5790
5 / 3
ans =
    1.6667
5 / 3;
4 + 5 / 6 + 7
ans =
   11.8333
(4 + 5) / 6 + 7
ans =
    8.5000
(4 + 5) / (6 + 7)
ans =
    0.6923
4 * 5 / 6 * 7
ans =
   23.3333
4*5 / 6*7
ans =
   23.3333
(4 * 5) / (6 * 7)
ans =
    0.4762
123 ^ (1/3)
ans =
    4.9732
123 ^ (1/3);

Variables and assignments

x = 4 + 7
x =
    11
x = x + 1
x =
    12
x = x + 1
x =
    13
2 * x
ans =
    26
y = x / 3
y =
    4.3333

Variable names

sphere_radius = 30
sphere_radius =
    30
Sphere_radius = 35
Sphere_radius =
    35
R = 30
R =
    30

Scripts

clc
edit sphere_volume.m
type sphere_volume.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Experiments with sphere volumes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

R1 = 30;   % Outer sphere volume
R2 = 15;   % Set R2 to 15   BAD COMMENT!!

% Compute volumes
V1 = 4*pi*R1^3 / 3;
V2 = 4*pi*R2^3 / 3;

delta_V = V1 - V2   %  Compute hollow volume
sphere_volume
delta_V =
    9.8960e+04

Functions

Built-in Functions

sin(1)
ans =
    0.8415
abs(-3.14)
ans =
    3.1400
sqrt(3.14)
ans =
    1.7720
3.14 ^ (1/3)
ans =
    1.4643

Inline Functions

myfunc = @(x,y) x + y^2;
myfunc(2,3)
ans =
    11
myfunc(3,4)
ans =
    19

File-based Functions

edit myfunc2.m
type myfunc2
function z = myfunc2(x,y)
% My fancy function 

    % Better first compute the square
    y_squared = y^2;

    % Main output
    z = 2*x + y_squared;

end
myfunc2(2,3)
ans =
    13

For loops

Basic syntax

for i = 1:10
    i
end
i =
     1
i =
     2
i =
     3
i =
     4
i =
     5
i =
     6
i =
     7
i =
     8
i =
     9
i =
    10
1/i
ans =
    0.1000

Calculations inside the for loop

for i = 1:10
    1 / i
end
ans =
     1
ans =
    0.5000
ans =
    0.3333
ans =
    0.2500
ans =
    0.2000
ans =
    0.1667
ans =
    0.1429
ans =
    0.1250
ans =
    0.1111
ans =
    0.1000

Harmonic sum

edit compute_s.m
type compute_s.m
function sn = compute_s(n)
% Compute sn = 1/1 + 1/2 + ... + 1/n

    sn = 0;
    for k = 1:n
        sn = sn + 1/k;
    end
end
compute_s(10)
ans =
    2.9290
compute_s(5)
ans =
    2.2833
1/1 + 1/2 + 1/3 + 1/4 + 1/5
ans =
    2.2833
format long
1/1 + 1/2 + 1/3 + 1/4 + 1/5
ans =
   2.283333333333333
compute_s(5)
ans =
   2.283333333333333
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6
ans =
   2.450000000000000
compute_s(6)
ans =
   2.450000000000000
format short
compute_s(1000000)
ans =
   14.3927
compute_s(10000000)
ans =
   16.6953
compute_s(100000000)
ans =
   18.9979
compute_s(1000000000)
ans =
   21.3005

For loops with other step sizes

type compute_s_odd_only
function sn = compute_s_odd_only(n)
% Compute sn = 1/1 + 1/3 + 1/5 + ... + 1/n  (assuming n is odd)

    sn = 0;
    for k = 1:2:n
        sn = sn + 1/k;
    end
end
compute_s_odd_only(100000000)
ans =
    9.8455
for k = 10:-2:-5, k, end
k =
    10
k =
     8
k =
     6
k =
     4
k =
     2
k =
     0
k =
    -2
k =
    -4

Conditionals

Basic comparisons

123 == 124
ans =
   0
123 == 123
ans =
   1
123 ~= 123
ans =
   0
123 ~= 124
ans =
   1
123 < 124
ans =
   1
123 <= 124
ans =
   1
x = 5.5
x =
    5.5000
x > 3
ans =
   1
x < 7
ans =
   1
x > 3 && x < 7
ans =
   1
(x > 3) && (x < 7)
ans =
   1
(x >= 3) && (x <= 7)
ans =
   1
3 <= x <= 7
ans =
   1
x = 8.1
x =
    8.1000
(x >= 3) || (x <= 7)
ans =
   1
(x >= 7) || (x <= 3)
ans =
   1
x = 5.5
x =
    5.5000
(x >= 7) || (x <= 3)
ans =
   0
if (x >= 7) || (x <= 3)
    x
end
x = 8.1;
if (x >= 7) || (x <= 3)
    x
end
x =
    8.1000

Sum of Multiples - Find the sum of all the multiples of 3 or 5 below n

15 / 5
ans =
     3
mod(15, 5)
ans =
     0
mod(16, 5)
ans =
     1
edit sum_of_multiples.m
type sum_of_multiples.m
function s = sum_of_multiples(n)
% Find the sum of all the multiples of 3 or 5 below n

    s = 0;
    for i = 1:n - 1
        if mod(i,3) == 0 || mod(i,5) == 0
            s = s + i;
        end
    end
end
sum_of_multiples(10)
ans =
    23

While loops

edit compute_s_delta.m
type compute_s_delta.m
function sn = compute_s_delta(delta)
% Compute sn = 1/1 + 1/2 + ..., stopping when a term < delta

% Rule of thumb: Use for loop when you know the number of iterations,
% use while loop otherwise

    i = 1;
    sn = 0;
    term = 1 / i;
    while term >= delta
        sn = sn + term;
        i = i + 1;
        term = 1 / i;
    end
end
compute_s_delta(1e-6)
ans =
   14.3927
compute_s(1e6)
ans =
   14.3927

Break statement

type compute_s_delta_break
function sn = compute_s_delta_break(delta)
% Compute sn = 1/1 + 1/2 + ..., stopping when a term < delta

% Rule of thumb: Use for loop when you know the number of iterations,
% use while loop otherwise

    i = 0;
    sn = 0;
    while 1   % Warning! Infinite loop! But OK if you break out
        i = i + 1;
        term = 1 / i;
        sn = sn + term;
        if term < delta
            break
        end
    end
end
compute_s_delta_break(1e-6)
ans =
   14.3927

Newton iteration for computing sqrt(a)

edit mysqrt.m
type mysqrt.m
function x = mysqrt(a, epsilon)

x = 1;
while 1
    xnew = (x + a/x) / 2;
    x = xnew;
    if abs(xnew - x) < epsilon
        break
    end
end
format long
sqrt(3)
ans =
   1.732050807568877
mysqrt(3, 1e-15)
ans =
   1.732050807568877

Arrays

x = [3 2 -4 5]
x =
     3     2    -4     5
x = [3; 2; -4; 5]
x =
     3
     2
    -4
     5
x(1)
ans =
     3
x(2)
ans =
     2
x(end)
ans =
     5
length(x)
ans =
     4
for i = 1:length(x)
    x(i)
end
ans =
     3
ans =
     2
ans =
    -4
ans =
     5
x = zeros(1,100)
x =
  Columns 1 through 17
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 18 through 34
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 35 through 51
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 52 through 68
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 69 through 85
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 86 through 100
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
y = ones(1,100)
y =
  Columns 1 through 17
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 18 through 34
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 35 through 51
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 52 through 68
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 69 through 85
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 86 through 100
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
z = 1:100
z =
  Columns 1 through 17
     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16    17
  Columns 18 through 34
    18    19    20    21    22    23    24    25    26    27    28    29    30    31    32    33    34
  Columns 35 through 51
    35    36    37    38    39    40    41    42    43    44    45    46    47    48    49    50    51
  Columns 52 through 68
    52    53    54    55    56    57    58    59    60    61    62    63    64    65    66    67    68
  Columns 69 through 85
    69    70    71    72    73    74    75    76    77    78    79    80    81    82    83    84    85
  Columns 86 through 100
    86    87    88    89    90    91    92    93    94    95    96    97    98    99   100
z + y
ans =
  Columns 1 through 17
     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16    17    18
  Columns 18 through 34
    19    20    21    22    23    24    25    26    27    28    29    30    31    32    33    34    35
  Columns 35 through 51
    36    37    38    39    40    41    42    43    44    45    46    47    48    49    50    51    52
  Columns 52 through 68
    53    54    55    56    57    58    59    60    61    62    63    64    65    66    67    68    69
  Columns 69 through 85
    70    71    72    73    74    75    76    77    78    79    80    81    82    83    84    85    86
  Columns 86 through 100
    87    88    89    90    91    92    93    94    95    96    97    98    99   100   101
z * y'
ans =
        5050
dot(z,y)
ans =
        5050
y ./ z
ans =
  Columns 1 through 10
    1.0000    0.5000    0.3333    0.2500    0.2000    0.1667    0.1429    0.1250    0.1111    0.1000
  Columns 11 through 20
    0.0909    0.0833    0.0769    0.0714    0.0667    0.0625    0.0588    0.0556    0.0526    0.0500
  Columns 21 through 30
    0.0476    0.0455    0.0435    0.0417    0.0400    0.0385    0.0370    0.0357    0.0345    0.0333
  Columns 31 through 40
    0.0323    0.0312    0.0303    0.0294    0.0286    0.0278    0.0270    0.0263    0.0256    0.0250
  Columns 41 through 50
    0.0244    0.0238    0.0233    0.0227    0.0222    0.0217    0.0213    0.0208    0.0204    0.0200
  Columns 51 through 60
    0.0196    0.0192    0.0189    0.0185    0.0182    0.0179    0.0175    0.0172    0.0169    0.0167
  Columns 61 through 70
    0.0164    0.0161    0.0159    0.0156    0.0154    0.0152    0.0149    0.0147    0.0145    0.0143
  Columns 71 through 80
    0.0141    0.0139    0.0137    0.0135    0.0133    0.0132    0.0130    0.0128    0.0127    0.0125
  Columns 81 through 90
    0.0123    0.0122    0.0120    0.0119    0.0118    0.0116    0.0115    0.0114    0.0112    0.0111
  Columns 91 through 100
    0.0110    0.0109    0.0108    0.0106    0.0105    0.0104    0.0103    0.0102    0.0101    0.0100

Matrices

A = [3 5 6 7; -2 -3 -1 -4; 1 2 3 4]
A =
     3     5     6     7
    -2    -3    -1    -4
     1     2     3     4
A * ones(4,1)    % Matrix-vector product
ans =
    21
   -10
    10
A' * A          % Matrix-matrix product
ans =
    14    23    23    33
    23    38    39    55
    23    39    46    58
    33    55    58    81
B = A * A'
B =
   119   -55    59
   -55    30   -27
    59   -27    30
y = ones(3,1)
y =
     1
     1
     1
x = B \ y   % Solve Bx = y
x =
   -0.1429
    0.2857
    0.5714
B * x
ans =
    1.0000
    1.0000
    1.0000
B * x - y
ans =
   1.0e-14 *
         0
    0.1776
         0
A
A =
     3     5     6     7
    -2    -3    -1    -4
     1     2     3     4
A(2,3)
ans =
    -1
size(A,1)
ans =
     3
size(A,2)
ans =
     4
for i = 1:size(A,1)
    for j = 1:size(A,2)
        A(i,j)
    end
end
ans =
     3
ans =
     5
ans =
     6
ans =
     7
ans =
    -2
ans =
    -3
ans =
    -1
ans =
    -4
ans =
     1
ans =
     2
ans =
     3
ans =
     4
C = zeros(4,3)
C =
     0     0     0
     0     0     0
     0     0     0
     0     0     0

Plotting

x = [1,5,4,2]
x =
     1     5     4     2
y = [1,2,5,-1]
y =
     1     2     5    -1
plot(x,y)
x_square = [0,1,1,0,0]
x_square =
     0     1     1     0     0
y_square = [0,0,1,1,0]
y_square =
     0     0     1     1     0
plot(x,y, x_square,y_square)
plot(x,y,'k', x_square,y_square,'b')
plot(x,y,'k', x_square,y_square,'b', 'linewidth',2)
axis equal
grid on
xlabel('x-coordinate')
ylabel('y-coordinate')\
title('Sample image with lines')
hold on
plot([1,4], [3,2], 'r')
plot([1,4], [3,2], 'o')
plot([1,4], [3,2], 'ro')
plot([1,4], [3,2], 'ro:')

Plotting functions

f = @(x) sin(3*x) + 0.5*sin(5*x) - 0.1*x
f =
  function_handle with value:
    @(x)sin(3*x)+0.5*sin(5*x)-0.1*x
x = 0:0.1:10;
clf
plot(x, f(x))
g = @(x) sin(3*x) + 0.5 * x .*sin(7*x) - 0.1*x
g =
  function_handle with value:
    @(x)sin(3*x)+0.5*x.*sin(7*x)-0.1*x
plot(x, f(x), x, g(x))
legend('f(x)', 'g(x)')