0% found this document useful (0 votes)
11 views3 pages

MATLAB Functions for Drilling Mechanics

The document provides a tutorial on MATLAB functions, including creating functions for mathematical equations, calculating velocity and distance for falling objects, and using decision-making with the find function. It includes examples of if statements, for loops, and while loops, along with tests for implementing these concepts. Additionally, it outlines specific tasks such as computing series sums and plotting functions based on given conditions.

Uploaded by

yassine39573505
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

MATLAB Functions for Drilling Mechanics

The document provides a tutorial on MATLAB functions, including creating functions for mathematical equations, calculating velocity and distance for falling objects, and using decision-making with the find function. It includes examples of if statements, for loops, and while loops, along with tests for implementing these concepts. Additionally, it outlines specific tasks such as computing series sums and plotting functions based on given conditions.

Uploaded by

yassine39573505
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

KASDI MERBAH UNIVERSITY OF OUARGLA

Faculty – Department FHREEUS – Drilling and oilfields mechanics


Module Computer Science / 3rd Year Drilling
PW Tutorial N°02 Function, statement and loops

Procedure I
1) Create a MATLAB function of the following equation and save it as function m file called f1:
𝑧 = 3𝑥 + 6𝑦 2
Then, find z when x = 3 and y = 7;

function z = f1(x,y)
z = (3*x) + (6*(y^2));

>>x = 3; y = 7;
>>z = f1(x,y)

2) The following function, called drop, computes a falling object’s velocity and distance dropped. The input variables are
the acceleration g, the initial velocity, and the elapsed time t. Here we anticipate that t will be an array, so we use the
element-by-element operator (.^).

function [dist,vel] = drop(g,vO,t)


vel = g*t + vO;
dist = 0.5*g*t.^2 + vO*t;

The variable names used in the function definition may, but need not, be used when the function is called:
a = 32.2;
v0 = 10;
t = 5;
[F,V] = drop(a,v0,time)

The inputs and outputs may be arrays:


a = 32.2;
v0 = 10;
time = [0:1:5];
[f,V]=drop(a,v0,t)

3) The find function is very useful for creating decision-making programs, especially when combined with the relational
or logical operators.
Note that the find function returns the indices, not the values. In the following session, note the difference between the
result obtained by x(x<y) and the result obtained by find(x<y).
>>x = [6, 3, 9, 11];y = [14, 2, 9, 13];
>>values = x(x<y)
values =
6 11
>>indices = find(x<y)
indices =
1 4

Test.1
The height and speed of a projectile launched with a speed of 𝑣0 atan angle A to the horizontal are given by
ℎ 𝑡 = 𝑣0 𝑡 𝑠𝑖𝑛𝐴 − 0.5 𝑔 𝑡 2
𝑣 𝑡 = 𝑣02 − 2𝑣0 𝑔 𝑡 𝑠𝑖𝑛𝐴 + 𝑔2 𝑡 2
where g is the acceleration due to gravity. The projectile will strike the ground when h(t) =0, which gives the time to hit,
v0
t hit = 2 g
sin A.
Suppose that A = 40°, v0 = 20 m/s, g = 9.81 m/s 2 , the time step (sample) is t hit /100
Use the MATLAB relational and logical operators to find the times range when the height is no less than 6 m and the
speed is simultaneously no greater than 16 m/s. Use the plot to confirm the time.

The if Statement
The following if statement implements this procedure in MATLAB assuming x already has ascalar value

Course responsible Dr. HACHANA Oussama


if x >= 0
y = sqrt(x)
end

The next codes illustrate some working examples of using if statement with else and elseif.

if x >= 0
y = sqrt(x)
else
y = exp(x) - 1
end

x = [4,-9,25];
if x < 0
disp(‘Some of the elements of x are negative.’)
else
y = sqrt(x)
end

if x > 10
y = log(x)
elseif x >= 0
y = sqrt(x)
else
y = exp(x) - 1
end

for Loops
A simple example of a for loop is
a = 1;
for k = a:2:30
x = k^2 ;
disp('For k equal to')
disp(k)
disp('The variable x is')
disp(x(k))
end

Test 2
Write a script file to compute the sum of the first 15 terms in the series 5𝑘 2 – 2𝑘
k = 1, 2, 3, ... , 15.

Test 3
Write a script file to plot the function:
15 4𝑥 + 10 𝑥≥9
𝑦 = 10𝑥 + 10 0 ≤ 𝑥 < 9
10 𝑥<0
for −5 ≤ x ≤ 30, using a spacing (step) of dx = 35/300

It is permissible to use an if statement to “jump” out of the loop before the loop variable reaches its terminating value.
The break command, which terminates the loop but does not stop the entire program, can be used for this purpose. For
example:

for k = 1:10
x = 50 - k^2;
if x < 0
break
end
y = sqrt(x)
end

while Loops
The while loop is used when the looping process terminates because a specified condition is satisfied, and thus the number
of passes is not known in advance. A simple example of a while loop is:

2
x = 5;
while x < 25
end
disp(x)
x = 2*x - 1;
end

Test 4
Write a script file to determine how many terms are required for the sum of the series 5𝑘 2 – 2𝑘
k = 1, 2, 3, ... , to exceed 10,000. What is the sum for this many terms?

You might also like