Lab Intro

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

MATLAB

MATLAB (Matrix Laboratory)

MATLAB is a high-level language that has specialized toolboxes


Advantages
– Fast implementation and debugging
– Natural matrix operation
– Powerful image processing toolbox
_ Simulink modelling
_Computer Engineering –larger computations-collaborative Projects
_paid software
MATLAB (Main Window)
MATLAB
• MATLAB is a high-performance language for technical computing. It integrates computation,
visualization, and programming environments.
• 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.
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
MATLAB

Arithmetic Operations

>>1+2*3

>>x=1+2*3

>>4*x
Creating expressions

The syntax of variable assignment is

variable name = a value (or an expression)

For example,
>> x = expression

where expression is a combination of numerical values, mathematical operators, variables,


and function calls.

>> t = 5;
>> t = t+1
t=6
(Overwriting a variable)
Controlling the hierarchy of operations

>> (1+2)*3

>> 1+2*3

Exponentiations are done first, followed by multiplications and divisions, and finally by additions and
subtractions.
MATLAB

𝟏 𝟒 𝟔
Consider the example, + ∗
𝟐+𝟑𝟐 𝟓 𝟕
Write matlab command to execute the expression.
MATLAB
• >> 1/(2+3^2)+4/5*6/7

• ans = 0.7766

• if parentheses are missing,


• >> 1/2+3^2+4/5*6/7
• ans =
• 10.1857

Managing the workspace

>>clear

It is possible to keep track of everything done during a MATLAB session with the diary
command.
>> diary
MATLAB
• Entering multiple statements per line
• Use commas (,) or semicolons (;) to enter more than one statement at once. Commas (,) allow
multiple statements per line without suppressing output.
• >> a=7; b=cos(a), c=cosh(a)

b=
0.6570
c=
548.3170
Getting help

To view the online documentation, select MATLAB Help from Help menu or MATLAB Help
directly in the Command Window. The preferred method is to use the Help Browser. The
Help Browser can be started by selecting the ? icon from the desktop toolbar. On the other
hand, information about any command is available by typing

>> help Command

Quitting MATLAB

To end your MATLAB session, type quit in the Command Window, or select File −→ Exit

MATLAB in the desktop main menu.


Examples

Example 1: Compute y = 𝑒 −𝑎 sin(x) + 10√y, for a = 5, x = 2, and y = 8.


Examples

Example 1: Compute y = 𝑒 −𝑎 sin(x) + 10√y, for a = 5, x = 2, and y = 8.

>> a = 5; x = 2; y = 8;
>> y = exp(-a)*sin(x)+10*sqrt(y)

y=
28.2904
Examples

>> log(142)

ans =
4.9558

>> log10(142)

ans =
2.1523

Note the difference between the natural logarithm log(x) and the decimal logarithm (base
10) log10(x).
Examples

To calculate sin(π/4) and 𝑒 10 , we enter the following commands in MATLAB,

>> sin(pi/4)

ans =
0.7071

>> exp(10)
ans =
2.2026e+004
Creating simple plots

The MATLAB command to plot a graph is plot(x,y). The vectors x = (1, 2, 3, 4, 5, 6) and y = (3, −1, 2, 4,
5, 1)

>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot(x,y)
Experiment 1

1.Create a one dimensional array

>>t= linspace(1,10,10)

Linspace →Generate linearly spaced vector

y = linspace(x1,x2,n) generates n points. The spacing between the points is (x2-x1)/(n-1).

t=

1 2 3 4 5 6 7 8 9 10
Experiment 1

2.Create a two dimensional array

>>theta= [0; pi/4;pi/2;3*pi/4;pi;5*pi/4]

theta =

0
0.7854
1.5708
2.3562
3.1416
3.9270

>>x=1:1:10

x=

1 2 3 4 5 6 7 8 9 10
Experiment 1

2.Create a two dimensional array

>>a= zeros (2,2);

>>a

a=

0 0
0 0

>>a= ones (2,2);

>>a

a=

1 1
1 1
Experiment 1

2.Create a two dimensional array

>>a= eye (2,2);

>>a

a=

1 0
0 1
Experiment 1

3.Performing arithmetic operations

>>x=5.3;
>>y=7.8;

>> ((x*z)/(x/z)^2)+14*x^2-0.8*z^2

ans =

434.1261

>> (x^2)*z-(z^2)*x+(x/z)^2-(z/x)^(1/2)

ans =

-104.1014
Experiment 1

3.Performing arithmetic operations

>> (2^5)/(2^5-1)

ans =

1.0323

>> (1-(1/2^5))^-1

ans =

1.0323
Experiment 1

3.Performing arithmetic operations

>> pi*(pi^(1/3)-1)^2

ans =

0.6781

>> exp(3)

ans =

20.0855

>> log(exp(3))

ans =

3
Experiment 1

3.Performing arithmetic operations

>> log10(exp(3))

ans =

1.3029

>> log10(10^5)

ans =

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy