Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Computer Programming and Applications MATLAB-beginner

Uploaded by

hasonalkadummy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Computer Programming and Applications MATLAB-beginner

Uploaded by

hasonalkadummy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

Computer programming and

applications MATLAB-beginner
Introduction

Assist.Lec Aya Abdel Hussein


MATLAB
 MATLAB is a powerful computing system for handling the calculations
involved in scientific and engineering problems. The name MATLAB
stands for MATrix LABoratory, because the system was designed to make
matrix computations particularly easy.
 One of the many things you will like about MATLAB (and which
distinguishes it from many other computer programming systems, such as
C++ and Java) is that you can use it interactively. This means you type
some commands at the special MATLAB prompt, and get the answers
immediately. The problems solved in this way can be very simple, like
finding a square root, or they can be much more complicated, like finding
the solution to a system of differential equations.
MATLAB Environments
 The MATLAB Desktop is what results when you invoke MATLAB on your computer.
Matlab windows
 1. Command Window: This window serves as the primary interface for
communicating directly with MATLAB. Commands and expressions are entered
here, and MATLAB evaluates them in real-time, displaying the results of
computations. It's essentially the space where MATLAB and users interact.
Matlab windows
 2. Workspace Window: In this window, MATLAB keeps track of all variables and
data used during a session. It displays various data types, with matrices being
the most common. It's like a virtual table where MATLAB organizes and
presents the information you're working with.
Matlab windows
 3. Help Window: The Help Window provides extensive information about
MATLAB's language and environment, along with examples and tutorials. Users
can access explanations of commands or functions, making it akin to having a
comprehensive guide or tutor within MATLAB itself.
Matlab windows
 4. Command History Window: This window records all commands previously
entered into MATLAB. It functions as a historical record, allowing users to
revisit and potentially reuse commands without retyping them. Users can re-
execute commands from this window, facilitating efficiency in workflow.
Matlab windows
 5. Editor Window: The Editor Window offers a platform for writing, editing,
and saving complete MATLAB programs. It functions as a text editor tailored
for MATLAB code, providing tools for organizing and managing scripts.
Additionally, users can run programs directly from this window, streamlining
the coding process.
Questions
 1- Define MATLAB.
 2- What is the Command Window in MATLAB?
 3- What is the Workspace Window in MATLAB?
 4- What is the Help Window in MATLAB?
 5- What is the Command History Window in MATLAB?
 6- What is the Editor Window in MATLAB?
Solutions
Computer programming and
applications MATLAB-beginner
The first step in Matlab

Assist.Lec Aya Abdel Hussein


Constants
 Constants are fixed values that remain unchanged during the execution
of a program.
 They are used to represent data that does not change, such as
mathematical constants, special values, or fixed parameters.
 Constants are typically directly used in expressions or assigned to
variables.
Variables
 Variables are symbols that represent values or data.
 They can vary; their values can change during the execution of a
program.
 Variables are typically assigned values using the assignment operator '='.
 They can be numeric values, strings, arrays or other data types.
Expressions
 In MATLAB, expressions are combinations of variables, constants,
operators, and functions that represent mathematical computations.
These expressions are evaluated to produce a result. Here are some
examples of expressions in MATLAB:
1-Arithmetic Expressions:
Expressions
write a program in MATLAB that allows the user to input four numbers.
The program should then calculate the average of these numbers and
display the result.
Expressions
Write a program in Matlab to enter two numbers from the user to perform
mathematical operations such as addition, multiplication and subtraction
on them.
Expressions
2-Relational Expressions:
Expressions
2-Relational Expressions:

Write a program in MATLAB that prompts the user to


enter two numbers and then performs four relational
operations on them.
Expressions
3-Logical Expressions:
Expressions
3-Logical Expressions:
Write a program in MATLAB that examines two logical values, both set to true, and
then performs the logical 'AND' and 'OR' operations on them.
Questions:
Solutions
Computer programming and
applications MATLAB-beginner
Arrays

Assist.Lec Aya Abdel Hussein


matrix
 A matrix is a mathematical concept that represents a rectangular array of
numbers, symbols, or expressions arranged in rows and columns. Each
element in a matrix is identified by its row and column indices. Matrices
are widely used in linear algebra and various fields such as physics,
engineering, computer graphics, and economics.
Properties of Matrices:
 1. Size and Dimensions: A matrix has a defined number of rows and
columns, which determine its dimensions or size.
 2. Elements: The individual entries in a matrix are called elements. Each
element is identified by its row and column indices.
 3. Types of Matrices: Matrices can be square (same number of rows and
columns) or rectangular. They can also be classified based on other
properties like symmetry, diagonal dominance, etc.
 4. Operations:Matrices support operations such as addition, subtraction,
scalar multiplication, matrix multiplication,and transpose.
Entering Matrices
Manual Entry
Entering Matrices
Manual Entry

