Al Imam Mohammad Bin Saud Islamic University College of Sciences Department of Mathematics

12
AL IMAM MOHAMMAD BIN SAUD ISLAMIC UNIVERSITY COLLEGE OF SCIENCES DEPARTMENT OF MATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB

description

Al Imam Mohammad Bin Saud Islamic University College of Sciences Department of Mathematics. MATLAB 251 : MATH SOFTWARE Introduction to MATLAB. mrs. Asra Sultana. Introduction to MATLAB. 7. Script Files. - PowerPoint PPT Presentation

Transcript of Al Imam Mohammad Bin Saud Islamic University College of Sciences Department of Mathematics

Page 1: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

AL IMAM MOHAMMAD BIN SAUD ISLAMIC UNIVERSITY

COLLEGE OF SCIENCESDEPARTMENT OF MATHEMATICS

MATLAB 251 : MATH SOFTWARE

Introduction to MATLAB

Page 2: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

7 .Script Files 7 .Script Files

M – Files : It is used foe solving complicated problems. There are two different kinds of M – Files:• Script M – Files•Function M – Files

%Script fileP = [1 3 2]roots(P)

%Function fileFunction[y] = fun(x) y= x^2+3*x^2+2

M-files are ordinary text files containing MATLAB commands. You can create and modify them using any text editor or word processor that is capable of saving files as plain ASCII text. (Such text editors include notepad in Windows or emacs, winedit, and vi in UNIX.) More

conveniently, you can use the built-in Editor/Debugger, which you can start by typing edit, either by itself (to edit a new file) or followed by the

name of an existing M-file in the current working directory. You can also use the File menu or the two leftmost buttons on the tool bar to start

the Editor/Debugger, either to create a new file or to open an existing file . Double-clicking on an M-file in the Current Directory browser will also open

it in the Editor/Debugger

M-files are ordinary text files containing MATLAB commands. You can create and modify them using any text editor or word processor that is capable of saving files as plain ASCII text. (Such text editors include notepad in Windows or emacs, winedit, and vi in UNIX.) More

conveniently, you can use the built-in Editor/Debugger, which you can start by typing edit, either by itself (to edit a new file) or followed by the

name of an existing M-file in the current working directory. You can also use the File menu or the two leftmost buttons on the tool bar to start

the Editor/Debugger, either to create a new file or to open an existing file . Double-clicking on an M-file in the Current Directory browser will also open

it in the Editor/Debugger

All Matlab commands, functions and operations can be done in an editor text file. To open an editor text file you click on the menu FILE/New or use that matlab command

>>edit

Page 3: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

Open an existing files

Create a New

file

OPEN NEW M – files

Page 4: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

Once you have finished to type your commands you have to save it by giving it a name under extension .mMatlab save all files in the default repertory C:\MATLAB7\work

>> Edit trial1.m % edit a Matlab file named trial1

Then type the following commands in this text editorx=[1:0.1:10];y=sin(x);plot(x,y,'r-.')and save the file trial1.m

To run a file in Matlab command window prompt we have to type thename of the file without its extension (Matlab recognize it)

Type in the Matlab window prompt>> trial1

Page 5: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

If you want to see the command during the script carrying outyou have to use the command>> echo on/off

A sequence of instructions in a file is called a SCRIPT(For the commentaries use % character)

A sequence of instructions in a file is called a SCRIPT(For the commentaries use % character)

Exercise:Type “echo on” on the MATLAB prompt and run the filetrial1.m

A practical example :Open a new file and call it Peaks1.mEnter the following commands and run the file.subplot(2,1,1)rv=linspace(.3,1,50);thv=linspace(pi/4,5*pi/4,50);[r,th]=meshgrid(rv,thv);x=r.*cos(th); y=r.*sin(th);plot(x,y,’.’)

Page 6: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

Reopen the file Peaks1.m. Go to the end of the file and enter the following commandsubplot(2,1,2)mesh(x,y,peaks(50)+10*x)

Carry out the fileAnother example:Create the file task1.m that contains the following script and execute itecho on% Turn on 15 digit displayformat longx = [0.1, 0.01, 0.001];y = sin(x)./x% These values illustrate the fact that the limit of% sin(x)/x as x approaches 0 is 1.echo off

>> task1If you use echo on in a script M-file, then MATLAB will also echo thecomments, so they will appear in the Command Window

Page 7: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

8 .Functions M - files 8 .Functions M - files•Functions are m-files that can be used to extend the MATLAB language.

•Functions can accept input arguments and produce output arguments.

•Many of MATLAB’s own commands are implemented as m-files;Try typing >> type mean to see how MATLAB calculates the mean.

•Functions use variables that are local to themselves and do not appear in the main workspace.

•Always the m-file has the name of the function

Syntax: function [u,v,…]=FunctionName(x,y,…)

output input

Page 8: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

function = quadratic(a,b,c)% QUADRATIC Find roots of a quadratic equation.%% X = QUADRATIC(A,B,C) returns the two roots of the% quadratic equation y = A*x^2 + B*x + C.%% The roots are contained in X = [X1 X2].% M251, April 2011delta = 4*a*c;denom = 2*a;rootdisc = sqrt(b.^2 - delta); % Root of the discriminantx1 = (-b + rootdisc)./denom;x2 = (-b - rootdisc)./denom;x = [x1 x2];

x

output input

Com

menta

ries

Inst

ruct

ions

Page 9: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

Function m-files must start with the word function, followed by the output variable(s), an equals sign, the name of the function, and the input variable(s).

Functions do not have to have input or output arguments. If there is more than one input or output argument, they must be separated by commas. If there are one or more input arguments, they must be enclosed in round brackets, and if there are two or more output arguments, they must be enclosed in square brackets. The following illustrate these points (they are all valid function definition lines):

1. function [xx,yy,zz] = sphere(n)2. function fancyplot3. function a = lists(x,y,z,t)

Page 10: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

>> help quadratic

QUADRATIC Find roots of a quadratic equation.X = QUADRATIC(A,B,C) returns the two roots of the quadratic equationy = A*x^2 + B*x + C.The roots are contained in X = [X1 X2]

Function names must follow the same rules as variable names.The file name is the function name with “.m” appended.

If the file name and the function name are different, MATLAB uses the file name and ignores the function name.

You should use the same name for both the function and the file to avoidconfusion.

Following the function definition line you should put comment lines that explain how to use the function. These comment lines are printed in the command window when you type help followed by the m-file name at the prompt:

Page 11: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

MATLAB only echoes the comment lines that are contiguous; the first no comment line, in this case the blank line before the signature, tells MATLAB that the help comments have ended. The first line of the help comments is searched and, if successful, displayed when you type a lookfor command.

Comment lines can appear anywhere in the body of an m-file. Comments can be put at the end of a line of code: For instance rootdisc = sqrt(b.^2 - delta); % Root of the discriminant

Blank lines can appear anywhere in the body of an m-file. Apart from ending the help comment lines in a function, blank lines are ignored.

Page 12: Al  Imam  Mohammad Bin Saud Islamic University  College of  Sciences Department of  Mathematics

INTRODUCTION TO MATLAB

we computed some values of sin(x)/x with x = 10−b for several values of b. Suppose, in addition, that you want to find the smallest value of b for which sin(10−b)/(10−b) and 1 agree to 15 digits. Here is a function M-file called sinelimit.m designed to solve that problem:

function y = sinelimit(c)% SINELIMIT computes sin(x)/x for x = 10ˆ(-b),% where b = 1, ..., c.format longb = 1:c;x = 10.ˆ(-b);y = (sin(x)./x)’;