MATLAB: Introduction - Part 1: Assignment

Contents

Problem 1

y = @(x) abs(x)*sin(x^2);

x1 = pi/3;
y1 = y(x1);
sprintf('y(%g) = %g',x1,y1)

x2 = pi/6;
y2 = y(x2);
sprintf('y(%g) = %g',x2,y2)
ans =

y(1.0472) = 0.931661


ans =

y(0.523599) = 0.141756

Problem 2

% Part (a)
v1 = [1; -3; 2];
v2 = [-1; 3; -2];
va = v1 + v2

% Part (b)
v3 = [2.6 3.5 -8.9];
v4 = [-2; -9; 5];
vb = 3*v3*v4 - 4/3

% Part (c)
m1 = [1 2; 2 1];
v5 = [-1; 1];
v6 = [1/3; -1/6];
vc = m1*v5 + 0.7*v6

% Part (d)
m2 = eye(3);
v7 = [10; 0; 5];
v8 = [2 -5 1];
m3 = [4 2 -5; 2 1 3; -5 3 2];
vd = (m2*v7).' - v8*m3
va =

     0
     0
     0


vb =

 -244.9333


vc =

    1.2333
   -1.1167


vd =

    17    -2    28

Problem 3

f = @(x) x.^2.*cos(pi*x)./((x.^3 + 1).*(x + 2));

% Part (a)
xa = 0:0.01:1;
fa = f(xa)

% Part (b)
xb = linspace(0,1);
fb = f(xb)
fa =

  Columns 1 through 9

         0    0.0000    0.0002    0.0004    0.0008    0.0012    0.0017    0.0023    0.0030

  Columns 10 through 18

    0.0037    0.0045    0.0054    0.0063    0.0073    0.0083    0.0093    0.0103    0.0114

  Columns 19 through 27

    0.0125    0.0135    0.0146    0.0156    0.0166    0.0176    0.0185    0.0193    0.0201

  Columns 28 through 36

    0.0208    0.0214    0.0220    0.0224    0.0227    0.0229    0.0230    0.0229    0.0227

  Columns 37 through 45

    0.0223    0.0218    0.0212    0.0204    0.0194    0.0182    0.0169    0.0154    0.0137

  Columns 46 through 54

    0.0118    0.0098    0.0076    0.0053    0.0027    0.0000   -0.0029   -0.0059   -0.0091

  Columns 55 through 63

   -0.0124   -0.0159   -0.0195   -0.0233   -0.0271   -0.0311   -0.0352   -0.0394   -0.0436

  Columns 64 through 72

   -0.0479   -0.0523   -0.0568   -0.0613   -0.0658   -0.0703   -0.0749   -0.0794   -0.0840

  Columns 73 through 81

   -0.0885   -0.0929   -0.0974   -0.1017   -0.1060   -0.1102   -0.1144   -0.1184   -0.1223

  Columns 82 through 90

   -0.1261   -0.1298   -0.1333   -0.1367   -0.1399   -0.1430   -0.1459   -0.1487   -0.1513

  Columns 91 through 99

   -0.1536   -0.1558   -0.1578   -0.1597   -0.1613   -0.1627   -0.1639   -0.1649   -0.1657

  Columns 100 through 101

   -0.1663   -0.1667


fb =

  Columns 1 through 9

         0    0.0001    0.0002    0.0005    0.0008    0.0012    0.0017    0.0024    0.0030

  Columns 10 through 18

    0.0038    0.0046    0.0055    0.0064    0.0074    0.0084    0.0095    0.0105    0.0116

  Columns 19 through 27

    0.0127    0.0137    0.0148    0.0158    0.0168    0.0178    0.0187    0.0195    0.0203

  Columns 28 through 36

    0.0210    0.0216    0.0221    0.0225    0.0228    0.0229    0.0230    0.0228    0.0226

  Columns 37 through 45

    0.0222    0.0216    0.0209    0.0200    0.0189    0.0177    0.0163    0.0147    0.0129

  Columns 46 through 54

    0.0110    0.0088    0.0065    0.0040    0.0014   -0.0014   -0.0044   -0.0076   -0.0109

  Columns 55 through 63

   -0.0143   -0.0179   -0.0216   -0.0255   -0.0294   -0.0335   -0.0377   -0.0420   -0.0463

  Columns 64 through 72

   -0.0507   -0.0552   -0.0597   -0.0643   -0.0689   -0.0735   -0.0781   -0.0826   -0.0872

  Columns 73 through 81

   -0.0917   -0.0962   -0.1006   -0.1050   -0.1093   -0.1134   -0.1175   -0.1215   -0.1254

  Columns 82 through 90

   -0.1291   -0.1327   -0.1362   -0.1395   -0.1426   -0.1456   -0.1484   -0.1510   -0.1534

  Columns 91 through 99

   -0.1556   -0.1577   -0.1595   -0.1612   -0.1626   -0.1638   -0.1649   -0.1657   -0.1663

  Column 100

   -0.1667