What is MATLAB and what problems can it solve?


Complete MAT LAB Exercise in attached file.
MATLAB sessions: Laboratory 1
MAT 275 Laboratory 1
Introduction to MATLAB
MATLAB is a computer software commonly used in both education and industry to solve a wide range
of problems.
This Laboratory provides a brief introduction to MATLAB, and the tools and functions that help
you to work with MATLAB variables and les.
The MATLAB Environment
To start MATLAB double-click on the MATLAB shortcut icon. The MATLAB desktop will open.
On the left side you will generally nd the Current Folder window and on the right the Workspace
and Command History windows. The Command Window is where the MATLAB commands are entered
and executed. Note that windows within the MATLAB desktop can be resized by dragging the separator
If you have never used MATLAB before, we suggest you type demo at the MATLAB prompt. Click
on Getting Started with MATLAB and run the le.
Basics And Help
Commands are entered in the Command Window.
Basic operations are +, -, *, and /. The sequence
>> a=2; b=3; a+b, a*b
ans =
ans =
denes variables a and b and assigns values 2 and 3, respectively, then computes the sum a+b and product
ab. Each command ends with , (output is visible) or ; (output is suppressed). The last command on a
line does not require a ,.
Standard functions can be invoked using their usual mathematical notations. For example
>> theta=pi/5;
>> cos(theta)^2+sin(theta)^2
ans =
veries the trigonometric identity sin2 + cos2 = 1 for =
be obtained by typing
A list of elementary math functions can
>> help elfun
To obtain a description of the use of a particular function type help followed by the name of the
function. For example
>> help cosh
gives help on the hyperbolic cosine function.
To get a list of other groups of MATLAB programs already available enter help:
>> help
Stefania Tracogna, SoMSS, ASU
MATLAB sessions: Laboratory 1
Another way to obtain help is through the desktop Help menu, Help > Product Help.
MATLAB is case-sensitive. For example
>> theta=1e-3, Theta=2e-5, ratio=theta/Theta
theta =
Theta =
ratio =
The quantities Inf () and NaN (Not a Number) also appear frequently. Compare
>> c=1/0
c =
>> d=0/0
d =
Plotting with MATLAB
To plot a function you have to create two arrays (vectors): one containing the abscissae, the other the
corresponding function values. Both arrays should have the same length. For example, consider plotting
the function
x2 sin(x) + ex
y = f (x) =
for 0 x 2. First choose a sample of x values in this interval:
>> x=[0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1, …
x =
Columns 1 through 7
Columns 8 through 14
Columns 15 through 21
Note that an ellipsis … was used to continue a command too long to t in a single line.
Rather than manually entering each entry of the vector x we can simply use
>> x=0:.1:2
>> x=linspace(0,2,21)
Both commands above generate the same output vector x.
The output for x can be suppressed (by adding ; at the end of the command) or condensed by entering
>> format compact
Stefania Tracogna, SoMSS, ASU
MATLAB sessions: Laboratory 1
(This format was used for all previous outputs).
To evaluate the function f simultaneously at all the values contained in x, type
>> y=(x.^2-sin(pi.*x)+exp(x))./(x-1)
y =
Columns 1 through 6
Columns 7 through 12
-5.6491 -11.3888 -29.6059
Columns 13 through 18
Columns 19 through 21
Note that the function becomes innite at x = 1 (vertical asymptote). The array y inherits the dimension
of x, namely 1 (row) by 21 (columns). Note also the use of parentheses.
In the above example *, / and ^ are preceded by a dot . in order for the expression to be evaluated for
each component (entry) of x. This is necessary to prevent MATLAB from interpreting these symbols
as standard linear algebra symbols operating on arrays. Because the standard + and – operations on
arrays already work componentwise, a dot is not necessary for + and -.
The command
>> plot(x,y)
creates a Figure window and shows the function. The gure can be edited and manipulated using the
Figure window menus and buttons. Alternately, properties of the gure can also be dened directly at
the command line:
axis([0,2,-10,20]); grid on;
title(f(x)=(x^2-sin(pi x)+e^x)/(x-1));
xlabel(x); ylabel(y);
The number of x-values has been increased for a smoother curve (note that the stepsize is now .01
rather than .1).
The option r- plots the curve in red.
LineWidth,2 sets the width of the line to 2 points (the default is 0.5).
The range of x and y values has been reset using axis([0,2,-10,20]) (always a good idea in the
presence of vertical asymptotes).
The command grid on adds a grid to the plot.
A title and labels have been added.
The resulting new plot is shown in Fig. L1a. For more options type help plot in the Command
Stefania Tracogna, SoMSS, ASU
MATLAB sessions: Laboratory 1
Figure L1a: A Figure window
Scripts and Functions
Files containing MATLAB commands are called m-les and have a .m extension. They are two types:
1. A script is simply a collection of MATLAB commands gathered in a single le. The value of the
data created in a script is still available in the Command Window after execution. To create a
new script select the MATLAB desktop File menu File > New > Script. In the MATLAB text
editor window enter the commands as you would in the Command window. To save the le use
the menu File > Save or File > Save As…, or the shortcut SAVE button
Variable dened in a script are accessible from the command window.
2. A function is similar to a script, but can accept and return arguments. Unless otherwise specied
any variable inside a function is local to the function and not available in the command window.
To create a new function select the MATLAB desktop File menu File > New > Function. A
MATLAB text editor window will open with the following predened commands
function [ output_args ] = Untitled3( input_args )
%UNTITLED3 Summary of this function goes here
Detailed explanation goes here
The output args are the output arguments, while the input args are the input arguments. The
lines beginning with % are to be replaced with comments describing what the functions does. The
command(s) dening the function must be inserted after these comments and before end.
To save the le proceed similarly to the Script M-le.
Use a function when a group of commands needs to be evaluated multiple times.
Examples of script/function:
1. script
Stefania Tracogna, SoMSS, ASU
% x-values
% y-values
MATLAB sessions: Laboratory 1
axis([0,2,-10,20]); grid on;
title(f(x)=(x^2-sin(pi x)+e^x)/(x-1));
xlabel(x); ylabel(y);
plot in red with wider line
set range and add grid
add title
add labels
evaluate myfunction at x
plot in red
set range and add grid
add title
add labels
2. script+function (two separate les)
myplot2.m (driver script)
axis([0,2,-10,20]); grid on;
title(f(x)=(x^2-sin(pi x)+e^x)/(x-1));
xlabel(x); ylabel(y);
myfunction.m (function)
function y=myfunction(x)
% defines function
% y-values
3. function+function (one single le)
myplot1.m (driver script converted to function + function)
function myplot1
% x-values
% evaluate myfunction at x
% plot in red
axis([0,2,-10,20]); grid on;
% set range and add grid
title(f(x)=(x^2-sin(pi x)+e^x)/(x-1));
% add title
xlabel(x); ylabel(y);
% add labels
%—————————————-function y=myfunction(x)
% defines function
% y-values
In case 2 myfunction.m can be used in any other m-le (just as other predened MATLAB functions).
In case 3 myfunction.m can be used by any other function in the same m-le (myplot1.m) only. Use 3
when dealing with a single project and 2 when a function is used by several projects.
Note that the function myplot1 does not have explicit input or output arguments, however we cannot
use a script since the construct script+function in one single le is not allowed.
It is convenient to add descriptive comments into the script le. Anything appearing after % on any
given line is understood as a comment (in green in the MATLAB text editor).
To execute a script simply enter its name (without the .m extension) in the Command Window (or
click on the SAVE & RUN button
The function myfunction can also be used independently if implemented in a separate le myfunction.m:
>> x=2; y=myfunction(x)
y =
Stefania Tracogna, SoMSS, ASU
MATLAB sessions: Laboratory 1
A script can be called from another script or function (in which case it is local to that function).
If any modication is made, the script or function can be re-executed by simply retyping the script
or function name in the Command Window (or use the up-arrow on the keyboard to browse through
past commands).
By default MATLAB saves les in the Current Folder. To change directory use the Current Directory
box on top of the MATLAB desktop.
A function le can contain a lot more than a simple evaluation of a function f (x) or f (t, y). But in
simple cases f (x) or f (t, y) can simply be dened using the inline syntax.
For instance, if we want to dene the function f (t, y) = t2 y, we can write the function le f.m
function dydt = f(t,y)
dydt = t^2-y;
and, in the command window, we can evaluate the function at dierent values:
>> f(2,1)
ans =
% evaluate the function f at t = 2 and y = 1
or we can dene the function directly on the command line with the inline command:
>> f = inline(t^2-y,t,y)
f =
Inline function:
f(t,y) = t^2-y
>> f(2,1)
% evaluate the function f at t = 2 and y = 1
ans =
However, an inline function is only available where it is used and not to other functions. It is not recommended when the function implemented is too complicated or involves too many statements.
Alternatively, the function can be entered as an anonymous function
>> f = @(t,y)(t^2-y)
The names of script or function M-les must begin with a letter. The rest of the characters may
include digits and the underscore character. You may not use periods in the name other than the
last one in .m and the name cannot contain blank spaces.
Avoid name clashes with built-in functions. It is a good idea to rst check if a function or a script
le of the proposed name already exists. You can do this with the command exist(name),
which returns zero if nothing with name name exists.
NEVER name a script file or function file the same as the name of the variable it computes. When
MATLAB looks for a name, it rst searches the list of variables in the workspace. If a variable of
the same name as the script le exists, MATLAB will never be able to access the script le.
Stefania Tracogna, SoMSS, ASU
MATLAB sessions: Laboratory 1
You will need to record the results of your MATLAB session to generate your lab report. Create a
directory (folder) on your computer to save your MATLAB work in. Then use the Current Directory
eld in the desktop toolbar to change the directory to this folder. Now type
diary lab1 yourname.txt
followed by the Enter key. Now each computation you make in MATLAB will be save in your directory
in a text le named lab1 yourname.txt. When you have nished your MATLAB session you can turn
o the recording by typing diary off at the MATLAB prompt. You can then edit this le using your
favorite text editor (e.g. MS Word).
Lab Write-up: Now that your diary le is open, enter the command format compact (so that when
you print out your diary le it will not have unnecessary blank lines), and the comment line
% MAT 275 MATLAB Assignment # 1
Include labels to mark the beginning of your work on each part of each question, so that your edited lab
write-up has the format
% Exercise 1
% Exercise 2
Final Editing of Lab Write-up: After you have worked through all the parts of the lab assignment
you will need to edit your diary le.
Remove all typing errors.
Unless otherwise specied, your write-up should contain the MATLAB input commands,
the corresponding output, and the answers to the questions that you have written.
If the exercise asks you to write an M-le, copy and paste the le into your diary le in the
appropriate position (after the problem number and before the output generated by the le).
If the exercise asks for a graph, copy the gure and paste it into your diary le in the appropriate
position. Crop and resize the gure so that it does not take too much space. Use ; to suppress
the output from the vectors used to generate the graph. Make sure you use enough points for your
graphs so that the resulting curves are nice and smooth.
Clearly separate all exercises. The exercises numbers should be in a larger format and in boldface.
Preview the document before printing and remove unnecessary page breaks and blank spaces.
Put your name and class time on each page.
Important: An unedited diary file without comments submitted as a lab write-up is not
1. All points with coordinates x = r cos() and y = r sin(), where r is a constant, lie on a circle
with radius r, i.e. satisfy the equation x2 + y 2 = r2 . Create a row vector for with the values
0, 4 , 2 , 3
4 , , and 4 .
Take r = 2 and compute the row vectors x and
y. Now check that x and y indeed satisfy the
equation of a circle, by computing the radius r = x2 + y 2 .
Hint: To calculate r you will need the array operator .^ for squaring x and y. Of course, you
could also compute x2 by x.*x.
Stefania Tracogna, SoMSS, ASU
MATLAB sessions: Laboratory 1
2. Use the linspace command or the colon operator : to create a vector t with 91 elements:
et/10 sin(t)
(make sure you use ; to suppress
1, 1.1, 1.2, . . . , 10 and dene the function y =
t2 + 1
the output for both t and y).
(a) Plot the function y in black and include a title with the expression for y.
(b) Make the same plot as in part (a), but rather than displaying the graph as a curve, show the
unconnected data points. To display the data points with small circles, use plot(t,y,o).
Now combine the two plots with the command plot(t,y,o-) to show the line through the
data points as well as the distinct data points.
3. Use the command plot3(x,y,z) to plot the circular helix x(t) = sin t, y(t) = cos t, z(t) = t
0 t 20.
NOTE: Use semicolon to suppress the output when you dene the vectors t, x, y and z. Make sure
you use enough points for your graph so that the resulting curve is nice and smooth.
4. Plot y = cos x in red with a solid line and z = 1 x2 + x24 in blue with a dashed line for 0 x
on the same plot.
Hint: Use plot(x,y,r,x,z,–).
Add a grid to the plot using the command grid on.
NOTE: Use semicolon to suppress the output when you dene the vectors x, y and z. Make sure
you use enough points for your graph so that the resulting curves are nice and smooth.
5. The general solution to the dierential equation
y(x) =
= x + 2 is
+ 2x + C
with y(0) = C.
The goal of this exercise is to write a function le to plot the solutions to the dierential equation
in the interval 0 x 4, with initial conditions y(0) = 1, 0, 1.
The function le should have the structure function+function (similarly to the M-le myplot1.m
Example 3, page 5). The function that denes y(x) must be included in the same le (note that
the function dening y(x) will have two input arguments: x and C).
Your M-le should have the following structure (ll in all the ?? with the appropriate commands):
function ex5
x = ?? ;
y1 = f(??);
y2 = f(??);
y3 = f(??);
% define the vector x in the interval [0,4]
compute the solution with C = -1
compute the solution with C = 0
compute the solution with C = 1
plot the three solutions with different line-styles
add a title
add a legend
function y = f(x,C)
y = ?? % fill-in with the expression for the general solution
Plot the graphs in the same window and use dierent line-styles for each graph. To plot the graphs
in the same window you can use the command hold on or use the plot command similarly to
Exercise 4.
Add the title Solutions to dy/dx = x + 2.
Add a legend on the top left corner of the plot with the list of C values used for each graph.
Stefania Tracogna, SoMSS, ASU
MATLAB sessions: Laboratory 1
(Type help plot for a list of the dierent line-styles, and help legend for help on how to add a
legend.) Include both the M-le and the plot in your report.
NOTE: the only output of the function le should be the graph of the three curves. Make sure
you use enough points so that the curves are nice and smooth.
as an inline or anonymous function (see page 6).
Evaluate the function at x = 2 and y = 1.
6. (a) Enter the function f (x, y) = x3 +
(b) Type clear f to clear the value of the function from part (a). Now write a function M-le
. Save the le as f.m (include the M-le in your report).
for the function f (x, y) = x3 +
Evaluate the function at x = 2 and y = 1 by entering f(2,-1) in the command window.
Stefania Tracogna, SoMSS, ASU

MATLAB is a software that has grown very popular over the years, it is extensively used in both education and industry to solve several kinds of problems. In this laboratory, we have a brief introductory course on MATLAB and how we can work with MATLAB variables, files, tools, and functions.

Don't use plagiarized sources. Get Your Custom Essay on
What is MATLAB and what problems can it solve?
Just from $13/Page
Order Essay

The Laboratory 1 of MAT 275 comprises a comprehensive study of MATLAB. It acquaints you with the MATLAB environment, how to open MATLAB desktop, its features and functions, and how to compute and process data. This lab will help you understand basic operators like +, -, *, / and how to define variables. It also sheds light on invoking standard mathematical functions and obtaining help on any MATLAB function using the help command. Furthermore, it covers plotting functions in MATLAB by creating two arrays, i.e., vectors and much more. By the end of this laboratory, you will have a thorough understanding of the basic concepts of MATLAB and the tools to work efficiently in MATLAB’s environment.


1. To introduce students to the basic functionality of MATLAB software.
2. To demonstrate the use of MATLAB for solving mathematical problems.
3. To teach students how to plot functions using MATLAB.
4. To equip students with basic commands and functions required for carrying out mathematical operations in MATLAB.
5. To help students develop an understanding of the MATLAB environment.

Learning Outcomes:

1. Students will be able to launch MATLAB and navigate through its different windows and tools.
2. Students will be able to define and manipulate variables in MATLAB by carrying out basic mathematical operations.
3. Students will be able to invoke standard functions in MATLAB to carry out complex mathematical operations.
4. Students will be able to use the “help” command to obtain assistance with MATLAB functions and commands.
5. Students will be able to plot mathematical functions by creating arrays containing abscissae and function values.
6. Students will develop a basic understanding of MATLAB language syntax and programming structure.


1. Introduction to MATLAB
2. The MATLAB Environment
3. Basics and Help in MATLAB
4. Plotting with MATLAB.

Solution 1:

To plot a function in MATLAB, one needs to create two arrays (vectors) with the same length: one consisting of the abscissae and the other consisting of the corresponding function values. The following are the steps to plot a function f(x) = x^2 sin(x) + e^x for 0 ≤ x ≤ 2:

1. Choose a sample of x values in the interval [0, 2] using the colon operator and store them in the variable x:
>> x = 0:0.1:2;

2. Evaluate the function at each value of x using element-wise operations and store the results in the variable y:
>> y = x.^2 .* sin(x) + exp(x);

3. Plot the function using the plot command:
>> plot(x, y);

4. Customize the plot by adding title, axis labels, legend, etc.

Solution 2:

One can use MATLAB’s built-in functions to perform basic operations and manipulate variables. The following are some examples:

1. To compute the sum and product of two variables a = 2 and b = 3, denoted as a + b and a * b respectively, one can use the following commands:
>> a = 2; b = 3;
>> sum = a + b;
>> product = a * b;

2. To verify the trigonometric identity sin^2(x) + cos^2(x) = 1 for x = π/5, one can use the cos and sin functions as follows:
>> x = pi/5;
>> identity = cos(x)^2 + sin(x)^2;

3. To obtain help on a specific function, one can use the help command followed by the name of the function. For example, to obtain help on the cosine hyperbolic function cosh, one can use the following command:
>> help cosh;

4. To list MATLAB’s available groups of programs, one can use the command help:.

Suggested Resources/Books:
1. MATLAB: An Introduction with Applications, by Amos Gilat
2. Learning MATLAB: A Problem Solving Approach, by Walter Gander and Jiří Hřebíček
3. MATLAB for Engineers, by Holly Moore
4. MATLAB Primer, Eighth Edition, by Timothy A. Davis and Kermit Sigmon
5. MATLAB: A Practical Introduction to Programming and Problem Solving, by Stormy Attaway

Similar asked questions:
1. What is MATLAB and how is it used in industry and education?
2. What are the basic operations that can be performed in MATLAB?
3. How can standard functions be invoked in MATLAB?
4. How can help be obtained in MATLAB?
5. How can functions be plotted in MATLAB?

Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more
× How can I help you?