Professional Documents
Culture Documents
8a - A0124541r - Douglas Tan
8a - A0124541r - Douglas Tan
Task 1
No
1
help CelciusToFahrenheit
This function takes in one parameter `in' (unit:
Celcius) and then convert it into `out' (unit:
Fahrenheit). The value of `out' after this function is
terminated will be returned to the caller of this
function.
help results in the paragraph of
comments(%comments) will be shown as a
description until there is another command or an
empty line
CelciusToFahrenheit([35 40 45])
ans = [95 104 113]
The input is a matrix, the function will calculate the
individual values of each column and return the
output values in vector form.
CelciusToFahrenheit([35, 40, 45])
ans = [95 104 113]
The input is also a row matrix, and the function will
return the
output values in each column.
5
Omit semicolon
out = 95
ans = 95
there is now no semicolon to suppress the output of
ans, this will results both the function output and
ans being expressed instead of just out
Change name
CelciusToFahrenheit(35)
ans = 95
CelciusToFarrenheit(35) will call the function which
name has remained unchanged in this case. Hence,
calling the function with the input of (35) will return
the output value of the function.
CtoF(35)
Undefined function 'CToF' for input arguments of
type 'double'.
CtoF(35) will call the function CtoF with an input of
35. Since there is no function with name CtoF that
exists, there is no function called and there is an
error.
No
7a
ConvertTemperature(35, 'ctof')
ans = 95
Function strcmp compare strings between the first
value and
the second value, and if they are the same it will
return a value
of 1 for true. By having the input of code ctof, it
satisfies the condition for if, the following
calculation will be applied out = 35 * 9 / 5 + 32, and
the value returned will be the answer in Fahrenheit.
7b
ConvertTemperature(95, 'ftoc')
The input of code ftoc satisfies the elseif condition,
as it is false for the if condition and the following
calculation is applied to the value inputted out =
(95 - 32) * 5 / 9, and the value returned will be the
answer in Celcius. The operators in the brackets will
be calculated first, before multiplication and division
in the order they are arranged.(BODMAS)
7c
ConvertTemperature(77, 'lalala')
Error using ConvertTemperature (line 11)
You entered wrong code! Choose ctof or ftoc!\n
The input of the code lalala does not satisfy either
the if or elseif conditions as it is false. Hence it will
go on to the else statement which will show an error
with the display You entered wrong code! Choose
ctof or ftoc!\n
7d
7e
ConvertTemperature('ctof', 100)
Error using ConvertTemperature (line 11)
You entered wrong code! Choose ctof or ftoc!\n
This values are placed in the wrong position. 100 is
entered as the code and will be false and not satisfy
either if and elseif statements. Which will go on to
the else statement which will show an error with the
display You entered wrong code! Choose ctof or
ftoc!\n
ConvertTemperature('lololo', 'ctof')
ConvertTemperature('lololo', 'ctof')
ans = [226.4000 231.8000 226.4000 231.8000
226.4000 231.8000]
The input of the code ctof satisfies the if condition,
and the value inputted re 108 for l and 111 for o
from the ASCII table, and hence
lololo is row vector of [108 111 108 111 108 111],
and the
function of ConvertTemperature apply the
calculation to each
column giving the answer as shown above.
No
8a
8c
8d
Task 2
Your better MATLAB code
find (m == target)
Task 3
Your MATLAB code
clear; clc;
n = input('enter n (n must be odd): ');
m = zeros(n); % the initial state of matrix 'm' of
size n x n, all zeroes
% now, modify matrix 'm' to make it a magic matrix!
if mod(n,2) == 0
error('n must be odd')
end
m(1,(n+1)/2) = 1;
row = 1; col = (n+1)/2;
for i = 2:n^2
if row == 1;
row = n; %if in the first row, start from
the bottom row
else
row = row-1; %if not in first row, move
up by one row
end
if col == n;
col = 1; %if in the last column, start
from first column
else
col = col+1; %if not in the last column,
move right by one column
end
if m(row,col) == 0;
m(row,col) = i; %if place is empty, put i
in
else
[row col] = find(m==i-1);
row = row+1;
m(row,col) = i; %if place is not empty,
put i below i-1
end
end
disp(m);