Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Lab 0

Submitted by:
Asim Bashir
Registration No.
2019-BME-109
Submitted to:
Dr. Rehan
Subject:
Biomechanics
Date:
February 02, 2022

BIOMEDICAL ENGINEERING DEPARTMENT


UNIVERSITY OF ENGINEERING AND TECHNOLGY,
LAHORE
CAMPUS NAROWAL
HELP EXERCISES :

1 . Using search feature in the upper right corner of matlab, find help on the following:

a. THE HYPERBOLIC COSINE FUNCTION

cosh
Hyperbolic cosine

Syntax
Y = cosh(X)

Description
The cosh function operates element-wise on arrays. The function's domains and ranges include
complex values. All angles are in radians.

Y = cosh(X) returns the hyperbolic cosine for each element of X.

b. Matrix inverse inv


Matrix inverse

Syntax
Description :
Y=inv(x) computes the inverse
Y = inv(X)

of matrix

Examples in matlab:
Compute the inverse of a 3-by-3 matrix.

X=
1 0 2
-1 5 0

0 3 -9

Y = inv(X)

Y=

0.8824 -0.1176 0.1961


0.1765 0.1765 0.0392

0.0588 0.0588 -0.0980

c. TIC and TOC

tic
>> help tic
tic Start a stopwatch timer.
tic and TOC functions work together to measure elapsed time.
tic, by itself, saves the current time that TOC uses later to
measure the time elapsed between the two.

TSTART = tic saves the time to an output argument, TSTART. The


numeric value of TSTART is only useful as an input argument
for a subsequent call to TOC.

Example: Measure the minimum and average time to compute a sum


of Bessel functions.

REPS = 1000; minTime = Inf; nsum = 10;


tic;
for i=1:REPS
tstart = tic;
sum = 0; for j=1:nsum, sum = sum + besselj(j,REPS); end
telapsed = toc(tstart);
minTime = min(telapsed,minTime);
end
averageTime = toc/REPS;
See also toc, cputime.

Reference page for tic

>>
toc :

>> help toc

toc Read the stopwatch timer.

TIC and toc functions work together to measure elapsed time.

toc, by itself, displays the elapsed time, in seconds, since

the most recent execution of the TIC command.

T = toc; saves the elapsed time in T as a double scalar.

toc(TSTART) measures the time elapsed since the TIC command that

generated TSTART.

Example: Measure the minimum and average time to compute a sum

of Bessel functions.

REPS = 1000; minTime = Inf; nsum = 10;

tic;

for i=1:REPS

tstart = tic;

sum = 0; for j=1:nsum, sum = sum + besselj(j,REPS); end

telapsed = toc(tstart);

minTime = min(telapsed,minTime);
end

averageTime = toc/REPS;

See also tic, cputime.

Reference page for toc

>> > help toc

toc Read the stopwatch timer.

TIC and toc functions work together to measure elapsed time.

toc, by itself, displays the elapsed time, in seconds, since

the most recent execution of the TIC command.

Basic Syntax and Command-Line Exercises 2.

Create a row vector of the even integers between 8 and 12 inclusive.


i. x = 8:2:12

x=

8 10 12

ii. Let x = [2 5 1 6].


a. Add 16 to each element y = x +
16
x=

2 5 1 6

Y=
18 21 17 22

b. Add 3 to just the odd-


index elements

y= x(1:2:end) + 3

x=

2 5 1 6

y=

5 4

c. Compute the square root of


each element y= sqrt(x)
x=

2 5 1 6

y=

1.4142 2.2361 1.0000


2.4495

d. Compute the square of


each element

y = x.^2
x=

2 5 1 6

y=

4 25 1 36

3. Let x = [3 2 6 8] and y = [4 1 3 5] (Note that x and y are row vectors)

a. Raise each element of x to the power


specified by the corresponding element
in a = x.^y
y=

4 1 3 5

a=

81 2 216 32768

b. Divide each element of y by


the corresponding element in x
b = y./x
y=

4 1 3 5

b=

1.3333 0.5000 0.5000 0.6250

c. Multiply each element in x by the


corresponding element in y, calling
the result "z".
z = x.*y
y=

4 1 3 5

z=

12 2 18 40

d. Add up the elements in z and assign


the result to a variable called "w" w =
sum(z)
y=

