Sunteți pe pagina 1din 12

NUMERICALS 01/13/17

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)

-> save as scriptdemo.m


-> command window
>>scriptdemo

Answer:

v = 50.6175

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);

-> save as freefallvel.m


-> command window
>>freefallvel(68.1,0.25,12)

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;

m = input ('Mass(kg): ');


cd = input ('Drag coefficient (kg/m): ');
t = input ('Time (s): ');

velocity = sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t);

The display function

Example:
function velocity = freefallvel(m,cd,t)
%computes the free-fall velocity
% of an object
g = 9.81;

m = input ('Mass(kg): ');


cd = input ('Drag coefficient (kg/m): ');
t = input ('Time (s): ');

disp (' ');


disp ('Velocity (m/s): ');
disp (sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t));

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)

-> if m>50, error(HEAVY LOAD), end

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

for index = start:step:finish


statements
end

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,...)

>>outvar=feval(cos,pi/6) %diretso sa command window

inline FUNCTION

funcname = inline (expression,var1,var2,...)

>>fx = inline (cos(x)*sin(x))


>>fx (pi/6)

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

S-ar putea să vă placă și