lab02

17
ASSIGNMENT AND MATHERMATICAL OPERATION IN MATLAB VARIABLE SCOPE SCRIPTS AND FUNCTIOND (M-FILES) PLOTTING IN MATLAB

Upload: mxr-3

Post on 21-Jul-2016

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: lab02

ASSIGNMENT AND MATHERMATICAL OPERATION IN MATLAB

VARIABLE SCOPE

SCRIPTS AND FUNCTIOND (M-FILES)

PLOTTING IN MATLAB

Page 2: lab02

Complex value;

>> x = 2+i*4

x =

2.0000+4.0000i

i 1 aspresentedis

Page 3: lab02

For π (pi); >> pi ans = 3.1416 COLON and Semi-Colon Operators (Colon - creates a column) (Semi-Colon creates matrix of rows and columns) >> t = 1:5 t = 1 2 3 4 5

>> t = 1:0.5:3 (column with a step of 0.5) t= 1.0000 1.5000 2.0000 2.5000 3.0000

Page 4: lab02

Semicolon operator (create matrix >> A = [1 2 3; 4 5 6; 7 8 9] A= 1 2 3 4 5 6 7 8 9 >>A(2,:) ans = 4 5 6 >>t = 10:-1:5 t = 10 9 8 7 6 5 >>t (2:4) t = 9 8 7

Page 5: lab02

Solve the mathematical model of the parachutist problem using the command window

m = 68.1

c = 12.5

t = 2

g = 9.8

v = ?

))/exp(1()(

tmcc

gmv

Page 6: lab02

Through command window:

>> g = 9.8;

>> m = 68.1;

>> cd = 12.5;

>> tf = 2;

>> v= ((g*m)/cd)*(1-exp((-cd/m)*tf))

v =

16.4050

Page 7: lab02

Through M-file: i) Script file ii) Function file i) Solve the previous problem using script file: Steps: 1. Open file, new, M-file 2. Edit function will be available 3. Type the problem statement in the editor file 4. Save the editor file as XXXX.m (any name that you like) 5. Return to command window and type the saved file name >>XXXX ans = 50.6175

Page 8: lab02

Through script file;

In editor file:

g= 9.81; m = 68.1; tf = 2; cd = 12.5;

v= ((g*m)/cd)*(1-exp((-cd/m)*tf))

Save the editor file to your desired name

xxxx.M

Run the program through command window by typing the name of the M-file

>>xxxx

v= 16.4050

Page 9: lab02

To see the effect of mass on the velocity at 2 seconds: Steps: 1. Recall the previous M-file - xxxx.m 2. g= 9.81; m = input(‘mass (kg) :’); tf = 2; cd = 12.5; v= ((g*m)/cd)*(1-exp(-cd/m)*tf) Save the edited file as xxxx2.m In command windor type the name of the edited m-file, the

promt would show mass (kg) : (enter any value and result will be displayed) v = 17.3420

Page 10: lab02

Solve the previous problem using function file;

Function files are M-files that start with the word function.

Accept input argument and return output.

Syntax for the function file can be represented as;

1. Function outvar = funcname (arglist)

2. % helpcomments

3. Statements

4. Outvar = value

Page 11: lab02

outvar = name of the output variable,

example (velocity )

funcname = the function file name,

example (xxxx.m)

arglist = argument list,

example (m, cd, tf)

% helpcomments = provide information to the user and doest not involve in the calculation

statement = the mathematical operation that compute the value of the outvar

Page 12: lab02

Through function file: function velocity = freefall (m,cd,tf) % freefall (m,cd,tf) compute the freefall velocity % input: % m = mass (kg) % cd = drag coefficient (kg/m) % t = time (s) % output: % velocity = downward velocity (m/s) % g = acceleration of gravity g = 9.81; velocity = ((g*m)/cd)*(1-exp((-cd/m)*tf)); Save the M-file as stated in the 1st line freefall.m Return to command window and type the saved M-file >> freefall(68.1,12.5,2) ans = …….. You can introduce different argument values

Page 13: lab02

Plotting To plot the previous problem in Matlab and

excel MATLAB: >>t=[0:2:20]’ >>length(t) ans = >>g=9.81; m=68.1; cd=12.5; >>v = ((g*m)/cd)*(1-exp((-cd/m)*t)) v = …..

Page 14: lab02

Plotting

>>plot(t,v)

>>title(‘Plot of v versus t’)

>>xlabel(‘values of t’)

>>ylabel(‘value of v’)

>>grid

>>plot(t,v,’o’)

>>plot(t,v,t,v,’x’)

Page 15: lab02
Page 16: lab02

Plotting

LIST THE DATA FOR X-AXIS AND Y-AXIS

Page 17: lab02

Problems

1. Solve problem of example 5.1 in the text book through plotting using MATLAB and Excel.

2. Problem 5.3

3. Problem 5.4