1 / 12

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

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.

iolana
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Al Imam Mohammad Bin Saud Islamic University College of SciencesDepartment of Mathematics MATLAB 251 : MATH SOFTWARE Introduction to MATLAB mrs.Asra Sultana

  2. Introduction to MATLAB 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 file P = [1 3 2] roots(P) %Function file Function[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 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

  3. Introduction to Matlab Open an existing files OPEN NEW M – files Create a New file

  4. Introduction to Matlab Once you have finished to type your commands you have to save it by giving it a name under extension .m Matlab 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 editor x=[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 the name of the file without its extension (Matlab recognize it) Type in the Matlab window prompt >> trial1

  5. Introduction to Matlab A sequence of instructions in a file is called a SCRIPT (For the commentaries use % character) If you want to see the command during the script carrying out you have to use the command >> echo on/off Exercise: Type “echo on” on the MATLAB prompt and run the filetrial1.m A practical example : Open a new file and call it Peaks1.m Enter 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,’.’)

  6. Introduction to Matlab Reopen the file Peaks1.m. Go to the end of the file and enter the following command subplot(2,1,2) mesh(x,y,peaks(50)+10*x) Carry out the file Another example: Create the file task1.m that contains the following script and execute it echo on % Turn on 15 digit display format long x = [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 >> task1 If you use echo on in a script M-file, then MATLAB will also echo the comments, so they will appear in the Command Window

  7. Introduction to Matlab 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

  8. Introduction to Matlab input output 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 2011 delta = 4*a*c; denom = 2*a; rootdisc = sqrt(b.^2 - delta); % Root of the discriminant x1 = (-b + rootdisc)./denom; x2 = (-b - rootdisc)./denom; x = [x1 x2]; x Commentaries Instructions

  9. 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 fancyplot 3. function a = lists(x,y,z,t)

  10. Introduction to Matlab • 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 avoid • confusion. • 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: >> help quadratic 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]

  11. 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.

  12. 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 long • b = 1:c; • x = 10.ˆ(-b); • y = (sin(x)./x)’;

More Related