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

MEEN210_L02_MATLAB basics

This document serves as an introductory guide to using MATLAB for the Mechanical Engineering Department at North Carolina A&T State University. It covers basic operations, plotting data, and the structure of MATLAB's interface, including the Command, Graphic, and Edit Windows. Additionally, it compares MATLAB with Excel in terms of functionality and provides examples of mathematical operations and plotting techniques.

Uploaded by

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

MEEN210_L02_MATLAB basics

This document serves as an introductory guide to using MATLAB for the Mechanical Engineering Department at North Carolina A&T State University. It covers basic operations, plotting data, and the structure of MATLAB's interface, including the Command, Graphic, and Edit Windows. Additionally, it compares MATLAB with Excel in terms of functionality and provides examples of mathematical operations and plotting techniques.

Uploaded by

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

North Carolina A&T State University –

Mechanical Engineering Department


“Numerical Methods Using MATLAB”

MATLAB intro lab 1.

Today’s Goals.

1. MATLAB as a calculator (compare to excel)


2. Learning to plot data
MATLAB® at NCAT

2 ways to Run MATLAB:


1. NCA&T Computers;
Or
Log onto MATLAB Web Site (www.mathworks.com)
2. Download on your personal computer
(R2023a, 2 to 3 hours to download)

MATHWORKS Account for Downloading


MATLAB® for NCAT-MEEN 210:
Mechanical Engineering – MEEN 210

MATLAB – has 3 Main Windows and


2 Side Windows:
Main
1. Command Window
2. Graphic Window
3. Edit Window
Side
1. Current Folder
2. Workspace
MATLAB - Display Graphics

Editor Window Workspace


Current
Folder

Command Window
Text editor window
Directory
Where you write code Workspace
space,
(History)
Files, etc.
(Where you
save code and
data)
Command window
Where you type commands and run code
Let’s Play – MATLAB

In the Command Window


>> a
What did MATLAB do?
Command Window
>> a
Undefined function or variable 'a'.
Why?
What is a?

“a” has not been defined

Look at MATLAB’s message!


Command Window
>> a
Undefined function or variable 'a'

Look at MATLAB’s message!

MATLAB – tells you a lot – MATLAB is


looking for a “Known” Function or Variable!!
Defining some Variables
>> a=3
a=
3
>> b=5; c=7; d=9; e=11;

Why did MATLAB repeat a, but not b, c, d, or


e?
Command Window

>> X=99;

>> x = 88;

What is the value of X?

MATLAB Variables – Upper Case makes a different


variable.
Command Window
>> pi
ans =
3.1416
>> format long
>> pi
ans =
3.141592653589793
Order of Operations

Left to right
Mechanical Engineering – MEEN 210

Command Window

>> a+b*c/d^e 3 + 5 * 7/9^11

1st Action by MATLAB?


2nd
3rd
Mechanical Engineering – MEEN 210

Command Window
>> a+b*c/d^e 3 + 5 * 7/9^11
ans =
3.0000
>> format long (page 27)
Use the up arrow to recall previous
commands –
>> a+b*c/d^e
ans =
3.000000001115323
Mathematical Operations
Order of Calculations
Order of Priority (page 32):
1. ( ) 2. ^
3. * 4. /
5. + or -
Another Problem in the Command Window

>> 5 + 8 * 9 – 7 / 3 * 2^3
Answer = 58.3333
How?

1st Step?
“^” first = = > 5 + 8 * 9 – 7 / 3 * 8

2nd Step?
* and / = = > 5 + 72 – 2.3333 * 8
Command Window

>> 5 + 8 * 9 – 7 / 3 * 2^3

2nd Step?
* and / = = > 5 + 72 – 2.3333 * 8
5 + 72 – 18.6667

3rd Step?
+ and - = = > 77 + (-18.6667)
Mechanical Engineering – MEEN 210

Original equation
>> 5 + 8 * 9 – 7 / 3 * 2^3

4th Step?
Answer = 58.3333

Wow … got to be careful, here is a Secret


Control the mathematical operations with;
(…..)
Like a command inside your equations!
Mechanical Engineering – MEEN 210

Mathematical Operations
Order of Calculations
>> x1=(45/5)*10 - 20*(-50)

>> x2=(45/5)*10 + 20*(-50)

What are the values of x1 and x2?

Do in MATLAB
Mechanical Engineering – MEEN 210

Mathematical Operations Order of Calculations

>> x1=(45/5)*10 - 20*(-50)


45/5 =9, then 9*10 = 90 20*(-50) = -1000
90 – (-1000) = 1090

>> x2=(45/5)*10 + 20*(-50) = -910


