DownloadClassSessionFile - 2024-02-26T205009.417

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

University of Salahaddin-Hawler

College of Engineering
Software and Informatics Engineering Department
Second Year Class

Numerical Analysis
and
Probability

Lecturer
Kanar Shukr Muhamad

2023-2024
Making variables
 Writing who in command window list all the used

variables:
 >> who

 Your variables are:

a ans c

 Writing whos in command window list all the used

variables, with its size and type:


2
Making variables

 date: displays current system date

 pwd: displays current path

3
Lab Activities
 Ac1/ Put ; at the end of the line the command to realize that it

does not appear at the command window.

 Ac2/ Write some comments. What did you realize!!

 Ac3/ Write clear command. What did you realize!!

 Ac4/ Write a MATLAB command(s) to take two numbers and

output average and reminder of them on the screen.

 Ac5/ Write a MATLAB command to find sqrt of a given value.

4
Lab Activities
 Ac6/ Write a MATLAB command(s) to get two double

numbers and output division of them.

 Ac7/ Use MATLAB lines code to find solutions of

quadratic equations.

 Ac8/ clear the first variable.

 Ac9/ clear the variables

 Ac10/ close MATLAB program

5
Format Decimal Points in MATLAB
 By default, MATLAB uses a 4-digit short

format to display numbers. For example

 You can change the display in the Command

Window or Editor using the format function.


6
Format Decimal Points in MATLAB
>>format long
>>x
Ans/ x = 1.333333333333333 %15 digit
Using the format function only sets the format for the
current MATLAB session. To set the format for
subsequent sessions, click Preferences on the Home tab
in the Environment section. Select MATLAB > Command
Window, and then choose a Numeric format option.
7
Format Decimal Points in MATLAB
short Short, fixed-decimal format with 4 3.1416
(default) digits after the decimal point.
long Long, fixed-decimal format with 3.14159265
15 digits after the decimal point 3589793
for double values.
rat Ratio of small integers.

8
MATLAB Files
 MATLAB allows you to write a series of commands into a

file and execute the file as a complete unit, like writing a


function and calling it. It is done using M-file.

 M-File also called as the script file is a series of MATLAB

commands that will get executed sequentially when the


file is executed.

 The m-file is saved with .m extension.

9
MATLAB Files
 MATLAB allows writing two kinds of program files,

which are:

 Scripts

 Script files are program files with .m extension. In these

files, you write a series of commands, which you want to


execute together. Scripts do not accept inputs and do not
return any outputs. They operate on data in the workspace.
10
MATLAB Files
 Functions

 Function files are also program files with .m

extension. Functions can accept inputs and return


outputs. Internal variables are local to the function.

 M. files can be written either in MATLAB editor

or any other editor

11
MATLAB Files
 To create m-file, we will make use of MATLAB IDE as
shown below.

12
MATLAB Files
Click on New Script to open a new script file.

13
MATLAB Files
 Saving m-file
 You will get an untitled file. Let us save the file
as firstmfile.m.
 Click on the save button and it will open a popup,
where you can enter the name of the file.

14
MATLAB Files
 Click on OK to save the file.

 Now, you are free to write your commands in

the file below.

 Click on the Run button to see the result

inside the command window as shown below:

15
MATLAB Files

16
Matlab M-Files - Functions
 A function is a group of statements that together perform

a task.

 In MATLAB, functions are defined in separate files.

 The name of the file and name of the function should be

the same.

17
Matlab M-Files - Functions
 Functions operate on variables within their own
workspace, which is also called the local workspace.
These functions separate the variables from the
workspace which you access at the MATLAB command
prompt. This is called the base workspace.

 Functions can accept more than one input arguments and

may return more than one output arguments.


18
Function
 Functions let you do a specific task. User defined functions

are the functions created by the users according to their


needs.

 MATLAB permits creating own functions

 These are scripts that take in certain inputs and return a

value or set of values

19
Function
 Declaration function

 function output(return value)=function_Name(input(s))

 Functions’ body …..

 End

 For saving a user defined function, go to Home -> New

Scripts, write the function definition and save the script as


the function name.

20
Function
After writing the function and saving the file as
the function name, write the function name and
gives value(s) to it’s parameter call and execute
function in the command window.

21
Function
Ac11: Write a function that takes two parameters
(x and y) and return division x by y.

22

You might also like