lab02
TRANSCRIPT
ASSIGNMENT AND MATHERMATICAL OPERATION IN MATLAB
VARIABLE SCOPE
SCRIPTS AND FUNCTIOND (M-FILES)
PLOTTING IN MATLAB
Complex value;
>> x = 2+i*4
x =
2.0000+4.0000i
i 1 aspresentedis
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
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
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
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
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
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
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
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
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
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
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 = …..
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’)
Plotting
LIST THE DATA FOR X-AXIS AND Y-AXIS
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