Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Matching: a. Script _____ Shares its variables with the command window b. Fun

ID: 3739192 • Letter: 1

Question

1. Matching:
a. Script _____ Shares its variables with the command window
b. Function _____ Its variables are private by default
_____ Has a header definition line defining the inputs and outputs
2. Which can be run from the Matlab command window:
a. Script c. both a script and function
b. Function d. neither can be run from the command window
3. A script often uses the input( ‘Message’ ) function to prompt for input values. A script
can display messages with the disp( arg ). The strcat( str1, str2 ) concatenates two strings.
yourName = input( ?Enter your name: ? );
disp( strcat(?Hello ?, yourName ) );
Change the above code example to request and display your favorite color.
4. Write a template for a Matlab Function here:
5. The formula for the area of triangle is: A = ½ b h. Where b = base length, and h = height. The
function is used like this: area = areaTriangle( base, height );
Write a function named areaTriangle, to compute and return the area of a triangle given the
base and height as inputs.

Explanation / Answer

1)

a) Script --> Shares its variables with the command window

b) Function -->Has a header definition line defining the inputs and outputs

2) c ) both a script and function

You can run a function by calling it from the command window and provide appropiate arguments.

3) favColor = input('Favourite colour ? ', 's');

disp(strcat('Your favourite color is : ', favColor);

Note: 's' tells that the input will be of string type.

4)

function [output_arguments] = function_name(input_arguments)

function defination ......................;

end

5)

function [a] = areaTriangle( base, height)

a = 0.5 * base * height;

end