4 1 3 5
w=

72

4. Given a vector t, write down the MATLAB expressions that will correctly compute
the following:
t = 1:0.2:2

a. ln(2 + t + t2) a = log(2 + t + t.^2)

b. et(1 + cos(3t)) b = exp(t).*(1 + cos(3*t))


c. cos2(t) + sin2(t) c = cos(t).^2 +
sin(t).^2
d. tan-1(t) (this is the inverse
tangent function) d = atan(t)

e. cot(t) e = cot(t)
f. sec2(t) + cot(t) – 1
f. sec2(t)
+ cot(t) -
1
Matrix concatenations

Exercise
• Create the matrix whose first column is the numbers 1:10, the next column contains the
first 10 squares 1, 4, 9 ...100 and the third column contains the first 10 powers of 2: 2 4 8
16...1024.

>> a=[1:10]';

>> b=a.^2;

>> c=2.^a;

>> m=[a,b,c]

m=

1 1 2
2 4 4
3 9 8
4 16 16
5 25 32
6 36 64
7 49 128
8 64 256
9 81 512
10 100 1024

Plotting

• Plot the sin over a different interval

• x = 0:pi/120:2pi;

• y = sin(x); plot(x,y)

• title('plot of x over different interval');

• the
function
x^2
between
the
numbers -5
and 5 plot
x=-
5:0.2:5;
y = sin(x.^2); plot(x,y,'r*-');
title('plot of x.^2');
plot the function log(x) from 1 to 10 (be sure that your plots are nice and smooth)

Code:

x=1:0.1:10;
y=log(x); plot(y,x,'m')
title('function logx') xlabel('x')
ylabel('y') grid
on
Array Syntax and Manipulations :

1. Let x = [3 1 5 7 9 2 6]. For each of the following commands first think about what the result should
be and then type the command and verify your answer.
Verification in matlab as follows:

>> x = [3 1 5 7 9 2 6]

x=
3 1 5 7 9 2 6
>> x(3)

ans =

>> x(1:7)

ans =

3 1 5 7 9 2 6

>> x(1:end)

ans =

3 1 5 7 9 2 6

>> x(1:end-1)

ans =

3 1 5 7 9 2

>> x(6:-2:1)

ans =

2 7 1

>> x([1 6 2 1 1])

ans =

3 2 1 3 3

>> sum(x)

ans =

33
2. Given the array A = [ 2 4 1 ; 6 7 2 ; 3 5 9], provide the commands needed to:
a. assign the first row of A to a vector called x1
x1 = A(1,:)

b. assign the last 2 rows of A to an array called y y = A(end-


1:end,:)

c. compute the sum over the columns of A


c = sum(A)

d. compute the sum over the rows of A


d = sum(A,2)

3. Read the help on the function diag(). Generate a random vector d of length 100 and from this
vector generate a 100x100 matrix A = I + D, where I is the identity matrix and D is the diagonal
matrix with elements d. Verify that the matrix A looks correct in the 6x6 block on the top-left.

d = rand(100,1);
A = eye(100) +
diag(d);
A(1:6,1:6)

ans =

1.8147 0 0 0 0 0
0 1.9058 0 0 0 0
0 0 1.1270 0 0 0
0 0 0 1.9134 0 0
0 0 0 0 1.6324 0
0 0 0 0 0 1.0975
>>
4. Read the help on the function inv(). Copy paste the lines from the help page to your
notebook

inv
Matrix inverse
Syntax
Y = inv(X)
Description
Y = inv(X) computes the inverse of square matrix X. X^(-
1) is equivalent to inv(X).
x = A\b is computed differently than x = inv(A)*b and is recommended for solving systems of
linear equations.
Examples
Compute the inverse of a 3-by-3 matrix. X =
[1 0 2; -1 5 0; 0 3 -9]
X=

1 0 2
-1 5 0
0 3 -9
Y = inv(X) Y =

0.8824 -0.1176 0.1961


0.1765 0.1765 0.0392
0.0588 0.0588 -0.0980

5. Give the following commands to create an array called F: F = randn(5,10) F =

