0% found this document useful (0 votes)
13 views5 pages

Unit1-Programming in MATLAB - F3782ec7932e7978f78694 - 250305 - 205714

The document provides an introduction to MATLAB programming, focusing on the use of script files (M-files) and functions. It explains how to create and run script files for executing MATLAB commands, as well as the differences between scripts and functions, including their input and output handling. Additionally, it discusses output commands like 'disp' and 'fprintf' for displaying results in MATLAB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Unit1-Programming in MATLAB - F3782ec7932e7978f78694 - 250305 - 205714

The document provides an introduction to MATLAB programming, focusing on the use of script files (M-files) and functions. It explains how to create and run script files for executing MATLAB commands, as well as the differences between scripts and functions, including their input and output handling. Additionally, it discusses output commands like 'disp' and 'fprintf' for displaying results in MATLAB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to MATLAB Programming

(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

1. To create a file with a list of commands,


2. Save the file, and
3. Run the file.

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.

Example: – Consider the system of linear equations:

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.

(2) Enter the following statements in the script file:

A = [1 2 3; 3 3 4; 2 3 3];

b = [1; 1; 2];

x = A\b

(3) Save the file, for example, example1.m

(4) Run the file, in the command line, by typing, ≫ example1


Introduction to MATLAB Programming
(BEECE1C010)
ECE-Avionics-II semester
Unit 1-Programming in MATLAB

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.

Sol: The roots of the equation are calculated as

𝑏 ± √𝑏 # − 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)

Save the script file as 'quadeqn.m'


Now, define the coefficients a, b, and c, and run the script file

≫ 𝑎 = 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.

Example: Calculate the value of n!, or the factorial of n, where n ∈ N.

In MATLAB,
Introduction to MATLAB Programming
(BEECE1C010)
ECE-Avionics-II semester
Unit 1-Programming in MATLAB
function f = Factorial(n) …….(1)

% Factorial(n) returns the factorial of n. Compute a factorial value. ……..(2)

f = prod(1:n); ……..(3)

end

This is a function of one input argument and one output argument.

For n = 5, the result is:

≫ f = Factorial(5)

f = 120

Description of the steps (1), (2) & (3):

(1) Function definition line: Define the function name and the number and order of input
and output arguments.

2) Hi-line or Help text! A detailed description of the program.

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

¨ Difference between scripts and functions

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

¨ Input and output arguments


The input arguments are listed inside parentheses following the function name. The output
arguments are listed inside the brackets on the left side. They are used to transfer the output
from the function file.

“Function [outputs] = function_name(inputs)”

Examples:

(1) function C=FtoC(F)

- One input argument and one output argument

(2) function area=TrapArea(a,b,h)

- Three inputs and one output

(3) function [h,d]=motion(v,angle)

- Two inputs and two outputs

¨ Input to a script file


When a script file is executed, the variables that are used in the calculations within the file
must have assigned values. The assignment of a value to a variable can be done in three
ways.

1. The variable is defined in the script file.


2. The variable is defined in the command prompt.
3. The variable is entered when the script is executed.

Example: % This script file calculates the average of points scored in three games; first game
%A, second game B and the third game C.

A = input(’Enter the points scored in the first game ’);


B = input(’Enter the points scored in the second game ’);
C = input(’Enter the points scored in the third game ’);
average = (A+B+C)/3

save the file with the name ‘example’ and execute.


Introduction to MATLAB Programming
(BEECE1C010)
ECE-Avionics-II semester
Unit 1-Programming in MATLAB
≫ example
≫ Enter the points scored in the first game
15
≫Enter the points scored in the second game
23
≫ Enter the points scored in the third game
10
Average = 16

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

¨ Difference between “disp” and “fprintf”

Feature disp fprintf

Formatting no formatting, print as it allows precise formatting


(e.g, decimals, integers)
Newline handling automatically adds a does not add a newline unless
newline ‘\n’ is included
Multiple values Cannot combine multiple Can print multiple variables in
values a formatting string
Best for Quick display of Precise formatting and
text/numbers structured output.

You might also like