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.2581    0.7112    0.4242
    0.4087    0.2217    0.5079
    0.5949    0.1174    0.0855
    0.2622    0.2967    0.2625
    0.6028    0.3188    0.8010

Back to the top

Problem 4

A = -1 + 2*rand(5,3)
A =
   -0.9416   -0.5254   -0.5368
    0.8577   -0.0823   -0.0222
    0.4607    0.9262    0.2481
   -0.0228    0.0936    0.3583
    0.1571    0.0423   -0.2090

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.2651   -0.8026   -0.7865    0.7818    0.0000
    0.9760   -0.4763    0.3075   -0.3317   -0.0402
   -0.9245   -0.3293   -0.0117    0.3975    0.8094
    0.7703    0.3595    0.5581   -0.6044    0.2197
    0.8266   -0.7269    0.4301   -0.9389    0.2353
    0.5924    0.4425    0.8074    0.4881    0.7189
   -1.9756    1.5331   -1.3050    0.2075   -1.9433
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.6