Write MATLAB code to manually enter a matrix consisting of four rows and three
columns.
Entering Matrices
Using the zeros, ones, or rand Functions:
Entering Matrices
Using the zeros, ones, or rand Functions:

Write a program in MATLAB that generates a 4x4 matrix in three ways: the first
matrix should be composed of zeros, the second of ones, and the third of random
decimal values.
Deleting Rows or Columns.
Transpose
Change the value of an element in an array
Questions
 1- Define the matrix and explain its most important properties.
 2- Write MATLAB code to manually enter a matrix consisting of four rows and
three columns. Then perform a transpose between the rows and columns,
delete the second row, and replace the first element in the array with the
number 100.
Solutions
Computer programming and
applications MATLAB-beginner
Built in functions

Assist.Lec Aya Abdel Hussein


Built-in functions
 Built-in functions: are predefined functions that come with the MATLAB
environment, designed to perform a variety of specific tasks in areas
including mathematics, statistics, engineering, and graphics. These functions
are ready to use and help to efficiently perform complex operations without
the need for writing extensive code from scratch.
Built-in functions
Built in functions Always shorten and simplify programming processes
Basic Matrix Functions
 Basic matrix functions in MATLAB are fundamental operations used for
manipulating matrices and performing common tasks.
 1- sum: Computes the sum of all elements in a matrix.
Basic Matrix Functions
 2-max: Finds the maximum value in a matrix.
 3-min: Finds the minimum value in a matrix.
Basic Matrix Functions
 Write a program in MATLAB that finds the largest and smallest elements of a
3x3 matrix and multiplies them together.
Basic Matrix Functions
 4- mean: Computes the mean of all elements in a matrix.
Basic Matrix Functions
 5- magic: Generates a magic square matrix of a given size.
 A magic square is a square matrix in which the sums of the elements in each
row, each column, and both main diagonals are the same.
Basic Matrix Functions
 6- diag: Extracts the diagonal elements of a matrix or constructs a diagonal
matrix from a vector.
Basic Matrix Functions
Create a magic matrix in MATLAB, then find its average and major diagonal.
Basic Matrix Functions
 7- length: Returns the length of the largest array dimension.
 8- size: Returns the size of a matrix.
Basic Matrix Functions
 9- median: Computes the median value of all elements in a matrix.
 10- prod: Computes the product of all elements in a matrix.
Basic Matrix Functions
 11- sort: Sorts the elements of a matrix in ascending order.
Questions
 Define built-in functions. Then, in MATLAB, create a 4x3 matrix and perform the
following operations:

 1. Find the sum of all matrix values.


 2. Determine the largest and smallest element in the matrix.
 3. Calculate the average of all array elements.
 4. Compute the main diagonal of the matrix.
 5. Find the length of the array (total number of elements).
 6. Determine the size of the matrix (rows x columns).
 7. Find the median of all matrix elements.
 8. Calculate the product of all matrix elements.
Solutions
Computer programming and
applications MATLAB-beginner
Control Statements

Assist.Lec Aya Abdel Hussein


Conditional statements
Conditional statements, also known as control statements, are programming
constructs that allow you to execute different blocks of code based on certain
conditions or criteria. They are fundamental to controlling the flow of execution
in a program.
Conditional statements: If
An if-end statement is the simplest decision-making statement. It decides
whether a particular block of code has to be executed or not, based on the
given boolean condition. Only when the given condition is true, it executes the
statements inside the block otherwise not.
Conditional statements: If
Write a MATLAB program that classifies a number as positive, negative, or zero
using only if statements.
Conditional statements: If Else
In conditional if Statement the additional block of code is merged as else statement
which is performed when if the condition is false else condition won’t be executed
when if the condition is True.
Conditional statements: If Else
Write a program in Matlab that determines whether the entered character is the letter
A or not
Conditional statements: If-elseif-else
 An if statement can be followed by one (or more) optional elseif and an else
statement, which is very useful to test various conditions.
Conditional statements: If-elseif-else
Write a MATLAB program that classifies a number as positive, negative, or zero
using only if statements.
Conditional statements:

Write a MATLAB program that checks if a given number is odd, even, or zero.
Conditional statements: Switch-case
 A switch block is familiar to if-elif-else-end statements. It works as same as in
other languages except for syntax. But there are few key differences between
them. A switch block conditionally executes one set of statements from several
choices. Each choice is covered by a case statement.
Questions
 1-Write a MATLAB program that classifies a student's grade based on their
score.
 2-Write a MATLAB program that checks if a given number is positive.
 3-Write a MATLAB program that determines the day of the week based on a
user-inputted number (1 for Monday, 2 for Tuesday, etc.).
 4-Give me a programming example for each:
IF
If_Else
If_elseIf_Else
Switch_case
Solutions
Computer programming and
applications MATLAB-beginner
Repetition statementsents

Assist.Lec Aya Abdel Hussein


