Professional Documents
Culture Documents
2019 Bme 109 Biomechanics Lab 0
2019 Bme 109 Biomechanics Lab 0
Submitted by:
Asim Bashir
Registration No.
2019-BME-109
Submitted to:
Dr. Rehan
Subject:
Biomechanics
Date:
February 02, 2022
1 . Using search feature in the upper right corner of matlab, find help on the following:
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.
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=
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.
>>
toc :
toc(TSTART) measures the time elapsed since the TIC command that
generated TSTART.
of Bessel functions.
tic;
for i=1:REPS
tstart = tic;
telapsed = toc(tstart);
minTime = min(telapsed,minTime);
end
averageTime = toc/REPS;
x=
8 10 12
2 5 1 6
Y=
18 21 17 22
y= x(1:2:end) + 3
x=
2 5 1 6
y=
5 4
2 5 1 6
y=
y = x.^2
x=
2 5 1 6
y=
4 25 1 36
4 1 3 5
a=
81 2 216 32768
4 1 3 5
b=
4 1 3 5
z=
12 2 18 40
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
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
• x = 0:pi/120:2pi;
• y = sin(x); plot(x,y)
• 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
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,:)
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.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.
129
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');