0% found this document useful (0 votes)
87 views5 pages

Introduction To MATLAB & Function Manipulations

This document provides instructions for 13 MATLAB exercises involving: 1) Creating vectors, performing operations on vectors, and using logical indexing 2) Plotting trigonometric, parametric, and logarithmic functions 3) Finding prime numbers, computing series sums, and fitting data to models 4) Minimizing the cost of fencing around a given area The exercises cover basic MATLAB skills like vectors, loops, functions, plotting, and optimization.

Uploaded by

NoorLiana Mahmud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views5 pages

Introduction To MATLAB & Function Manipulations

This document provides instructions for 13 MATLAB exercises involving: 1) Creating vectors, performing operations on vectors, and using logical indexing 2) Plotting trigonometric, parametric, and logarithmic functions 3) Finding prime numbers, computing series sums, and fitting data to models 4) Minimizing the cost of fencing around a given area The exercises cover basic MATLAB skills like vectors, loops, functions, plotting, and optimization.

Uploaded by

NoorLiana Mahmud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

veb1062/ml Introduction to matlab Tutorial Sheet 1

Introduction to MATLAB & function manipulations

1. Start up matlab and get all the code snippets in the lecture notes working.

2. Perform the following using a single matlab command

(a) Create the vector consisting of the whole even numbers between 15 and 27.

(b) Create the vector consisting of the whole odd numbers between 15 and 27.

(c) Create the vector of (a) and (b) in decreasing order.

3. Let x = [2 5 1 6]. For this vector

(a) Create another vector containing 2 numbers by adding 3 to just the odd–index elements.

(b) Overwrite the even positions in vector x with the vector created in (a).

4. (Optional because we won’t spend too much time on strings) Strings can be indexed just like vectors. In addition,
for strings, the ’:’ operator works in an identical manner to that when used with numbers. For example
’a’:’c’ = [’a’,’b’,’c’]. Perform the following for strings

(a) Create the string of letters ’zyx...cba’, in other words, the alphabets in the reverse order.

(b) Interwine the two strings ’Hello!!’ and ’Goodbye’ to create ’HGeololdob!y!e’.

5. Perform the following using both for loops and vecotrisation (single line)

(a) The sum of integers from 1 to 100

(b) The sum of squares of the numbers from 1 to 10

(c) The sum of the powers of 0.5 to the numbers from 1 to 10.

6. For x = [1, 5, 2, 8, 9, 0, 1] and y = [5, 2, 2, 6, 0, 0, 2], evaluate and understand the


following

(a) x > y

(b) y > x

(c) x == y

(d) (x > y) | (y > x)

(e) x(y < 5)

7. For x = 1:10 and y = [3, 1, 5, 6, 8, 2, 9, 4, 7, 0], evaluate and understand the following

(a) (x>3) & (y < 5)

(b) x(x > 5)

1
veb1062/ml Introduction to matlab Tutorial Sheet 1

(c) x(y > 5)

(d) y(x <= 4)

(e) x( (x < 2) | (x >= 8) )

(f) y( (x < 2) | (x >= 8) )

8. For the vector x = [3, 15, 9, 12, -1, 0, -12, 9, 6, 1]

(a) Set to 0 the positive elements of x.

(b) Set to 3 the multiples of 3.

(c) Multiply by 5 the even elements of x.

(d) Create y from the elements of x that are > 10

(e) Set to 0 the elements less than the average. (Use the built–in function mean(x) to calculate the average.)

9. Re–create the following plots. Read up on xlabel(), ylabel() and legend() using the help command.

(a) Trigonometric curves


PSfrag replacements

1 sin(x)
1 cos(5x)
2

−1
0 2 4 6

(b) Lissajou curve defined parametrically as

x = sin(3t)

y = sin(4t)

0 ≤ t ≤ 2π

Lissajou curve

PSfrag replacements 0.5


0

−0.5

−1 0 1

2
veb1062/ml Introduction to matlab Tutorial Sheet 1

Circles

1.5
r =1
r = 0.5
1

0.5

y
−0.5

−1

−1.5

−1.5 −1 −0.5 0 0.5 1 1.5


