A MATLAB function is a .m file that stores a sequence of MATLAB commands. Like a script, MATLAB executes those commands from top to bottom. The key difference is that a function is designed to accept inputs (known information) and return outputs (desired information).
You have already used many built-in MATLAB functions (such as sqrt, abs, and floor). The functions you write in this course work the same way: MATLAB only cares about the inputs you provide and the outputs the function returns.
Functions help you package a useful calculation into a reusable tool. Instead of rewriting the same computations in many scripts, you write them once as a function and then call that function whenever you need it.
function perimeter = square_perimeter(side)
% Use the input (side) to compute the output (perimeter)
perimeter = 4 * side;
end
To use this function, save it in a file named square_perimeter.m. Then, in the Command Window (or in another script), call the function by providing an input value inside parentheses. For example, to compute the perimeter of a square with side length 5, type:
p = square_perimeter(5) % returns 20 and stores it in variable p
MATLAB will run the commands inside the function, using the input value you provided (5) to compute the output value (20). The result is then returned to the Command Window and stored in the variable p.
To create a function in MATLAB, you start by opening a new function file. Here is an example of a function that computes the perimeter of a rectangle given its two sides.
function perimeter = rectangle_perimeter(side1, side2)
% Use the inputs (side1 and side2)
% to compute the output (perimeter)
perimeter = 2 * side1 + 2 * side2;
end
The first line of the function file is the function header, which defines the functionβs name, inputs, and outputs. For example, in the function above, the line
When you save the function, the function name should match the file name (without the .m extension). In this example, the function is named rectangle_perimeter, so the file should be saved as rectangle_perimeter.m.
Inside the function, you can use the input variables to perform calculations and assign values to the output variable. When the function is called, MATLAB executes the commands in the function file and returns the output value. Unlike scripts, intermediate variables stay inside the function unless you return them.
To call this function, provide two input values inside parentheses. For example, to compute the perimeter of a rectangle with side lengths 4 and 7, type:
p = rectangle_perimeter(4, 7) % returns 22 & stores it in p
MATLAB will run the commands inside the function, using the input values you provided (4 and 7) to compute the output value (22). The result is then returned to the Command Window and stored in the variable p.
Functions can return multiple outputs. To specify multiple outputs, list them in square brackets in the function header. Here is an example of a function that computes both the perimeter and area of a rectangle given the lengths of its two sides.
function [perimeter, area] = rectangle_properties(side1, side2)
% Use the inputs (side1 and side2)
% to compute the outputs (perimeter and area)
perimeter = 2 * side1 + 2 * side2;
area = side1 * side2;
end
To call this function and capture both outputs, use square brackets on the left side of the assignment. For example, to compute the perimeter and area of a rectangle with side lengths 4 and 7, type:
[p, a] = rectangle_perimeter(4, 7) % returns p = 22 and a = 28
MATLAB will run the commands inside the function, using the input values you provided (4 and 7) to compute the output values (22 and 28). The results are then returned to the Command Window and stored in the variables p and a.
π§π»βπ» Class Activitiesπ§π»βπ» Class Activities
1.Hello Function.
(a)
Create a function named hello with no inputs and outputs that displays 'Hello World' using the fprintf command. Test your function by typing hello in the command window.
In the piggy bank script, you computed a total value from the number of pennies, nickels, dimes, and quarters. Now, package that computation into a function so you can reuse it with different coin counts.
Write a function that computes the two solutions of \(a x^2 + b x + c = 0\) using the quadratic formula. This function may return complex solutions when the discriminant is negative.
Test your function on each equation below, and print the results. Use the real-and-imaginary printing approach so your output also works for complex solutions.
The function fails when a = 0, because the quadratic formula divides by \(2a\text{.}\) Handling a = 0 properly requires making a decision inside the function, which needs flow control.
A script can call a function that you wrote (for example, coin_total), as long as the function file is available in the Current Folder (or on the path).