Unit1-Programming in MATLAB - F3782ec7932e7978f78694 - 250305 - 205714
Unit1-Programming in MATLAB - F3782ec7932e7978f78694 - 250305 - 205714
(BEECE1C010)
ECE-Avionics-II semester
Unit 1-Programming in MATLAB
¨ Programming in MATLAB
The problem is that the commands entered in the command window cannot be saved and
executed again for several times. Therefore, a different way of executing repeatedly commands
with MATLAB is
The files that are used for this purpose are called script files or scripts for short.
• M-file scripts
• M-file functions
¨ M-file scripts
A script file is an external file that contains a sequence of MATLAB statements. Script files
have a filename extension .m 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 can accept arguments
and produce one or more outputs.
x + 2y + 3z = 1
3x + 3y + 4z = 1
2x + 3y + 3z = 2
Find the solution x to the system of equations.
Sol. (1) Use the MATLAB editor to create a file: File → New → M-file.
A = [1 2 3; 3 3 4; 2 3 3];
b = [1; 1; 2];
x = A\b
When execution completes, the variables (A, b, and x) remain in the workspace.
Example 2. Solve the quadratic equation ax² + bx + c = 0 by using the script file.
𝑏 ± √𝑏 # − 4𝑎𝑐
𝑥!,# = −
2𝑎
In MATLAB,
% Solve quadratic equation ax² + bx + c = 0
x1 = (-b - sqrt(b^2 – 4*a*c))/(2*a)
x2 = (-b + sqrt(b^2 – 4*a*c))/(2*a)
≫ 𝑎 = 3; 𝑏 = 5; 𝑐 = 2;
≫ quadegn
𝑥! = −1
2
𝑥# = −
3
Remark: Variables created in script files are added to the workspace, which may cause
issues. Existing variables can be overwritten, and script execution may be affected by
workspace variables. Due to these side effects, scripts can be unreliable for complex
applications. It is preferable to use function m-files instead.
¨ M-file functions
Functions are programs (or routines) that accept “input” arguments and return “output”
arguments. Each m-file function (or function m-file) has its own area of workspace, separated
from the MATLAB base workspace. The first line of a function m-file starts with the
keyword function. It gives the function name and order of arguments.
In MATLAB,
Introduction to MATLAB Programming
(BEECE1C010)
ECE-Avionics-II semester
Unit 1-Programming in MATLAB
function f = Factorial(n) …….(1)
f = prod(1:n); ……..(3)
end
≫ f = Factorial(5)
f = 120
(1) Function definition line: Define the function name and the number and order of input
and output arguments.
(3) Function body! Program code that performs the actual computations.
Note: (1) Both functions and scripts can have all of these parts, except for the function
definition line, which applies to functions only.
(2) The function name must begin with a letter and must be no longer than the maximum of
63 characters.
(3) The name of the text file that you save will consist of the function name with the
extension .m.
Scripts Functions
Do not accept input arguments or return Can accept input arguments and return
output arguments. output arguments.
Store variables in a workspace that is Store variables in a workspace internal to
shared with other scripts the function
They are useful for automating a series of They are useful for extending the
commands. MATLAB language for your application.
Introduction to MATLAB Programming
(BEECE1C010)
ECE-Avionics-II semester
Unit 1-Programming in MATLAB
Examples:
Example: % This script file calculates the average of points scored in three games; first game
%A, second game B and the third game C.
¨ Output commands
MATLAB automatically generates a “disply” when commands are execute. Further, MATLAB
has several commands that can be used to generate displays or outputs There are two commands
that are frequently used to generate output as: “disp” and “fprintf”.
disp – simple to use and provide limited control over the appearance of output.
fprintf- slightly more complicated than “disp” and provide total control over appearance of
output. Also, it allows formatted output.