7
IT1005 Lab04 answer FILE Task 1 No What Do You See? & Personalized Explanation. (Maximum 100 words per answer, the shorter the better!) FOR LAB TA USAGE (DO NOT FILL IN THIS COLUMN) 1 F5. Error using CelciusToFahrenheit (line 7) Not enough input arguments. This is because there is no input argument (in) for the function CelciusToFarenheit. The code cannot work as there is no input to process an output 2 CelciusToFahrenheit(35) ans=95 There is now an input for the function CelciusToFarenheit. The input of (35) will undergo a multiplication by a factor of 9 and a division by a factor of 5 then 32 is added the value. 35*9/5+32=63+32=95 3 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 4 CelciusToFahrenheit(35 40 45) and its variants CelciusToFahrenheit(35 40 45) Error: Unexpected MATLAB expression. (35 40 45) is not a correct format. CelciusToFahrenheit(35, 40, 45)

8a_a0124541r_Douglas Tan (1)

  • Upload
    douglas

  • View
    213

  • Download
    1

Embed Size (px)

DESCRIPTION

Answer

Citation preview

Page 1: 8a_a0124541r_Douglas Tan (1)

IT1005 Lab04 answer FILE

Task 1No What Do You See? & Personalized Explanation.

(Maximum 100 words per answer, the shorter the better!)FOR LAB TA USAGE(DO NOT FILL IN THIS COLUMN)

1 F5.Error using CelciusToFahrenheit (line 7)Not enough input arguments.

This is because there is no input argument (in) for the function CelciusToFarenheit. The code cannot work as there is no input to process an output

2 CelciusToFahrenheit(35)ans=95

There is now an input for the function CelciusToFarenheit. The input of (35) will undergo a multiplication by a factor of 9 and a division by a factor of 5 then 32 is added the value.

35*9/5+32=63+32=95

3 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

4 CelciusToFahrenheit(35 40 45) and its variants

CelciusToFahrenheit(35 40 45)Error: Unexpected MATLAB expression.

(35 40 45) is not a correct format.

CelciusToFahrenheit(35, 40, 45)Error using CelciusToFahrenheitToo many input arguments.

There are too many input arguments because in this case there are 3 input arguments when the function can only deal with 1 input argument at a time.

Page 2: 8a_a0124541r_Douglas Tan (1)

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 = 95ans = 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’

6 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 What Do You See? & Personalized Explanation.(Maximum 100 words per answer, the shorter the better!)

FOR LAB TA USAGE(DO NOT FILL IN THIS COLUMN)

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

Page 3: 8a_a0124541r_Douglas Tan (1)

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 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’

7e 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 What Do You See? & Personalized Explanation.(Maximum 100 words per answer, the shorter the better!)

FOR LAB TA USAGE(DO NOT FILL IN THIS COLUMN)

8a What do you see?Code ‘ctof’In 35

The break point stops the script at the line before the break point. Though the condition in line 6 ‘ctof’ is satisfied6, line 7 is not executed and only values are stored.

8b What do you see now?In 35

The ‘step in’ steps into the ‘CelciusToFahrenheit’ function, butthe function is not executed and hence only an ‘in’ value is saved.

8c What happen?

Page 4: 8a_a0124541r_Douglas Tan (1)

The content of the workspace changes according to which function/script it is running, showing the variables stored for each particular script/workspace.

8d Conclude?The different variables are stored to each function and will not be used in other functions that they are not stored to.

Task 2Your better MATLAB code FOR LAB TA USAGE

(DO NOT FILL IN THIS COLUMN)find (m == target)

Page 5: 8a_a0124541r_Douglas Tan (1)

Task 3Your MATLAB code FOR LAB TA USAGE

(DO NOT FILL IN THIS COLUMN)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 enddisp(m);

(For Lab TA) Total marks for this lab = _______________