Sei sulla pagina 1di 5

LAB # 07

Matrix and Array Operations


2-D and 3-D Plots
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)

If there is no output, either omit it,


function myfunction(x)

or use empty square brackets:


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)

If there are no inputs, you can omit the parentheses.


Tip When you define a function with multiple input or output arguments, list any required arguments
first. This allows you to call your function without specifying optional arguments.

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.

Save the File


Save the file (in this example, fact.m), either in the current folder or in a folder on the MATLAB search
path. MATLAB looks for programs in these specific locations.

Call the Function


From the command line, call the new fact function to calculate 5!, using the same syntax rules that apply
to calling functions installed with MATLAB:
x = 5;

y = 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.

5. Create a surface plot.


6. Now display multiple plots in different sub regions of the same window using the subplot function.
The first two inputs to subplot indicate the number of plots in each row and column. The third input
specifies which plot is active. For example, create four plots in a 2-by-2 grid within a figure window.

Potrebbero piacerti anche