Repetition statementsents
 Repetition statements, also known as loops, are programming constructs
that allow you to repeat a block of code multiple times. They are used
when you need to perform a task iteratively, such as processing elements
in an array, iterating over a sequence of numbers, or executing a block of
code until a certain condition is met.
 In MATLAB, there are mainly two types of repetition statements:

 1- for loop: The for loop iterates over a sequence of values and executes a
block of code for each value in the sequence.
 2- while loop: The while loop repeats a block of code as long as a specified
condition is true.
for Statement: Example of printing numbers
 The following example is to print numbers from 1 to 5 using for statement
 using a for statement in MATLAB does shorten and simplify the process of
writing repetitive print instructions or any repetitive task.
for Statement: Sum of Numbers Example
 Explanation:
 total is initialized to zero.
 The for loop iterates over integers from 1 to 10.
 In each iteration, the current value of i is added to total.
 After the loop, the sum of the numbers from 1 to 10 is displayed.
for Statement:
 Write a MATLAB program to separate elements in the given array into two
arrays - one containing elements larger than ten and the other containing
elements smaller than ten.
for Statement:
 Write a MATLAB program that prompts the user to enter a number and then
prints the number repeatedly, with the number of repetitions equal to the
value of the entered number.
while Statement:
 while loop: The while loop repeats a block of code as long as a specified
condition is true.
 This loop will print the numbers 1 to 5.
while Statement:
 Write a program that prints numbers from 1 to 10 using a while loop.

Write a program that counts down from 10 to 1 using a while loop.


Questions
 1- Write a program in Matlab that reads the student's name and grades for
seven subjects and then prints the student's name and grade point average
Note: Use for
 2- Write a program in Matlab to print numbers in descending order from 10 to
1, once using for and once using while.
 3- what is Repetition statements
 4- define for loop
 5- define while loop
Solutions
Computer programming and
applications MATLAB-beginner
Combination of conditional and
repetition statements

Assist.Lec Aya Abdel Hussein


Combination of conditional and repetition
statements
 Combining conditional and repetition
statements in MATLAB can be very powerful
for implementing complex algorithms and
solving various computational problems
efficiently.
 Given a loop in MATLAB that iterates from 1
to 10, write the code snippet that prints a
message based on the value of the loop index
i:
If i is less than 5, the message "Less than 5" is
displayed.
If i is equal to 5, the message "Equal to 5" is
displayed.
If i is greater than 5, the message "Greater than
5" is displayed.
The code should execute the loop and print the
appropriate message for each value of i.
Combination of conditional and repetition
statements
 Given a loop in MATLAB that iterates
from 1 to 10, write the code snippet
that prints a message based on the
value of the loop index i:
 If i is less than 5, the message "Less
than 5" is displayed.
 If i is equal to 5, the message "Equal
to 5" is displayed.
 If i is greater than 5, the message
"Greater than 5" is displayed.
 The code should execute the loop
and print the appropriate message
for each value of i.
Combination of conditional and repetition
statements
 Consider an array of numbers
numbers containing the
following values: [1, 2, 3, 4,
5, 6, 7, 8, 9, 10]. Write a
MATLAB code snippet that
iterates through each number
in the array, checks if the
number is even, and if so,
adds it to a new array called
evens. After iterating through
all numbers, the code should
display the array evens,
containing all the even
numbers from the original
array.
Combination of conditional and repetition
statements
Given an array of student scores student_scores with the following values: [75, 85,
90, 60, 40, 55, 95, 80, 70, 65], write a MATLAB code snippet that iterates through
each score, determines the corresponding grade based on the score using the
following criteria:
A: score >= 90, B: score >= 80, C: score >= 70, D: score >= 60, F: score < 60
The code should output each score along with its corresponding grade
Combination of conditional and repetition
statements
 Write a program that examines an array consisting of ten numbers and determines
the odd and even numbers for each number
Computer programming and
applications MATLAB-beginner
Basic Plotting

Assist.Lec Aya Abdel Hussein


Basic Plotting
 Basic Plotting: MATLAB has extensive facilities for displaying vectors
and matrices as graphs, as well as annotating and printing these
graphs.
 In MATLAB, the plot function is used to create 2D plots. It's a
fundamental tool for visualizing data and mathematical functions.
Here's a breakdown of its importance and usefulness with a simple
example:

 1-Data Visualization
 2-Communicating Results
 3-Exploratory Data Analysis
 4-Debugging and Testing
Basic Plotting
Basic Plotting
Basic Plotting
Multiple Data Sets in One Graph
Specifying Line Styles and Colors
Multiple Plots in One Figure
Questions
 1- Define basic plotting in MATLAB and explain its benefits.
 2- Using basic plotting in MATLAB, create a plot of the sine function.
 3- Using basic plotting in MATLAB, create a plot of the cos function.
Solutions

You might also like