Back to tutorial index

# Lab 31 Solutions

## Introduction

function lab_solns()

set1()
set2()
set3()

end

Back to lab exercises.

Back to the top

## Problem set 1

function set1()
x = linspace(-1,1,21);
% Exercise 1
y = 4*x - 12
y =

Columns 1 through 7

-16.0000  -15.6000  -15.2000  -14.8000  -14.4000  -14.0000  -13.6000

Columns 8 through 14

-13.2000  -12.8000  -12.4000  -12.0000  -11.6000  -11.2000  -10.8000

Columns 15 through 21

-10.4000  -10.0000   -9.6000   -9.2000   -8.8000   -8.4000   -8.0000


% Exercise 2
y = tanh(x/10)
y =

Columns 1 through 7

-0.0997   -0.0898   -0.0798   -0.0699   -0.0599   -0.0500   -0.0400

Columns 8 through 14

-0.0300   -0.0200   -0.0100         0    0.0100    0.0200    0.0300

Columns 15 through 21

0.0400    0.0500    0.0599    0.0699    0.0798    0.0898    0.0997


% Exercise 3
y = -2*(x + 3).^2 + 5
y =

Columns 1 through 7

-3.0000   -3.8200   -4.6800   -5.5800   -6.5200   -7.5000   -8.5200

Columns 8 through 14

-9.5800  -10.6800  -11.8200  -13.0000  -14.2200  -15.4800  -16.7800

Columns 15 through 21

-18.1200  -19.5000  -20.9200  -22.3800  -23.8800  -25.4200  -27.0000


% Exercise 4
y = (3*x.^3 - 1)./(x.^2 + 3)
y =

Columns 1 through 7

-1.0000   -0.8365   -0.6967   -0.5814   -0.4905   -0.4231   -0.3772

Columns 8 through 14

-0.3498   -0.3368   -0.3332   -0.3333   -0.3312   -0.3211   -0.2974

Columns 15 through 21

-0.2557   -0.1923   -0.1048    0.0083    0.1473    0.3115    0.5000


% Exercise 5
y =  x.*exp(-x)
y =

Columns 1 through 7

-2.7183   -2.2136   -1.7804   -1.4096   -1.0933   -0.8244   -0.5967

Columns 8 through 14

-0.4050   -0.2443   -0.1105         0    0.0905    0.1637    0.2222

Columns 15 through 21

0.2681    0.3033    0.3293    0.3476    0.3595    0.3659    0.3679


% Exercise 6
y  =  1./(cos(x).^2 + 1)
y =

Columns 1 through 7

0.7740    0.7213    0.6732    0.6309    0.5948    0.5649    0.5410

Columns 8 through 14

0.5228    0.5101    0.5025    0.5000    0.5025    0.5101    0.5228

Columns 15 through 21

0.5410    0.5649    0.5948    0.6309    0.6732    0.7213    0.7740


% Exercise 7
y =  exp(-10*(x-2).^2)
y =

1.0e-04 *

Columns 1 through 7

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 8 through 14

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 15 through 21

0.0000    0.0000    0.0000    0.0005    0.0056    0.0556    0.4540


% Exercise 8
y = (x + 2).^(sin(pi*x))
y =

Columns 1 through 7

1.0000    0.9710    0.8984    0.8088    0.7261    0.6667    0.6395

Columns 8 through 14

0.6510    0.7079    0.8201    1.0000    1.2577    1.5895    1.9618

Columns 15 through 21

2.2993    2.5000    2.4812    2.2335    1.8316    1.3896    1.0000


end

Back to the top

## Problem set 2

function set2()
n = 100;
x = rand(n,1);

Back to the top

## Exercise 1 : mean(x)

s = sum(x)/n;
fprintf('Mean              : %24.16f\n',s);
fprintf('Mean (using mean) : %24.16f\n',mean(x));
Mean              :       0.4654066015696843
Mean (using mean) :       0.4654066015696843


Back to the top

## Exercise 2 : std(x)

mu = mean(x);
s = sqrt(sum((x-mu).^2)/(n-1));
fprintf('Standard deviation             : %24.16f\n',s);
fprintf('Standard deviation (using std) : %24.16f\n',std(x));
Standard deviation             :       0.2729461990986886
Standard deviation (using std) :       0.2729461990986886


Back to the top

## Exercise 3 : median(x)

y = sort(x);
m = (y(n/2) + y(n/2+1))/2;
fprintf('Median using ''sort''   :  %24.16f\n',m);
fprintf('Median using ''median'' :  %24.16f\n',median(x));
Median using 'sort'   :        0.5160182036979160
Median using 'median' :        0.5160182036979160

end

Back to the top

## Problem set 3

function set3()

R = 0.5;
N = 1000;
th = linspace(0,2*pi,N+1);
x = R*cos(th);
y = R*sin(th);

p = sum(sqrt(diff(x).^2 + diff(y).^2));

fprintf('Circumference of the circle is %24.16f\n',p);

end
Circumference of the circle is       3.1415874858795387


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_31_solns.m)

Published with MATLAB® 8.5