0.5377 -1.3077 -1.3499 -0.2050 0.6715 1.0347 0.8884 1.4384 -0.1022 -0.0301
1.8339 -0.4336 3.0349 -0.1241 -1.2075 0.7269 -1.1471 0.3252 -0.2414 -0.1649 -
2.2588 0.3426 0.7254 1.4897 0.7172 -0.3034 -1.0689 -0.7549 0.3192 0.6277
0.8622 3.5784 -0.0631 1.4090 1.6302 0.2939 -0.8095 1.3703 0.3129 1.0933
0.3188 2.7694 0.7147 1.4172 0.4889 -0.7873 -2.9443 -1.7115 -0.8649
1.1093 avg = mean(F) avg =

0.2587 0.9898 0.6124 0.7974 0.4601 0.1929 -1.0163 0.1335 -0.1153 0.5271

Flow Control
Exercises

1. Write a little program that checks if x=73 is prime. Do not use isprime. But you
might find mod or rem useful.

for x=[2 3 5 7 11 13 19]


if mod(73,x)==0
'73 is not a prime number'
else
'73 is a prime number'
break
end
break
break
end
result:
ans =

'73 is a prime number'


2. Find the sum of a vector x=[3 5 12 42 67] , use a for loop and don't use sum. Code:
x=[3 5 12 42 67]
sum = 0; for
n=
1:length(x)
sum = sum
+x(n);
end
sum
result:
sum =

129

3. Modify your program to find the first 20 prime numbers.


Code:

searchlimit = 70;
primes = 2;
for i = 1 : searchlimit
if mod(2+i, primes) ~= 0
primes = [primes; 2+i]
end
end

Result:
clc clear all close all x=71;
p=primes(x)

primes =

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
>>

SignalGeneration
TASK 1:

Specify a sample rate of 100 Hz in a variable named fs. Then create a signal named t that extends
from 0 to 1, with time steps that are 1/fs

fs=100;
t=0:1/fs:1;
s=sin(2*pi*f*t)
;

TASK 2
Create a sine signal named sig at the sample times in t. Use a frequency of 5. Then create a plot of sig
versus t. To add noise, you can create a vector of random numbers that's the same size as your signal:
randn(size(s))

Code:
fs=100; t=0:1/fs:1;
sig=sin(2*pi*5*t);
plot(t,sig,'r+--')
xlabel('time')
ylabel('sig') title('sine
signal') noise =
randn(size(sig));
TASK 3
Create a vector of random numbers named noise that is the same size as sig. Multiply the vector
by 0.1 to scale the vector.
clc

clear all
close all fs=100
t=0:1/fs:1;
sig=sin(2*pi*5*t)
noise =
randn(size(sig))
noise1=noise*0.1
TASK 4
Add the signal vector with the noise vector. Name the output sigNoisy. Then create a plot of
sigNoisy versus t.

Code:
fs=100
t=0:1/fs:1;
f=5;
sig=sin(2*pi*f*t); noise
= randn(size(sig));
noise=noise*0.1;
sigNoisy=sig+noise;
plot(t,sigNoisy,'ko-.');
title('sigNoisy');
xlabel('time');
ylabel('sigNoisy'); grid
on
TASK 5
Create a square signal named sq at the sample times in t. Use a frequency of 5. Then create
a plot of sq versus t.

clc clear
all
close all fs=100 t=0:1/fs:1;
sq=square(2*pi*5*t
)
plot(t,sq,'b');
xlabel('time');
ylabel('sq');
title('square signal');
Add noise to the square wave and plot the result

Code :

fs=100 t=0:1/fs:1;
sq=square(2*pi*5*t)
noise=randn(size(sq))
; noise=noise*0.1;
sqNoisy=sq+noise;
plot(t,sqNoisy,'c');
title('sqnoisy');
xlabel('time');
ylabel('sqsignal');
. Repeat all steps with sawtooth waveform

SAWTOOTH SIGNAL :

Code:
fs=100
t=0:1/fs:1; s=sawtooth(2*pi*5*t)
plot(t,s,'b*-');
xlabel('time');
ylabel('signal s'); title('sawtooth
signal');

Sawtooth with noise


fs=100
t=0:1/fs:1; s=sawtooth(2*pi*5*t)
noise=randn(size(s)); noise=noise*0.1;
sNoisy=s+noise; plot(t,sNoisy,'r*-');
xlabel('time');
ylabel('signal
sNoisy'); title('sNoisy
signal'); grid off

You might also like