x

PSfrag replacements
3

2
log(x)

0
0 5 10
x

(c) Circles

(d) Logarithm function

10. For the sum


N
X −1n+1
Sn = ,
n=1
2n − 1

(a) Evaluate Sn for N = 100.


π
(b) As N → +∞, S∞ = 4. Through trial and error of N , find the value of N for which the difference
between SN and S∞ is close with a difference ≈ 10−6 .

(c) For different N , ( N = 1, 10, 100,... , 106 , ... ), plot the error,  n , defined as

 n = |Sn − S∞ |

on a log scale. You may look up loglog() function to figure out how to plot on a log scale.

11. For a given number, x , to find whether it is a prime number, it needs to be checked whether it is a multiple

of all the integers less than x .

(a) Write a little program that checks if x=73 is prime. You may use mod or rem.

(b) Modify the program in part (a) to find the first 20 primes.

(c) Move the code in part (a) to a function named fIsPrime(x) and save it in fIsPrime.m. The function
checks whether x is a prime number and returns true if it is prime and false otherwise.

(d) Using your fIsPrime(x) function, through trial and error, find the 1000th prime.

3
veb1062/ml Introduction to matlab Tutorial Sheet 1

12. A driver’s manual provides the following data about the total time it takes to stop a car travelling at various
speeds.

Speed [kmhr−1 ] Reaction time [s] total stopping time [s]

16.1 0.75 1.57


32.2 0.75 2.45
48.3 0.75 3.25
64.4 0.75 4.33
80.5 0.75 5.88
96.6 0.75 7.57
112.7 0.75 9.61

(a) Recalling that if the acceleration is assumed constant

v = v0 + at,

where v is the speed, v0 is the initial speed, a is the acceleration and t is the time elapsed, compute
the constant deceleration of the car for each of the speeds in the table above in ms−2 . Remember that
the total stopping time includes the reaction time, during which the driver hasn’t applied the brakes.
Also, the car comes to a stop when v = 0. You may also use 1 hr = 3600 s.

(b) Compute the reaction distance and the braking distance recalling that

1
s = v0 t + at 2
2

(c) Plot a graph showing how the total stopping distance varies with speed.

(d) Mr Malik was driving at 60 km/hr inside utp when Ms Aida jaywalks in front of him. If Mr Malik
spots Ms Aida 15 m away, use your graph to decide whether Mr Malik stops his car before he hits Ms
Aida.

13. It is desired to build a rectangular fence around a 100 m2 area. The fence costs one ringgit per m. Let a and
b be the sides (in m) of the rectangular fence so that its area and perimeter are A = ab and F = 2a + 2b.
Obviously the cost of the entire fence is equal to F dollars.

It is desired that an ideal a and b be calculated that minimizes the cost of this fence. We will solve this by
searching for the value that minimize the cost.

(a) Define the vector a = 5:0.1:20.

(b) For this vector, compute b using the area and the total cost F .

(c) Plot a vs. F and decide graphically the value of a for which F is a minimum.

4
veb1062/ml Introduction to matlab Tutorial Sheet 1

(d) What is the corresponding value of b? What is the shape that minimizes the cost?

(e) Prove that the answer obtained graphically is correct using calculus. To prove this, first, express F in
terms of only a using the area limit. Second, find the minima of the resulting F , which should be as
a function of a only.

14. Taking f = cos(x), which is an even function and g = sin(x), which is an odd function, decide whether the
following are even or odd by plotting the resulting functions over −π ≤ x ≤ π

(a) f g

(b) f /g

(c) g/ f

(d) f 2

(e) g 2

(f) f (g(x))

(g) g( f (x))

15. For the following, plot the the original function (in red) along with the relevant shifting/scaling indicated
next to the function

(a) y = x 3 , Left 1, down 1

(b) y = 2 x − 7, Up 7
1
(c) y = x Up 1, right 1

(d) y = x 2 − 1 stretched vertically by a factor of 3


1
(e) y = 1 + x2
stretched horizontally by a factor of 3

16. Using appropriate plots, convince yourself that


 π
y = sin x + = cos(x).
2

You might also like