CSC 111 Assignment 1
CSC 111 Assignment 1
Computer Programming
(CSC111)
Assignment #1
Page 1 of 5
Exercise 1: Knowledge Review (0.6 Marks)
1) Write a short answer for the use of each of the following MATLAB commands. (0.2)
a) clear
b) whos
c) doc
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
3) Explain the use of each of the following MATLAB functions with the help of an example.
Note: Please use the MATLAB Command Window to test each function before answering.
a) sqrt (0.2)
b) log
c) log10
d) exp
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
Page 2 of 5
Exercise 2: Basic Practice Review (0.6 Marks)
Solution(a):
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
Solution(b):
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
Solution(c):
---------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
2) Below are some variable names and their assigned values. Identify which variable names are incorrect
and explain why they are invalid according to MATLAB's variable naming rules: (0.3)
a) x y = 10
b) value_1 = 7
c) 4num = 20
d) a#b = 5
Solution:
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
Page 3 of 5
Exercise 3: Coding MATLAB Scripts (0.8 Marks)
1) Write a MATLAB code to calculate and display the potential energy(PE=mgh) of an object given
the following values: (0.3)
o Mass: m=5
o Height: h=10
o Gravitational acceleration: g=9.8
Solution:
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
2) Write a MATLAB code to calculate the temperature in Celsius for a given Fahrenheit value.
Given the following value: (0.3)
o Fahrenheit F=80
5(𝐹−32)
Use the formula: 𝐶 =
9
Solution:
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
Page 4 of 5
3) Write a MATLAB code to find and display the roots of the quadratic equation 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 =
0, given the following values: (0.4)
o a=1
o b=−5
o c=6
Solution:
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
The End
Page 5 of 5