0% found this document useful (0 votes)
30 views

Lab 3

This document provides instructions for creating and working with M-files and plotting in MATLAB. It discusses two types of M-files: script files and function files. It also provides examples of how to create script files using the MATLAB editor and how to save and open scripts. The document then discusses MATLAB plotting, including how to generate simple two-dimensional plots using the plot command along with optional arguments to customize the plot line style, color, and markers. Exercises are provided to have the user generate sample plots and scripts, and homework assignments are given to create additional scripts involving plotting functions.

Uploaded by

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

Lab 3

This document provides instructions for creating and working with M-files and plotting in MATLAB. It discusses two types of M-files: script files and function files. It also provides examples of how to create script files using the MATLAB editor and how to save and open scripts. The document then discusses MATLAB plotting, including how to generate simple two-dimensional plots using the plot command along with optional arguments to customize the plot line style, color, and markers. Exercises are provided to have the user generate sample plots and scripts, and homework assignments are given to create additional scripts involving plotting functions.

Uploaded by

Enas Qtaifan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Engineering Application LAB

Applied Science Private University

LAB 3
MATLAB
(M-file, Plotting )

Dr. Abdelrahman Radwan

Student Name
Student ID
Student Score

1
M-file
MATLAB is a programming language, as well as an interactive
computational environment. Files that include code in the MATLAB
language are called M-files.

M-files are ordinary ASCII text data written in MATLAB's language. Such
files are called "M-files" because they must have the filename extension
".m" at the end of their name (like myfunction.m). This extension is
needed for this data to be interpreted by MATLAB.

M files can be created using any editor or word processing function.

There are two types of M-Files:

1- M-File Scripts: Scripts do not accept input arguments or return


output arguments. They operate on data in the workspace.
2- M-File Functions: Functions can accept input arguments and
return output arguments. Internal variables are local to the
function.

Create Script using MATLAB editor:

Use New Script icon on the Home tab of the environment.

Use Ctrl + n keyboard shortcut to get new script file.

On clicking the new script icon or using the keyboard shortcut, a blank
untitled file gets opened.

The blank file has a default directory to save it. We can change the file
storage location as per our requirement.

Save the script with the keyboard shortcut Ctrl + s or use the Save
icon which is placed along with the new script icon.

2
Use Ctrl + O keyboard shortcut or open file icon to open an already
existing script.

There are three more menu tabs available to create a script as, Editor,
Publish, and View.

We can work with multiple scripts in new tabs.

The Command Window area is divided into two panes, one for Script
editor and another one for Command Window.

We can simultaneously work with script and command line.

3
MATLAB Plotting
Plotting is a graphical representation of a data set that shows a
relationship between two or more variables. MATLAB plots play an
essential role in the field of mathematics, science, engineering,
technology, and finance for statistics and data analysis.
The plot command is used to create two-dimensional plots. The simplest
form of the command is:

The arguments x and y are each a vector (one-dimensional array). The


two vectors must have the same number of elements. When the plot
command is executed, a figure is created in the Figure Window.

Example 1:

>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

>> b = [0, 1, -1, 1, 0];

>> plot(a, b)

Output:

4
Example 2: Plot the function sin (a) on the interval [0, 3π], we first create
a vector of a values ranging from 0 to 3π, then compute the sine of
these values, and finally plot the result:

1. a = 0:pi/100:3*pi; % range is same as used while creating vector


2. b = sin(a);
3. plot (a, b)

The plot command has additional, optional arguments that can be used
to specify the color and style of the line and the color and type of
markers, if any are desired. With these options the command has the
form:

Some examples:
5
plot(x,y) A blue solid line connects
the points with no markers
(default).
plot(x,y,‘r’) A red solid line connects the
points.
plot(x,y,‘--y’) A yellow dashed line
connects the points.
plot(x,y,‘*’) The points are marked with *
(no line between the points).
plot(x,y,‘g:d’) A green dotted line connects
the points that are marked
with diamond markers.

Exercise
1- Plot the cosine at the interval from 0<x<4π ,and use range
300.

2- Create new script as following:


 On the Home tab, click the New Script button.
 Use the edit function. For example, edit new_file_name creates (if
the file does not exist) and opens the file new_file_name.
If new_file_name is unspecified, MATLAB opens a new file
called Untitled.
 After you create a script, you can add code to the script and save
it. For example, you can save this code that generates random
numbers from 0 through 100 as a script called numGenerator.m.

columns = 10000;
rows = 1;
bins = columns/100;

rng(now);
list = 100*rand(rows,columns);
histogram(list,bins)

 Save your script and run the code using either of these methods:

6
 Type the script name on the command line and press Enter. For
example, to run the numGenerator.m script, type numGenerator.
 On the Editor tab, click the Run button.

Homework

1- In new script named “exec_1”, plot the function yx


2e 0.2x for the range 0  x 10 .

2- Refer to example 2 and exercise 1, then in new script


“exec_2” use the codes to plot sine and cosine at the
interval from 0<x<2π, where sine plot should be in
solid line, red color, marked with “*”. For cosine use
dashed line with green line color and marked with
circle.

3- Search online on how to plot the cosine and sine in


single plotting area from homework2, after that search
on how to add labels to x-axis, Y axis, and title (the
plot title should be your name and ID”. Write your
code in new script “exec_3”.

7
Plotting Manual

You might also like