Back to tutorial index

Lab Solutions


Topics in this lab

Introduction

clear all;
format short

Back to the top

Problem 1

A = ones(10,1)
A =

     1
     1
     1
     1
     1
     1
     1
     1
     1
     1

Back to the top

Problem 2

A = zeros(2,9);
A(1,:) = pi;
A(2,:) = exp(1);
A
A =

  Columns 1 through 7

    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416    3.1416
    2.7183    2.7183    2.7183    2.7183    2.7183    2.7183    2.7183

  Columns 8 through 9

    3.1416    3.1416
    2.7183    2.7183

Back to the top

Problem 3

A = rand(5,3)
A =

    0.3583    0.1251    0.0471
    0.6624    0.0640    0.0738
    0.4341    0.6606    0.4640
    0.7728    0.8542    0.3137
    0.2499    0.1970    0.0335

Back to the top

Problem 4

A = -1 + 2*rand(5,3)
A =

    0.3361   -0.1763   -0.8802
    0.3250    0.6082    0.5453
    0.5313   -0.5924   -0.0500
    0.0054   -0.7763   -0.0385
    0.0651    0.7034    0.2274

Back to the top

Problem 5

A = ones(3,4);
A(2,:) = -4
A =

     1     1     1     1
    -4    -4    -4    -4
     1     1     1     1

Back to the top

Problem 6

A = -1 + 2*rand(6,5);
s = sum(A);
A = [A; -sum(A)]
A =

    0.8544    0.7835   -0.7763   -0.1112   -0.7413
   -0.4206    0.3559   -0.3068    0.4537    0.4711
    0.2909   -0.1479    0.8913    0.9920    0.5959
    0.8223   -0.5849    0.2129    0.3267   -0.4371
    0.3563   -0.2772    0.6713    0.0124    0.5924
   -0.1017   -0.5801    0.0991    0.6858   -0.6493
   -1.8016    0.4507   -0.7914   -2.3594    0.1684

sum(A)
ans =

     0     0     0     0     0

Back to the top

Problem 7

z = ones(10,1);
A = [z 2*z 3*z 4*z 5*z 6*z 7*z 8*z 9*z 10*z]
A =

     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10

An easier way to do this is using the kron function

A = kron(ones(10,1),1:10)
A =

     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10

Back to the top

Problem 8

A = zeros(7,7);
A(2:end-1,2:end-1) = 1;
A(3:end-2,3:end-2) = 2;
A(4,4) = 3
A =

     0     0     0     0     0     0     0
     0     1     1     1     1     1     0
     0     1     2     2     2     1     0
     0     1     2     3     2     1     0
     0     1     2     2     2     1     0
     0     1     1     1     1     1     0
     0     0     0     0     0     0     0

Back to the top

Using a for loop

x = 5.4;
s = 0;
for n = 0:20,
    s = s + (-1)^n*x^(2*n)/factorial(2*n);
    approx(n+1,:) = [n,s abs(cos(x)-s)];
end
format long e
approx
approx =

                         0     1.000000000000000e+00     3.653071240573653e-01
     1.000000000000000e+00    -1.358000000000000e+01     1.421469287594264e+01
     2.000000000000000e+00     2.184940000000001e+01     2.121470712405737e+01
     3.000000000000000e+00    -1.258797680000000e+01     1.322266967594264e+01
     4.000000000000000e+00     5.344057262285720e+00     4.709364386343085e+00
     5.000000000000000e+00    -4.659217738948556e-01     1.100614649837490e+00
     6.000000000000000e+00     8.175554131886718e-01     1.828625372460371e-01
     7.000000000000000e+00     6.119169803570472e-01     2.277589558558746e-02
     8.000000000000000e+00     6.369020499460896e-01     2.209174003454883e-03
     9.000000000000000e+00     6.345211197852514e-01     1.717561573832960e-04
     1.000000000000000e+01     6.347038248470673e-01     1.094890443265051e-05
     1.100000000000000e+01     6.346922930730359e-01     5.828695988085997e-07
     1.200000000000000e+01     6.346929022515336e-01     2.630889894827959e-08
     1.300000000000000e+01     6.346928749228490e-01     1.019785700329123e-09
     1.400000000000000e+01     6.346928759769553e-01     3.432065742714485e-11
     1.500000000000000e+01     6.346928759416246e-01     1.010080907803967e-12
     1.600000000000000e+01     6.346928759426631e-01     2.842170943040401e-14
     1.700000000000000e+01     6.346928759426361e-01     1.443289932012704e-15
     1.800000000000000e+01     6.346928759426368e-01     2.109423746787797e-15
     1.900000000000000e+01     6.346928759426368e-01     2.109423746787797e-15
     2.000000000000000e+01     6.346928759426368e-01     2.109423746787797e-15

Back to lab exercises.

Back to the top

Get the code

Do you want to try the above code fragments on your own? Download the Matlab script that produces this page here. (lab_5_solns.m)

Powered by MathJax

Published with MATLAB® 8.5