# Lab Solutions

## 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.3672    0.1126    0.4036
0.2393    0.4438    0.3902
0.5789    0.3002    0.3604
0.8669    0.4014    0.1403
0.4068    0.8334    0.2601



Back to the top

## Problem 4

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

-0.8264   -0.7616   -0.8518
-0.1412   -0.0099   -0.2122
-0.4854    0.4128   -0.9932
-0.4049   -0.5129   -0.5586
-0.1503    0.5701   -0.9974



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.6216    0.8021   -0.4703   -0.1388    0.2903
-0.7150    0.8788   -0.8633    0.9231    0.1046
-0.4638   -0.5576   -0.1273    0.5248   -0.5638
-0.6502   -0.0347   -0.6523   -0.9853    0.5447
-0.7227   -0.2480   -0.9478    0.3601   -0.5439
0.1978    0.0476    0.9094    0.4119   -0.2583
2.9757   -0.8882    2.1516   -1.0958    0.4264


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