0
0
MATLABdata~10 mins

Input and output arguments in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function that takes one input argument.

MATLAB
function result = square[1]
    result = x^2;
end
Drag options to blanks, or click blank then click option'
A{x}
B[x]
C(x)
D<x>
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] instead of parentheses for inputs.
Using curly braces {} which are for cell arrays, not inputs.
2fill in blank
medium

Complete the code to define a function that returns two output arguments.

MATLAB
function [sum, diff] = addAndSubtract[1]
    sum = a + b;
    diff = a - b;
end
Drag options to blanks, or click blank then click option'
A(a, b)
B[a, b]
C{a, b}
D<a, b>
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets for input arguments.
Using curly braces or angle brackets which are invalid.
3fill in blank
hard

Fix the error in the function definition to correctly specify output arguments.

MATLAB
function [1] = addAndSubtract(a, b)
    sum = a + b;
    diff = a - b;
end
Drag options to blanks, or click blank then click option'
A[a, b]
B[sum, diff]
C(a, b)
D(sum, diff)
Attempts:
3 left
💡 Hint
Common Mistakes
Not using square brackets for multiple outputs.
Placing output variables outside brackets or parentheses.
4fill in blank
hard

Fill both blanks to create a function that returns the square and cube of input x.

MATLAB
function [[1], [2]] = powers(x)
    [1] = x^2;
    [2] = x^3;
end
Drag options to blanks, or click blank then click option'
Asquare
Bcube
Cpow2
Dpow3
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names that don't match the assignments inside the function.
Mixing up the order of outputs.
5fill in blank
hard

Fill all three blanks to define a function that returns the sum, product, and difference of two inputs.

MATLAB
function [[1], [2], [3]] = calc(a, b)
    [1] = a + b;
    [2] = a * b;
    [3] = a - b;
end
Drag options to blanks, or click blank then click option'
Asum
Bprod
Cdiff
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic names like result instead of descriptive output names.
Mixing up the order of output variables.