90 -1000
90 + (-1000) = -910
MEEN 261 Materials Science
example problem
• Determine the equilibrium vacancy concentration
of 1 mol of Cu (N= 6.0220e+23 ) at 500C(
773.15K). Assume that the vacancy formation
energy is 0.90 eV and k is 8.62x10-5 eV/K.
• Governing Equation:
𝑛𝑛𝑣𝑣 = 𝑁𝑁𝑒𝑒 −𝐸𝐸𝑎𝑎 ⁄𝑘𝑘𝑘𝑘
• nv = number of vacancies
• N = number of atom sites
• Ev = vacancy formation energy ANSWER:
nv = 8.20x1017
• K = Boltzmann’s constant
nv/N = 1.36x10-6
• T = temperature in Kelvin Roughly 1 vacancy per million atoms
Mechanical Engineering – MEEN 210

MATLAB – has 3 Windows


1. Command Window
2. Graphic Window
3. Edit Window
Mechanical Engineering – MEEN 210

Graphic Window
Plotting Data and lines or curves through
the Data:
Very Powerful Tool in Engineering – Plotting

Can be done through the Command Window


or within a Function (Built-In to MATLAB or
written by you in a function)
Mechanical Engineering – MEEN 210

Plotting in MATLAB
Make some vectors; x and y

>> x=[1 2 3 4 5]; y=[.1 .2 .3 .4 1];

>> plot(x, y)
Mechanical Engineering – MEEN 210
Mechanical Engineering – MEEN 210

Plotting in MATLAB (continued)

>> plot(x, (sin(y)))


Mechanical Engineering – MEEN 210
Mechanical Engineering – MEEN 210

Plotting in MATLAB (continued)

>> hold on

>> plot(x, y, '--’)

Supported styles:
https://www.mathworks.com/help/matlab/cre
ating_plots/create-line-plot-with-
markers.html#bvcbmlz-1
Mechanical Engineering – MEEN 210
Mechanical Engineering – MEEN 210

Plotting in MATLAB (continued)

>> hold off

>> plot(x, y, '--’)


Mechanical Engineering – MEEN 210
A nice readable plot. Good for
presentation
Making nice graphs (more
details as we go)
• Play with the graph editor
• Add axis titles (NEVER PUT A GRAPH IN
A REPORT WITHOUT TITLES OR YOUR
PROFESSOR / BOSS / ADVISOR WILL
BURST IN TO FLAMES)
• Command line option:
> xlabel(‘x-axis (units)’)
> ylabel(‘y-axis (units)’)
GOOD Better?

Good practice:
• Labels and axes same font size as main document (or larger)
• Increase line width on plot lines and symbols
• Pay attention to white space. Don’t crowd axes.
• Each symbol and line should be a different color and or style (solid,
dashed, squares, triangles)
Mechanical Engineering – MEEN 210

Plotting in MATLAB
Make some vectors; x1 and y1

>> x1=[1 2 3 4 5 6]
>> y1=[.1 .2 .3 .4 1 1.5 1.7 1.9];

>> plot(x1, y1)

What happens?
Mechanical Engineering – MEEN 210

Vectors in MATLAB
>> xx = [
81
90
13
91
63
10
28
55
95
96
16 ]
Mechanical Engineering – MEEN 210

Vectors (continued)

>> yy = [
266
203
286
295
269
277
275
240
266
217
272]
Mechanical Engineering – MEEN 210

Handy commands with vectors

>> length(xx)

>> length(yy)

>> sum(xx)

>> sum(yy)
Mechanical Engineering – MEEN 210

Turn On
EXCEL®
Mechanical Engineering – MEEN 210

Let’s Play – EXCEL

Type in
a

What did EXCEL do?


Mechanical Engineering – MEEN 210

EXCEL does not use variables, it uses


Cells
Paste in these numbers into cells

5
7
9
11
Mechanical Engineering – MEEN 210

EXCEL does not use variables, it uses


Cells
Create a 3 new columns of values using the
column of numbers you just put in

x^2 x^2.456 cos(x)


Mechanical Engineering – MEEN 210

Cells in EXCEL
Numbers

Operations

Alpha Numerical Symbols Dog Cat x

Code (we will not get into)


Mechanical Engineering – MEEN 210

Mathematical Operations
Order of Calculations in EXCEL
Order of Priority:
1. ( ) 2. ^
3. * 4. /
5. + or -
Same as MATLAB
Mechanical Engineering – MEEN 210

EXCEL Plotting

Data in Columns

2 4
5 25
7 49
9 81
Mechanical Engineering – MEEN 210

Classwork Problems
Do in MATLAB Command Window and EXCEL

1. (9/11)/(33*(5*7)) =
2. 19/8 – (7/6) =
3. 2^(5 + (6/8)) =
4. (18 – (20/17)) =

Plot in MATLAB and EXCEL


Assume x is in radians not degrees
x = [1 4 8 9 15 23 31]
f(x) = y = sin(x) + x^3.7
Name Format:
CW#_MEEN2100#_USERNAME_P#
MATLAB Session 1.2 MATLAB Fundamentals Chapter 2

You might also like