matlab lect 2.pptx

25
Sana Zahid

Upload: qamar-abbas

Post on 14-Apr-2018

245 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 1/25

Sana Zahid

Page 2: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 2/25

Page 3: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 3/25

Problem 1 A particular circular cylindrical tank is 15 m tall andhas a radius 8 m. We want to construct another

cylindrical tank with a volume 20 % greater but havingthe same height. How large must its radius be?

Page 4: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 4/25

Solution r=8;

h=15;

 V= pi*r^2*h; V=V+0.2*V or V=1.2*V;

r=sqrt(V/(pi*h))

Page 5: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 5/25

Managing Working Session

Page 6: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 6/25

Polynomial Rootsx 3 - 7x2 + 40x – 34 = 0

MATLAB commands;

>>a= [1,-7,40,-34];>>roots(a)

ans =

3.0000 + 5.000i

3.0000 - 5.000i

1.0000

Page 7: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 7/25

Saving Data 

» clear » D = 2;

» v = 3;

» rho = 1000;

» mu = 0.001;

» NRe = D*v*rho/mu;

» f = 0.076*NRe^(-0.25);

»

» save data1 D v rho mu NRe f 

Page 8: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 8/25

Loading Data 

» clear » load data1

» who

Your variables are:

D f rho

 NRe mu v

» D

D =

2

Page 9: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 9/25

Common Mathematical Functions

exp (x)

sqrt (x)

log (x)

log10 (x)cos (x)

sin (x)

tan (x)

acos (x)asin (x)

atan (x)

Page 10: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 10/25

Problem:

Determine how many elements are in the array cos(0):0.02: log10(100). Determine the 25th element.

Find the roots of the polynomial 290-11x+6x2

+ x3

Page 11: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 11/25

Plotting With MATLAB

plot (x,y)

grid

gtext(‘ temperature ’) title(‘ graph between temp, press & vol’) 

xlabel(‘ pressure ‘) 

 ylabel(‘ volume ‘) Plot (x,y ,’ + ‘) 

Page 12: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 12/25

Problem:

Plot the function s=2 sin(3t+2)+√(5t+1) over theinterval 0<=t<=5. put a title on the plot and properly label the axes. The variable s represents speed in feetper second; the variable t represents time in seconds

Page 13: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 13/25

Linear Algebraic equations

6x + 12y +4z = 70

7x – 2y +3z = 5

2x +8y –

9z = 64>>A = [6,12,4;7,-2,3;2,8,-9];

>>B = [70;5;64];

>>Solution = A\B

Solution =3

5

-2

Page 14: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 14/25

Problem:

Use the Matlab to solve the following set of equations.

6x-4y+8z= 112

-5x+7z-3y=75 14x+9y-5z=-67

Page 15: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 15/25

Script file and Editor/Debugger 

Two ways to operate with MATLAB;

1. Interactive mode (for small calculations)

2. Script File (for complex or big problems)

MATLAB programs are saved in .m format.

M-files are built with the Editor/Debugger .

Page 16: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 16/25

M-files

 1. Script files; contains a sequence of MATLABcommands, and is useful when you need to use

many commands or arrays with many elements. 2. Function files; useful when you need to repeat the

operation of a set of commands. You can create yourown function files. (Detail in Chapter 3)

Page 17: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 17/25

How to run script file? Use of Comments?

>> % Mass Transfer Program

MATLAB ignores every thing to the right of the %symbol

Page 18: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 18/25

Example of Script File

% Program example.m

% This program computes the sine of 

% the square root and displays the results.x = sqrt ([5:2:30]);

y = sin (x);

z = cos (x);plot(x,y,x,z)

Page 19: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 19/25

Debugging

 Debugging; process of finding and removing the“bugs” or errors in a program. 

Syntax Error; related to typing or grammaticalmistake, MATLAB detects it automatically.

 Runtime error; are due to incorrect formula or

input. We have to find and fix it. e.g. Division by Zero.

Page 20: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 20/25

Programming Style

1. Comment Section2. Input Section

 3. Calculations Section4. Output Section

See Example of a good script file in your book at

Page 37

Page 21: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 21/25

For Changing MATLAB Look

Page 22: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 22/25

Input/Output commands

disp (A)

disp ( ‘ Text ’ ) 

x = input ( ‘ text ’ )

Page 23: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 23/25

num2str command

% This m-file calculates the area of a circle,

% and displays the result.

radius = 2.5;

area = pi * 2.5 ^ 2;

disp (area)

Page 24: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 24/25

Assignment

Problems:

Section 1.1( 1 to 6 group 1)

Section 1.1 (6 to 12 group 2)

Section 1.3 (13 to 18 group 3)

Section 1.3 (19 to 24 group 4)

Page 25: Matlab lect 2.pptx

7/27/2019 Matlab lect 2.pptx

http://slidepdf.com/reader/full/matlab-lect-2pptx 25/25

Practice more and more

&Have a nice dash.

(b +ve)