LAB 7 Matlab
LAB 7 Matlab
Declaring Matrix:
Operations on Matrices:
Example:
Functions:
Write a Function:
Open a file in a text editor. Within the file, declare the function and add program statements:
function f = fact(n)
f = prod(1:n);
Function fact accepts a single input argument n, and returns the factorial of n in output argument f.
The definition statement is the first executable line of any function. Function definitions are not valid at
the command line or within a script. Each function definition includes these elements.
function keyword Use lowercase characters for the keyword.
(required)
Output arguments If your function returns more than one output, enclose the output names in
(optional) square brackets, such as
function [one,two,three] = myfunction(x)
function [] = myfunction(x)
Function name Valid function names follow the same rules as variable names. They must start
(required) with a letter, and can contain letters, digits, or underscores.
Note: To avoid confusion, use the same name for both the file and the
first function within the file. MATLAB® associates your program with
the file name, not the function name.
Input arguments If your function accepts any inputs, enclose their names in parentheses after the
(optional) function name. Separate inputs with commas, such as
function y = myfunction(one,two,three)
The body of a function can include valid MATLAB expressions, control flow statements, comments, blank
lines, and nested functions. Any variables that you create within a function are stored within a
workspace specific to that function, which is separate from the base workspace.
Functions end with either an end statement, the end of the file, or the definition line for another
function, whichever comes first. The end statement is required only when a function in the file contains
a nested function (a function completely contained within its parent).
Program files can contain multiple functions. The first function is the main function, and is the function
that MATLAB associates with the file name. Subsequent functions that are not nested are called local
functions. They are only available to other functions within the same file.
fact(x);
The variables that you pass to the function do not need to have the same names as the arguments in the
function definition line.
Task 1:
Perform sum, product and division by creating a separate function for each operation (Take random
values of variables of your own choice) Task 2:
1. Create “a” matrix that has multiple rows, separate the rows with semicolons.
2. Create “z” matrix is to use a function, such as ones, zeros, or rand.
3. Add 3 in matrix “a”.
4. Take sin(a)
5. Take transpose of “a”
6. Concatenate horizontally arrays to make larger ones.
7. Concatenate vertically using semicolons.
Task 3:
1. Create two-dimensional line plots, the value of the sine function from 0 to 2π, use the plot function
label the axes and add a title.
2. Adding a third input argument to the plot function, you can plot the same variables using a red
dashed line
3. Add plots to an existing figure, use hold on. Until you use hold off or close the window, all plots
appear in the current figure window.
4. Three-dimensional plots typically display a surface defined by a function in two variables, z = f(x,y)
.To evaluate z, first create a set of (x,y) points over the domain of the function using meshgrid.
Task 2:
Task 3