0% found this document useful (0 votes)
5 views12 pages

MATLAB Report 1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

College of Engineering

Department of Technical Power Engineering


The First Stage

MATLAB Program Report

Author: Mustafa Jameel Thabit


Date: 19/01/2025
Class: B2
Institution: Al Maqaal University
Introduction
MATLAB (Matrix Laboratory) is a high-level programming language and
interactive environment designed for numerical computation, data analysis,
visualization, and algorithm development. It is widely used in engineering, science,
economics, and other technical fields. Below is a summary of MATLAB's key
features, applications, and resources. The tutorials are independent of the rest of
the document. The primarily objective is to help you learn quickly the first steps.
The emphasis here is “learning by doing”. Therefore, the best way to learn is by
trying it yourself. Working through the examples will give you a feel for the way
that MATLAB operates. In this introduction we will describe how MATLAB
handles simple numerical expressions and mathematical formulas.
The name MATLAB stands for MATrix LABoratory. MATLAB was written
originally to provide easy access to matrix software developed by the LINPACK
(linear system package) and EISPACK (Eigen system package) projects.
MATLAB [1] is a high-performance language for technical computing. It
integrates computation, visualization, and programming environment.
Furthermore, MATLAB is a modern programming language environment: it has
sophisticated data structures, contains built-in editing and debugging tools, and
supports object-oriented programming. These factors make MATLAB an excellent
tool for teaching and research.
MATLAB has many advantages compared to conventional computer
languages (e.g., C, FORTRAN) for solving technical problems. MATLAB is an
interactive system whose basic data element is an array that does not require
dimensioning. The software package has been commercially available since 1970
and is now considered as a standard tool at most universities and industries
worldwide.
It has powerful built-in routines that enable a very wide variety of
computations. It also has easy to use graphics commands that make the
visualization of results immediately available. Specific applications are collected in
packages referred to as toolbox. There are toolboxes for signal processing,
symbolic computation, control theory, simulation, optimization, and several other
fields of applied science and engineering.
Basic features:
As we mentioned earlier, the following tutorial lessons are designed to get you started quickly in
MATLAB. The lessons are intended to make you familiar with the basics of MATLAB. We urge
you to complete the exercises given at the end of each lesson.

Key Features of MATLAB


1. Matrix Operations:
o MATLAB is optimized for matrix and vector operations, making it ideal for linear
algebra and numerical computations.
o Example: A = [1, 2; 3, 4]; B = A * 2;
2. Built-in Functions:
o MATLAB provides a vast library of built-in functions for mathematical
operations, statistics, signal processing, and more.
o Example: sin(x), fft(signal), mean(data).
3. Data Visualization:
o MATLAB offers powerful tools for creating 2D and 3D plots, graphs, and
animations.
o Example: plot(x, y), surf(X, Y, Z), bar(data).
4. Toolboxes:
o MATLAB includes specialized toolboxes for various applications, such as:
 Signal Processing Toolbox: For signal analysis and filtering.
 Image Processing Toolbox: For image manipulation and analysis.
 Machine Learning Toolbox: For building and training machine
learning models.

5. Simulink:
o Simulink is a MATLAB-based environment for modeling, simulating, and
analyzing dynamic systems.

6. Scripting and Programming:


o MATLAB supports scripting and programming for automating tasks and
developing complex algorithms.
o Example: Writing .m files for reusable code.
7. Interoperability:
o MATLAB can interface with other programming languages like Python, C/C++,
and Java.
o It also supports importing/exporting data from/to Excel, CSV, and other formats.

Applications of MATLAB
1. Engineering:
o Control systems, signal processing, and robotics.
2. Science:
o Physics simulations, data analysis, and modeling.
3. Finance:
o Risk analysis, portfolio optimization, and algorithmic trading.
4. Machine Learning:
Building and training predictive models.
o
5. Image and Video Processing:
o Object detection, image enhancement, and computer vision.

Starting MATLAB
After logging into your account, you can enter MATLAB by double-clicking on the MATLAB
shortcut icon (MATLAB 7.0.4) on your Windows desktop. When you start MATLAB, a
special window called the MATLAB desktop appears. The desktop is a window that contains
other windows. The major tools within or accessible from the desktop are:
• The Command Window
• The Command History
• The Workspace
• The Current Directory
• The Help Browser
• The Start button
The graphical interface to the MATLAB workspace

When MATLAB is started for the first time, the screen looks like the one that shown in the
Figure 1.1. This illustration also shows the default configuration of the MATLAB desktop. You
can customize the arrangement of tools and documents to suit your needs. Now, we are
interested in doing some simple calculations. We will assume that you have sufficient
understanding of your computer under which MATLAB is being run. You are now faced with
the MATLAB desktop on your computer, which contains the prompt (>>) in the Command
Window. Usually, there are 2 types of prompt:
>> for full version
EDU> for educational version
Note: To simplify the notation, we will use this prompt, >>, as a standard prompt sign, though
our MATLAB version is for educational purpose.

