# Introduction to MATLAB_1_ by malj

VIEWS: 27 PAGES: 19

• pg 1
```									Introduction to MATLAB II

1
M-Files: Scripts and Functions
o   You can create and save code in text files using MATLAB
Editor/Debugger or other text editors (called m-files
since the ending must be .m)
o    M-file is an ASCII text file similar to FORTRAN or C
source codes ( computer programs)
o   A script can be executed by typing the file name, or
using the “run” command

Difference between scripts and functions
Scripts share variables with the main workspace
Functions do not

2
Script File – Bungee Jumper
o “Thetime rate change of momentum of a body is
equal to the resulting force acting on it.”

o Formulated         as   F = m.a
F = net force acting on the body
m = mass of the object (kg)
a = its acceleration (m/s2)

o Somecomplex models may require more sophisticated
mathematical techniques than simple algebra
n   Example, modeling of a falling parachutist:

FU = Force due to air resistance = -cv            (c = drag
coefficient)
FD = Force due to gravity =         mg
3
• This is a first order ordinary differential equation.
We would like to solve for v (velocity).

• It can not be solved using algebraic manipulation

• Analytical Solution:
If the parachutist is initially at rest (v=0 at t=0),
using calculus dv/dt can be solved to give the result:

Independent variable
Dependent variable

4
Forcing function         Parameters
Exact (Analytic) Solution
If v(t) could not be solved analytically, then
we need to use a numerical method to solve it

g = 9.8 m/s2 c =12.5 kg/s
m = 68.1 kg

t (sec.) V
(m/s)
0           0
2           16.40
4           27.77
8           41.10
10          44.87
12          47.49
∞           53.39

5
Numerical Method

This equation can be rearranged to yield

t (sec.)   V
(m/s)
0          0
∆t = 2 sec
2          19.60
4          32.00
8          44.82
10         47.97          To minimize the error, use a smaller step size, ∆t
12         49.96          No problem, if you use a computer!
6
∞          53.39
Analytical                  vs.                Numerical solution
m=68.1 kg c=12.5 kg/s
g=9.8 m/s               ∆t = 2 sec               ∆t = 0.5 sec         ∆t = 0.01 sec

t         V            t (sec.)     V           t (sec.)   V         t (sec.)   V
(sec.)    (m/s)                     (m/s)                  (m/s)                (m/s)
0         0            0            0           0          0         0          0

2         16.40        2            19.60       2          17.06     2          16.41

4         27.77        4            32.00       4          28.67     4          27.83

8         41.10        8            44.82       8          41.94     8          41.13

10        44.87        10           47.97       10         45.60     10         44.90

12        47.49        12           49.96       12         48.09     12         47.51

∞         53.39        ∞            53.39       ∞          53.39     ∞          53.39

CONCLUSION: If you want to minimize
the error, use a smaller step size, ∆t

7
Script File – Bungee Jumper
o   Develop a script       t=0:2:20; %Time(s)
g=9.81; %Acceleration due to
file to compute
gravity(m/s^2)
and plot the           m=68.1; %Mass (kg)
velocity v of the      cd=12.5; %Drag coefficient (kg/s)
bungee jumper as       v=(g*m/cd)*(1-(exp(-(cd/m)*t)));
a function of time     figure; %To open a figure window
t. Set t = 0 to 20 s   plot(t, v)
in steps of 2.         title('Plot of v versus t')
xlabel('Values of t')
ylabel('Values of v')

8
Interactive Input
o   The input command:
n = input('promptstring’): Prompt the user to enter a value from
the keyboard
n = input('promptstring','s'): Prompt the user to enter a string
from the keyboard
n    Examples:
>> m = input(‘Mass (kg): ‘); ¿
>> myname = input(‘My name is: ‘,’s’); ¿

o   The display command:
display(value): value = a numeric value, a variable, or a
string enclosed in ‘ ‘.
n  Examples:
>> display(m)              >> display(myname)

9
Script File – Bungee Jumper
o   In the bungee-jumper example, create a script
file that prompts the user to enter the values for
the mass, m, in kg, the drag coefficient, cd, in
kg/m, and the time, t, in s. Also, compute and
display the value for the velocity, v, at the
specified time instant.
n   Save the file as bungee-jumper.m.
n   Run the file.

10
Script File – Bungee Jumper
g=9.81; %Acceleration due to gravity(m/s^2)
m=input(‘Mass (kg): ‘);
cd=input(‘Drag coefficient (kg/s): ‘);
t=input (‘Time (s): ‘);
v=(g*m/cd)*(1-(exp(-(cd/m)*t)));
disp(‘ ‘)
disp(‘Velocity (m/s):’)
disp(v)

11
Function File – Bungee Jumper
o       Create a function file freefallvel.m
function velocity = freefallvel(m,cd,t)
% freefallvel(m,cd,t) computes the free-fall velocity (m/s)
%                      of an object with second-order drag
% input:
%     m = mass (kg)
%     cd = second-order drag coefficient (kg/s)
%     t = time (sec)
% output:
%     velocity = downward velocity (m/s)
g = 9.81;        % acceleration of gravity
velocity = (g*m/cd)*(1-(exp(-(cd/m)*t)));
12
Function File – Bungee Jumper
o   Run freefallvel.m
o   Input: mass (m), drag coef. (cd), and time (t)

>> vel1 = freefallvel(100,12.5,8)
vel1 =
53.1878
>> vel2 = freefallvel(100, 12.5,20)
vel2 =
62.4038
>> vel3 = freefallvel(70, 12.5,20)
vel3 =
52.3512

13
Function File
o    To invoke the help comments
o    Type help freefallvel
>> help freefallvel
freefallvel(m,cd,t) computes the free-fall velocity (m/s)
of an object with second-order drag
input:
m = mass (kg)
cd = second-order drag coefficient (kg/s)
t = time (sec)
output:
velocity = downward velocity (m/s)

14
MATLAB Output
o   Matlab automatically prints the results of
any calculation (unless suppressed by
semicolon ;)
o   Use “disp” to print out text to screen
n   disp (x.*y)
n   disp (´Temperature =´)
o   sprintf - display combination
o   Make a string to print to the screen
o   output = sprintf(‘Pi is equal to %f ’, pi)

15
Formatted Output
o   fprintf (format-string, var, ….)
o   %[flags] [width] [.precision] type
o   Examples of “type” fields
o   %d display in integer format
o   %e display in lowercase exponential notation
o   %E display in uppercase exponential notation
o   %f display in fixed point or decimal notation
o   %g display using %e or %f, depending on which is
shorter
o   %% display “%”

16
Numeric Display Format

x = [5 -2 3 0 1 -2]; format +
x = [+ - + + -]        (+/- sign only)
17
fprintf( ) of Scalar
temp = 98.6;
fprintf(‘The temperature is %8.1f degrees F.\n’, temp);

The temperature is    98.6 degrees F.

fprintf(‘The temperature is %08.2f degrees F.\n’, temp);
The temperature is 00098.60 degrees F.

fprintf(‘The temperature is %8.3e degrees F.\n’, temp);
The temperature is 9.860e+001 degrees F.
18
fprintf( ) of Matrices
Score = [1 2 3 4; 75 88 102 93; 99 84 95 105]
fprintf(‘Game %1.0f score: Houston: %3.0f Dallas: %3.0f \n’,Score)

Game 1 score: Houston: 75 Dallas: 99
Game 2 score: Houston: 88 Dallas: 84
Game 3 score: Houston: 102 Dallas: 95
Game 4 score: Houston: 93 Dallas: 105

fprintf control codes
\n Start new line           \t   Tab
19

```
To top