Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

Complex argument

To show how the functions of MATLAB work


for imaginary or complex variables, consider
the following examples,
» x = 1.5 + 0.5 * i
x=
1.5000 + 0.5000 i
» y = sin(x)
y=
1.1248 + 0.0369 i
» z = asin(y)
z=
1.5000 + 0.5000 i

» a = log(x)
a=
0.4581 + 0.3218 i

» b = exp(a)
b=
1.5000 + 0.5000i
Array argument
Most functions in MATLAB can take vectors
and matrices as argument. For example,
» x = [pi/2 , 0 , pi/4 ; pi/6 , -pi/4 , pi/3 ]
x=
1.5708 0 0.7854
0.5236 -0.7854 1.0472
» y = sin(x)
y=
1.0000 0 0.7071
0.5000 -0.7071 0.8660
Developing a program as an m-file
Executing commands from a window is suitable
only if the amount of typing is small, or if we
want to explore ideas interactively.
When commands are more than few lines long,
or the user should write a script m-file, or a
function m-file, because they are saved to disk
and can be corrected as many times as required.
The m-file can include anything the user would
write directly in the command window.
While function m-file corresponds to sub-
program, subroutine, or function in traditional
languages. A useful feature of m-files is that,
an m-file can call other m-files.

The calling m-file is a parent m-file, which


implies that one script may be broken into
one parent m-file and multiple m-files.
How to write a function m-file
Functions in MATLAB, which are saved as
separate m-files, are equivalent, as mentioned
above, to subroutines and functions in other
languages.
Consider the following example:
Calculate the value of the function ,
2
2x  5x  3
y
x 1

corresponding to x = 1.
* We start first by opening a new m-file
* We save this file as , for example, fraction.m
* We then write and save the script
* The script is shown in the figure below
* Notice that the name of the m-file is identical
to the name of the function, which appears
on the right hand side of the equality sign.
* In the m-file the array arithmetic operators
are used, so the argument can be a scalar
as well as a vector or a matrix.

* Once the file fraction.m is saved as an


m-file it can be used from the command
window or in another m-file
In the command window environment we can
run the fraction.m file by typing the following :
»x=1;
» y = fraction(x) ;
»y
y=
5

where y = 5 is the required value of y


corresponding to x = 1 .
We can also run the fraction.m file from
another m-file as follows:
* We start first by opening a new m-file
* We save this file as , say, frac.m
* We then write and save the script required to
run the fraction.m file
To run the fraction.m file we simply press
Debug from the pull-down menu, as shown
above, and then we press Run.
The output is to be found typed in the
command window as shown below,
»
y=
5
Note: A function may return more than one
variable.
Example
We are given the following data:
x = 6, 7, 8, 9, 12 Evaluate the sum and mean
value of the given data.
*A script function m-file may be written
as shown below,
Note:The function m-file above is to be saved
as sumean.m
Another script m-file is written, saved
(file_name.m) , used to enter the required
data and to call the function file sumean.m
(see the figure below)
To run the m-file above we press Debug
And then we press Run
The output data appearing on the command
,window is as shown below

You might also like