Using MATLAB as a calculator


As an example of a simple interactive calculation, just type the expression you want to evaluate.
Let’s start at the very beginning. For example, let’s suppose you want to calculate the expression,
1 + 2 × 3. You type it at the prompt command (>>) as follows,
>> 1+2*3
ans =
7
You will have noticed that if you do not specify an output variable, MATLAB uses a default
variable ans, short for answer, to store the results of the current calculation. Note that the
variable ans is created (or overwritten, if it is already existed). To avoid this, you may assign a
value to a variable or output argument name. For example,
>> x = 1+2*3
x=
7
will result in x being given the value 1 + 2 × 3 = 7. This variable name can always
be used to refer to the results of the previous computations. Therefore, computing 4x will
result in
>> 4*x
ans =
28.0000

Before we conclude this minimum session, Table 1.1 gives the partial list of arithmetic operators.
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 can produce one or more outputs.

Example
Consider the system of equations:

Find the solution x to the system of equations.


Solution:
• Use the MATLAB editor to create a file: File → New → M-file.
• Enter the following statements in the file:
A = [1 2 3; 3 3 4; 2 3 3];
b = [1; 1; 2];
x = A\b
• Save the file, for example.m
• Run the file, in the command line, by typing:
>> example1
x=
-0.5000
1.5000
-0.5000
When execution completes, the variables (A, b, and x) remain in the workspace. To see a
listing of them, enter whos at the command prompt.
Note: The MATLAB editor is both a text editor specialized for creating M-files and a
graphical MATLAB debugger. The MATLAB editor has numerous menus for tasks such as
saving, viewing, and debugging. Because it performs some simple checks and also uses color
to differentiate between various elements of codes, this text editor is recommended as the
tool of choice for writing and editing M-files.
There is another way to open the editor:
>> edit
or
>> edit filename.m

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.
We have already seen the two first cases. Here, we will focus our attention on the third one. In
this case, the variable is defined in the script file. When the file is executed, the user is prompted
to assign a value to the variable in the command prompt. This is done by using the input
command. Here is an example.

% This script file calculates the average of points

% scored in three games.

% The point from each game are assigned to a variable

% by using the ‘input’ command.

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


game2 = input(’Enter the points scored in the second game ’);

game3 = input(’Enter the points scored in the third game ’);

average = (game1+game2+game3)/3

The following shows the command prompt when this script file (saved as example3) is
executed.

>> example3

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

The input command can also be used to assign string to a variable. For more information, see MATLAB
documentation.
A typical example of M-file function programming can be found in a recent paper which related to the
solution of the ordinary differential equation (ODE) [12].

Advantages of MATLAB
1. Ease of Use:
o Intuitive syntax and interactive environment.
2. Rich Library:
o Extensive built-in functions and toolboxes.
3. Visualization:
o High-quality graphs and plots for data representation.
4. Community Support:
o Active user community and extensive documentation.
Conclusion
This report demonstrated four MATLAB programs to solve common mathematical problems.
MATLAB is a versatile tool for numerical computation and problem-solving, and the provided
references can help students and professionals enhance their MATLAB skills.

Example MATLAB Codes:


1. Solving a Quadratic Equation:
Solve the quadratic equation ax2+bx+c=0 and find its roots.
>> a=1;
>> b=-3;
>> c=2;
>> discriminant=b^2-4*a*c; disp(discriminant);
1
>> root1=(-b+sqrt(discriminant))/(2*a); disp(root1);
2
>> root2=(-b-sqrt(discriminant))/(2*a); disp(root2);
1
>>

2. Sum of Four Numbers:


write a program to calculate the sum of four numbers.
>> num1 = input('10:');
10:
>> num2 = input('20:');
20:
>> num3 = input('30:');
30:
>> num4 = input('40:');
40:
>> sum=num1+num2+num3+num4;
>> sum=10+20+30+40

sum =

100
>>

3. Calculating Mathematical expressions:


calculate the value of r=a.b-c/d
>> a=5;
>> b=3;
>> c=10;
>> d=2;
>> z=a*b-c/d; disp(z);
10
>>

4. Solving a System of Linear Equations:


Solve the system of linear equations:

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


>> B = [5; 1];
>> X=A/B

X=
0.4000 0
0.6000 0
0.8000 0
-0.2000 0

References
1. MATLAB Official Documentation:
https://www.mathworks.com/help/matlab/
Comprehensive documentation for MATLAB functions and features.
2. MATLAB Onramp (Free Interactive Tutorial):
https://www.mathworks.com/learn/tutorials/matlab-onramp.html
A beginner-friendly tutorial to learn MATLAB basics.
3. MATLAB Programming for Engineers (Coursera):
https://www.coursera.org/learn/matlab
A structured course for learning MATLAB programming.
4. MATLAB Central (Community Forum):
https://www.mathworks.com/matlabcentral/
A platform to ask questions and share knowledge with the MATLAB community.

You might also like