INTRODUCTION TO MATLAB – The full Matlab tutorial can be found here. In this lesson, we will talk about programming in Matlab.
So far, all commands for these lab sessions have been executed in the command window. The problem is that commands entered in the command window cannot be saved and executed repeatedly. So, another way to execute commands repeatedly with MATLAB :
1. Create a file with a list of commands,
2. Save the file and
3. Run the file.
If necessary, you can make corrections or changes to the file commands. The files used for this purpose are called script files or short scripts.
This section covers the following topics:
– M-files Scenarios
– M-File Functions
A script file is an external file that contains a sequence of MATLAB statements. Script files have an .m extension and are often called M-files. M-files can be scripts that simply execute a series of MATLAB statements, or they can be functions that accept arguments and produce one or more outputs.
Here are two simple scenarios.
Let’s consider a system of equations:
Find the solution of x in the system of equations.
– Use the MATLAB editor to modify the : File → New → M-File.
– Write the following instructions in the file :
A = [1 2 3 ; 3 3 4 ; 2 3 3] ;
b = [1 ; 1 ; 2] ;
x = Ab
– Save the file, for example B. example1.m.
– Run the file in the command line by entering the number:
>> Example 1
x = -0.5000
The variables (A, b and x) remain in the workspace after execution. To see their list, type who on the command line.
Pay attention: The MATLAB Editor is both a specialized text editor for creating M-files and a graphical MATLAB debugger. The MATLAB editor has many menus for tasks such as saving, viewing, and debugging. Since it performs some simple checks and also uses color to distinguish between different code elements, this text editor is recommended as the tool of choice for writing and editing M-files.
There is another way to open the editor:
>> change the name of the .m file
to open the file name.
Place the following cosine functions, y1 = 2 cos(x), y2 = cos(x) and y3 = 0.5 ∗ cos(x), in the range 0 ≤ x ≤ 2π. This example was presented in the previous chapter. Here we put the commands in the file.
– Create a file, for example B. example2.m, containing the following commands :
x = 0:ft/100:2*ft ;
y1 = 2*cos(x) ; y2 = cos(x) ;
y3 = 0.5*cos(x) ;
xlabel(0 leq x leq 2pi)
ylabel (cosine functions)
legend (‘2*cos(x)’, ‘cos(x)’, ‘0.5*cos(x)’)
title (A typical example of multiple images)
axis([0 2*pi -3 3]))
– Run the file by typing example2 in the command window.
Side effects of Scenario
All variables created in the script file are added to the workspace. This may have unintended consequences because
– Variables that already exist in the workspace may be overwritten.
– The execution of the script may be affected by state variables in the workspace
Because scripts have unwanted side effects, it is therefore preferable to program complex applications using the M-file function.
Functions of the M-file
As mentioned above, functions are programs (or routines) that accept input arguments and return output arguments. Each M-File function (or function, for short) has its own workspace, which is separate from MATLAB’s basic workspace.
Anatomy of an M-file function
This simple function shows the most important parts of the M-file.
Function f = factorial(n)
% FACTORIAL(N) refers to the faculty of N.
% Calculate the factor value.
f = pitch (1:n) ;
The first line of the M-File function starts with the keyword function. It specifies the name of the function and the order of its arguments. In the case of faculty, there is at most one exit argument and one entry argument. Table 4.1 summarizes the function of the M-file.
As an example, for n = 5, the result,
>> f = factorial(5)
f = 120
Introduction to Matlab – Anatomy of the M-File function
Functions and scripts can have all of these components, except for the function definition string, which is unique to the function.
Also note that the function name must begin with a letter and may contain up to 63 characters. In addition, the name of the text file to be saved will consist of the name of the function with the .m extension. The above example file will therefore be factorial.m.
The following table shows the differences between scripts and functions.
Introduction to Matlab – The difference between scripts and functions
Import and export arguments
As mentioned above, the input arguments are placed in parentheses after the function name. The first arguments are in brackets on the left. They are used to transfer the output of the file to the function. The general form is as follows
function [outputs] = function_name(inputs)
A function file can have no, one or more initial arguments. Table 4.3 shows some possible combinations of input and output arguments.
Introduction to Matlab – Examples of input and output arguments
Entering a script file
When a script file is executed, values must be assigned to the variables used in the file’s calculations. There are three ways to assign a value to a variable.
1. The variable is defined in the script file.
2. The variable is defined on the command line.
3. The variable is entered when the script is executed.
We’ve already seen the first two cases. We’re interested in the third. In this case, the variable is defined in the script file. When the file is run, the user is prompted to assign a value to a variable on the command line. This is done with the input command. Here is an example
% This script file calculates the average value of the points
% of points scored in three games.
A point in each set is assigned to a variable.
% with the input command.
set1 = enter (enter the points scored in the first set) ;
set2 = enter (enter the points scored in the second set) ;
game3 = enter (Enter the points scored in the third game) ;
Average = (Set1+Set2+Set3)/3
The command line when running this script file (included as example3) is shown below.
>> Record points scored in the first set 15
>> Record points scored in the second set 23
>> Record points scored in the third set 10
Average = 16
The input command can also be used to assign a string to a variable. See the MATLAB documentation for more information.
A typical example of functional programming with M-Files can be found in recent work on the solution of the ordinary differential equation (ODE).
As mentioned earlier, MATLAB automatically generates a display when commands are executed. In addition to this automatic display, MATLAB has several commands that can be used to generate displays or outputs.
Two commands are commonly used to generate outputs: disp and fprintf. The main differences between the two teams can be summarized as follows.
Introduction to Matlab – disp and fprintf commands
Post Views : 2
introduction to matlab slideshare, introduction to matlab pdf, matlab course ware, introduction to simulink, matlab advantages, introduction to matlab coursera, matlab tutorial for image processing, desktop of matlab, how many windows are there in matlab, matlab array i, matlab desktop tools, matlab basic commands, matlab online courses nptel, matlab training fees, learn matlab step by step, matlab programming pdf, matlab coursera, matlab online course for mechanical engineers, matlab tutorial youtube, matlab programming book, matlab program example, simple matlab program to add two numbers, matlab tutorial pdf, mit ocw fractal, matlab curriculum, matlab course topics, matlab programming video lecture, download matlab course, how to teach matlab, learn matlab step-by step, best matlab tutorial book, best matlab tutorial youtube, how to learn matlab? – quora, matlab onramp course quora, matlab programming tutorial, matlab programming language, introduction to matlab programming, matlab exercises and solutions pdf, introduction to matlab – ppt, matlab documentation