Documente Academic
Documente Profesional
Documente Cultură
Problem:
Develop a script file to compute velocity of free-falling bungee jumper
g = 9.81; m = 68.1; t = 12; cd = 0.25;
v = sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t)
Answer:
v = 50.6175
Rozette D. Reano
BS ECE 4-1
>>freefallvel(100,0.25,8)
Rozette D. Reano
BS ECE 4-1
>>help freefallvel
>>help incsearch
Rozette D. Reano
BS ECE 4-1
INPUT-OUTPUT
The input function
n = input(prompt string)
m = input (mass(kg):)
name = input (Enter yout name:, s)
Example:
function velocity = freefallvel(m,cd,t)
%computes the free-fall velocity
% of an object
g = 9.81;
velocity = sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t);
Example:
function velocity = freefallvel(m,cd,t)
%computes the free-fall velocity
% of an object
g = 9.81;
Rozette D. Reano
BS ECE 4-1
The fprintf function
fprintf (format,x, ...)
Example:
-> fprintf(the velocity is %8.4f m/s \n, velocity)
-> %8.4f = format code
\n = control code
%f = decimal format
%d = integer
%e = scientific
%g = more compact than %e and %f
control code:
\n = start new line
\t = tab
Rozette D. Reano
BS ECE 4-1
STRUCTURED PROGRAMMING
- Decisions
- Loops
1. Decisions
a. IF STRUCTURE
if condition
statements
end
b. ERROR FUNCTION
error (msg)
Rozette D. Reano
BS ECE 4-1
if...else STRUCTURE
if condition
statements
else
statements
end
if...else if STRUCTURE
if condition
statements
elseif condition
statements
elseif condition %WALANG SPACE YUNG elseif
statements
.
.
.
else
statement
end
Rozette D. Reano
BS ECE 4-1
Rozette D. Reano
BS ECE 4-1
2. LOOPS
a. FOR loops
Example:
function fout=factor (n)
x=1;
for i=1:n
x=x*i;
end
fout=x;
end
>>factor(5) % 5! (5 factorial) = 120
Rozette D. Reano
BS ECE 4-1
b. WHILE loop
while condition
statements
end
Example:
function fout=whiledemo()
x=8
while x>0
x=x-3;
disp(x)
end
>>whiledemo
Rozette D. Reano
BS ECE 4-1
feval FUNCTION
outvar=feval(funcname,arg1,arg2,...)
inline FUNCTION
Rozette D. Reano
BS ECE 4-1
EXERCISE:
Develop an M-file to determine the average value of a function over a range. Illustrate its use for
simple cubic equation.
f(x) = (0.125*(x.^3))-(1.125*(x.^2))+(2.75*x)+1
range: x = 0 to 6
ans = 2.5
Rozette D. Reano
BS ECE 4-1