Vous êtes sur la page 1sur 21

Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

Problem Solving in Engineering with Matlab


Introduction to Matlab

Objectives
In this lab you will become familiar with the Matlab environment and practice the use of
variables, vectors and matrices.
The command window allows you to interact with the program by writing basic in-
structions. The workspace allows you to see which variables are currently defined.

Basic operations
The simplest use of Matlab is to perform operations in the command window. Try the
following commands and check your understanding of the hierarchy of operations. Check
the lecture notes when necesssary.

>> 2*5 + 4/2^2

>> 8*3^2/4

>> 5*(3 - 6/2)/3 + 7*2

>> (4 - 2)/2/4

For introducing a number multiplied by a power of 10 we can use the following short
notation
>> 1e4

This is equivalent to 1 × 104 .

>> 8.324e8

This is equivalent to 8.324 × 108 .

>> 2.1e-5

This is equivalent to 2.1 × 10−5 .


When an operation is performed in Matlab, the result is assigned to a default variable
called ans. Check the workspace every time you write one of the previous expressions in
the Matlab command window.
An expression that has been written is remembered by Matlab. Use the up and down
arrows (↑ and ↓) to recover previously written expressions.

1/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

Variables
Define the following variables in the Matlab command window

>> x = 1.549

>> y = -2.001

>> z = 4.578

Check that the variables appear in the workspace.


Perform the following operations using the variables

>> a = x + y

>> b = x*y^2 + z

Note that, if a command is finished with a semicolon, the answer is not shown in the
screen. Try the following:

>> c = x*y;

>> d = x + y + a;

The value of a variable can be checked by writing its name in the command window.
Try the following:

>> a

>> d

A variable can be deleted by using the command clear followed by the name of the
variable/s that you want to delete. Try the following command:

>> clear c d

Note that the variables c and d have disappeared from the workspace, so they are no
longer available to be used. This means, that the following command will produce an
error:

>> a + d

The command clear all deletes all the variables in the workspace.
By default, Matlab shows the result on the screen by using four decimal places. You
can change the look of the output by using the command format. Try the following
commands:

>> format long

>> b

There are other possibilities. Check all of them by typing on the command window

>> help format

2/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

Functions
Matlab has a huge number of built-in functions that can be used. Try the following
functions:

• abs: Absolute value.

>> abs(2.4)
>> abs(-1.894)
>> abs(a)
>> abs(b)

• sin, cos, tan: cosinus, sinus and tangent (angle in radians).

>> sin(pi/2)
>> cos(pi/4)
>> tan(3*pi/2)

• sind, cosd, tand: cosinus, sinus and tangent (angle in degrees).

>> sind(90)
>> cosd(45)
>> tand(270)

• sqrt: square root.

>> sqrt(2)

There are thousands of Matlab functions. It is important to remember some of the


functions that appear in the lecture notes and in the examples, but it is even more impor-
tant to develop an ability to find an appropriate function by using the command lookfor
or the the Matlab help. See the following two example:
To find the Matlab function required to compute a logarithm in base 2:

• Try the following instruction in the command window

>> lookfor logarithm

• Open the Matlab help and search for the word logarithm

The Matlab help is usually much better, as it shows examples of how to use a particular
function.

Vectors
Row vectors
Row vectors can be defined by using square brackets and introducing all the components
of the vector separated by commas or spaces. Try the following commands:

>> v = [2, 8, 4.1, 9, -1.21]

>> w = [-1 0 1.1]

3/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

It is also possible to define vectors using the command linspace or the syntax:
initial:step:final
Check the lecture notes and try the following examples:

>> v1 = linspace(-1,1,10)

>> v2 = linspace(3,5,5)

>> w1 = [0:0.5:2]

>> w2 = [0:6]

>> w3 = [10:-1:6]

Column vectors
Column vectors can be defined by using square brackets and introducing all the compo-
nents of the vector separated by semicolons. Try the following commands:

>> s = [4.3; -0.9; 9; -2; 5.81]

>> t = [0; 1; -2; 8]

A column vector can also be defined by transposing a row vector. Try the following
command:

>> s1 = transpose(v1)

Operations with vectors


The most common operations with vectors are the addition, subtraction and the scalar
product, see the lecture notes. A list of commands is given below, for each one try
to anticipate the result that will be output by Matlab and then, check your answer by
writing the instruction in the Matlab command prompt. Note that some instructions will
produce an error, it is important to understand why!

>> v + v2

>> w-v1

>> w3-w1

>> v*s

>> v*w

>> v*w3

Accessing information from vectors


To access the value of a component of a vector, write the name of the vector followed by
brackets containing the required component. Try the following commands:

>> v(2)

>> w3(4)

4/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

It is possible to access to several components at the same time by using the syntax
initial:step:final
Check the lecture notes and try the following commands:

>> v1(2:4)

>> s(5:-2:1)

It is common to require the maximum/minimum value of a vector and where (in which
component) is the maximum/minimum located. Try the following commands:

>> h = max(v)
Returns the maximum value of the vector v in a variable named h.

>> [h,p] = max(v)


Returns the maximum value of the vector v in a variable named h and the component
of v containing the maximum is given in p. You van check that v(p)=h.

The same applies to the command min.


The number of components of a vector can be computed using the command lenght.
Try the following:

>> s = [4.3; -0.9; 9; -2; 5.81]

>> length(s)

Plots
Matlab allows us to represent functions by using two vectors. The first vector must contain
the list of x coordinates and the second vector their images. Try the following:

>> x = [0:5:360];

>> y = sind(x);

>> plot(x,y)

The colour of the line can be changed adding an extra argument to the plot command.
Try the following:

>> plot(x,y,’k’)

The type of the line can be changed. Try the following:

>> plot(x,y,’r--’)

A symbol can be introduced at each point. Try the following:

>> plot(x,y,’g-s’)

A complete list of all the available options is given in the help. Try the following:

>> help plot

5/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

After writing help plot, click on the link doc plot to see more examples.
Note that every time we produce a plot, the previous one disappears. To maintain the
previous plot and produce a new plot in the same figure, we will use the command hold
on. Close the figure and try the following:

>> z = cosd(x);

>> plot(x,y,’k--’)

>> hold on

>> plot(x,z,’r-’)

When different plots are drawn in the same figure, it is necessary to introduce a legend.
Try the following:

>> legend(’sinus’,’cosinus’)

Further information can be added to the plot. For instance:

• A title
>> title(’Trigonometric functions’)

• A text in the x axis


>> xlabel(’x’)

• A text in the y axis


>> xlabel(’y’)

Matrices
A matrix can be defined by writing the coefficients between square brackets. A space or
a comma means a change of column and a semicolon a change of row. Try the following
instructions:

>> A = [1, 4, 5; 2 6 -1]

>> B = [3 4; 6 9; -1 0; 2 1]

>> C = [3 5 2; -1 0 -1]

>> D = [3 0 1; 0 12 9; 8 7 2]

Matrices can be defined by using existing vectors and/or matrices. Try the following:

>> x = [1, 5, 0]

>> y = [-1, 5, 2]

>> X = [x; y]

>> Y = [x; A; y]

Note that you will receive an error if the dimensions are not consistent. For instance:

>> Z = [x, A]

6/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

A matrix can also be defined by multiplying a row vector by a column vector. Try the
following:

>> x = [1, 5, 0]

>> y = [-1, 5, 2]

>> A = transpose(x)*y

There are some special matrices than can be built without writing all the components
one by one. The most useful functions are zeros, ones and eye. See the following
examples:

• >> A1 = zeros(3,4)
Builds a matrix with 3 rows and 4 columns filled in with zeros

• >> A2 = ones(6,2)
Builds a matrix with 6 rows and 2 columns filled in with zeros

• >> A3 = eye(5)
Builds an identity matrix of dimension 5. Note that this is equivalent to eye(5,5)

Operations with matrices


The most common operations with matrices are the addition, subtraction and product,
see the lecture notes. Try the following instructions. Note that some instructions will
produce an error, it is important that you understand why!

>> A + C

>> D - A4

>> A*C

>> A*A1

>> transpose(B)*transpose(A1)

It is possible to solve linear systems of equations by using the backslash operator. Try
the following:

>> r = [1; 6; -1]

>> sol = D\r This is the solution of the system of equations with matrix D and
right hand side r.
You can check that D*sol=r.

Accessing information from matrices


To access to the value of a component of a matrix, write the name of the matrix followed
by brackets that contains the required component. A component is specified by a row and
a column separated by a comma. Try the following commands:

>> A(1,3)

>> B(3,2)

7/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

It is possible to access to several components at the same time by using the syntax
initial:step:final for both rows and columns. Check the lecture notes and try the
following commands:

>> B(1:2,1)

>> B(1:2:3,2)

>> B(2,1:2)

>> B(:,1)

>> D([1,3],1)

>> D([1,3],:)

>> D([1,3],[1,3])

It is common to require the maximum/minimum value of a matrix and where (in which
component) is the maximum/minimum located. Try the following commands:

>> A = [1, 4, 5; 2 6 -1]

>> max(A) Returns a vector with the maximum value of each row of A.

>> [h,p] = max(A) Returns the maximum value of each row of A in a variable
named h and the rows of A containing the maximums is given in p.

>> max(max(A)) Returns the maximum value of A.

The same applies to the command min.


The dimension of a matrix can be computed using the command size. Try the fol-
lowing:

>> C = [3 5 2; -1 0 -1]

>> size(C)

Review questions
Select the answer that you would obtain in Matlab if the following instructions are intro-
duced in the command window.
Note. Some of these questions appeared on past examinations. No calculator or
computers were allowed in the exam. Therefore, answer the questions first and check
your answers later by writing the expression in the Matlab command window.

1. >> 4*5^2
a) 40 b) 400 c)100 d) None of these

2. >> 4 + 2*16/2^4
a) 65540 b) 8 c)6 d) None of these

3. >> 5*(3 - 6/2 + 1)


a) 5 b) -2.5 c)13 d) None of these

8/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

4. >> (8 - 4)/2/4
a) 8 b) 4 c)0.5 d) None of these

5. >> sind(exp(0) + log10(1) + 3^2*5/0.5-1)


a) 0 b) 1 c)-1 d) None of these

6. >> v = [-2; 2; 5];


>> w = transpose(v)*v(2)
a) w=[-4 4 10] b) w=[4; -4; -10] c)w=[-4; 4; 10] d) None of these

7. >> v = [1, 7];


>> w = [5, -2];
>> x = [v; w]
a) x = [1, 7, 5, -2] b) x = [1, 7; 5, -2] c)x = [1, 5; 7, -2] d) None of these

8. >> v = linspace(-5,5,11);
>> w = v(1:3:10)
a) w = [-5, -2, 1, 4] b) w = [1, 4, 7, 10]
c) w = [1; 4; 7; 10]; d) None of these
9. >> v = [0, 2];
>> w = v*2 - 1;
>> x = transpose(v)*w
a) x = [0, 0; -2 4] b) x = [0, 0; -2 8] c)Error d) None of these

10. >> A = [1, 2; 0, 5];


>> B = [1 2; 3 4];
>> C = A*B
a) C = [1 4; 0 20] b) C = [7 10; 15 20]
c) Error d) None of these
11. >> v = [-2; 2; 5; 7; -12; 9];
>> x = max(v)
a) 12 b) 9 c)-12 d) None of these

12. >> v = [-2; 2; 5; 7; -12; 9];


>> [x,y] = min(v)
a) x=-12 y=5 b) x=5 y=-12
c) x=12 y=5 d) x=5 y=12

13. >> v = [-2; 2; 5; 7; -12; 9];


>> w = transpose(v(2:2:4))
a) w = [2, 7] b) w = [2; 5; 7] c)w = [2, 5, 7] d) None of these

14. >> A = [-2, 2, 5; 7, -12, 9];


>> v = min(A)
>>
a) v = [-2; -12; 5] b) v = [-2, -12] c)v = [-2, -12, 5] d) v = [-2; -12]

15. >> v = [0:0.5:4];


>> w = v([7, 1, 4])
a) w = [3.5, 0, 1.5] b) w = [0, 1.5, 3]
c) w = [3, 0, 1.5] d) None of these

9/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

Problem Solving in Engineering with Matlab


Introduction to Programming 1

Objectives
In this lab we will practise the creation of Matlab scripts and functions.
For each exercise it is mandatory to create a sketch following the recommendations
given in the lecture notes before trying to perform the implementation in Matlab.

Worked example
The displacement of a uniformly loaded cantilever beam attached to a wall at x = 0
and free at x = L is given by

wL4  4 
y=− X − 4X 3 + 6X 2 ,
24EI
where w is the load per unit width of the beam, L is the length of the beam, E is
the Young modulus, I is the moment of inertia and X = x/L.

a) Write a Matlab function that, given x, L, w, I and E, returns the displacement y.


Sketch

• Name: cantileverUniform.m
• Inputs: x, L, w, I, E
• Output: y
• Matlab header: function y=cantileverUniform(x,L,w,I,E)

Matlab implementation
f u n c t i o n y=c a n t i l e v e r U n i f o r m ( x , L , w, I , E)
%
% Inputs :
% x : position
% L : l e n g t h o f t h e beam
% w: uniform load
% I : moment o f i n e r t i a
% E : Young modulus
%
% Output :
% y : displacement
%

10/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

% Non−d i m e n s i o n a l p o s i t i o n
X = x/L ;
% Displacement
y = −w∗Lˆ 4 ∗ (Xˆ4 − 4∗Xˆ3 + 6∗Xˆ 2 ) / ( 2 4 ∗E∗ I ) ;

b) Write a Matlab script that defines the load w = 4000N, the moment of inertia
I = 863.65 × 10−8 m4 and the length of the beam L = 2.25m. Then, it computes the
displacement at the mid point of the beam for the three materials listed in Table 1
and writes three messages on the screen with the results.

Material E
Steel 2.0 × 1011 Pa
Aluminium 6.9 × 1010 Pa
Oak 1.1 × 1010 Pa

Table 1: Young modulus for different materials.

Sketch

• Name: mainCantilever.m
• Data: L, w, I, Esteel, Ealum, Eoak
• Output: Messages on the screen

Matlab implementation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% m a i n C a n t i l e v e r .m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% C l e a r memory and t h e s c r e e n
clear all ; clc ;

% Data −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
E s t e e l = 2 . 0 e11 ; % Young modulues S t e e l [ Pa ]
Ealum = 6 . 9 e10 ; % Young modulues Aluminium [ Pa ]
Eoak = 1 . 1 e10 ; % Young modulues Oak [ Pa ]
I = 8 6 3 . 6 5 e −8; % Moment o f i n e r t i a [mˆ 4 ]
L = 2.25; % Length o f t h e beam [m]
w = 4000; % Uniform l o a d [N]

% Computations −−−−−−−−−−−−−−−−−−−−−−−−−−−−
% Mid p o i n t
x = L/2;

% D i s p l a c e m e n t f o r each m a t e r i a l
y S t e e l = c a n t i l e v e r U n i f o r m ( x , L , w, I , E s t e e l ) ;
yAlum = c a n t i l e v e r U n i f o r m ( x , L , w, I , Ealum ) ;
yOak = c a n t i l e v e r U n i f o r m ( x , L , w, I , Eoak ) ;

% Output on t h e s c r e e n −−−−−−−−−−−−−−−−−−−−
d i s p ( ’ The d i s p l a c e m e n t a t t h e mid p o i n t i s : ’ ) ;
f p r i n t f ( ’ %6.4 f m f o r s t e e l \n ’ , −y S t e e l ) ;
f p r i n t f ( ’ %6.4 f m f o r aluminium \n ’ , −yAlum ) ;
f p r i n t f ( ’ %6.4 f m f o r oak \n ’ , −yOak ) ;

11/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

Exercises
1. For a rectangular channel, the Manning’s formula states that
5/3
1 Au 1/2
Q= 2/3
S0 ,
n Pu

where Q is the discharge, Au is the cross sectional area of the channel and Pu is the
wetted perimeter, that are expressed in terms of the breath B and the normal depth
yu according to
Au = Byu , Pu = B + 2yu

a) Write a Matlab function that, given the breath, the normal depth, the channel
slope and the constant n, outputs the discharge.
b) Write a Matlab script that defines the breath B = 50m, the channel slope
S0 = 0.002 and the constant n = 0.04. Then, it computes the discharge for two
values of the normal depth, namely yu = 5m and yu = 10m, and writes two
messages on the screen with the results.

2. Wind tunnel experiments for a particular aerofoil have resulted in the following
formulas for the lift and drag coefficients respectively

CL = 4.47 × 10−5 α3 + 1.15 × 10−3 α2 + 6.66 × 10−2 α + 1.02 × 10−1

CD = 5.75 × 10−6 α3 + 5.09 × 10−4 α2 + 1.08 × 10−4 α + 1.25 × 10−2


where the angle α is expressed in degrees.

a) Write a Matlab function that, given the angle, outputs the lift coefficient.
b) Write a Matlab function that, given the angle, outputs the drag coefficient.
c) Write a Matlab script that defines the angle, α = 2.21 degrees. Then, it
computes the lift and the drag coefficients and writes two messages on the
screen with the results.

3. A cable of length Lc supports a beam of length Lb so that it is horizontal when the


weight W is attached at the beam end, see Figure 1. The principles of statics can
be used to show that the tension force T in the cable is given by

LLW
T = b c
D L2b − D2

where D is the distance of the cable attachment point to the beam pivot.

a) Write a Matlab function that, given the distance D, the cable length Lc , the
beam length Lb and the weight W , outputs the tension T .
b) Write a Matlab script that defines the cable length Lc = 5m and the beam
length Lb = 3m. Then, it computes the tension at a point with D = Lb /4
for two different loads, namely W = 400N and W = 600N. The program must
write two messages on the screen with the results.

12/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

Figure 1: Cable supporting a beam with an attached weight.

4. The moment of inertia of a beam with rectangular section is


1 3
IR = bh (1)
12
where b is the width of the section and h is its height. For a beam with circular
section, the moment of inertia is
1 4
IC = πd (2)
64
where d denotes the diameter.

a) Write a Matlab function that, given the width b and the height h of a rectangular
section and the diameter d of a circular section, outputs both the moment of
inertia of a beam with rectangular section IR and with circular section IC .
b) Write a Matlab script that defines the width b = 10mm, the height h = 20mm
and the diameter d = 15mm. Then, it computes the moment of inertia of a
beam with rectangular section IR and with circular section IC . The program
must write two messages on the screen with the results.

13/21
Dr Zia R Tahir Mechanical Engineering Department,UET Lahore

Problem Solving in Engineering with Matlab


Introduction to Programming 2

Objectives
In this lab we will practise the control flow (conditionals) and loops.
For each exercise it is mandatory to create a sketch following the recommendations
given in the lecture notes before trying to perform the implementation in Matlab.

Worked example 1 - Control flow (Conditionals)


The displacement of a punctually loaded cantilever beam attached to a wall at x = 0
and free at x = L is given by


⎪ wx2
⎨ 6EI (3a − x) if x ∈ [0, a]
⎪ −
y= (1)

⎪ 2
⎪ wa
⎩ − (3x − a) if x ∈ [a, L]
6EI
where w is the punctual load, L is the length of the beam, E is the Young modulus,
I is the moment of inertia and a is the point where the punctual load is applied.

a) Write a Matlab function that, given x, L, w, I, E and a, returns the displacement


y.
Sketch

• Name: cantileverPunctual.m
• Inputs: x, w, I, E, a
• Output: y
• Matlab header: function y=cantileverPunctual(x,L,w,I,E,a)

Matlab implementation
f u n c t i o n y=c a n t i l e v e r P u n c t u a l ( x , w, I , E , a )
%
% Inputs :
% x : position
% w: uniform load
% I : moment o f i n e r t i a
% E : Young modulus
% a : P o i n t where t h e l o a d i s a p p l i e d

14/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

%
% Output :
% y : displacement
%

i f x<=a
y = −w∗x ˆ 2 ∗ ( 3 ∗ a−x ) / ( 6 ∗E∗ I ) ;
else
y = −w∗ a ˆ 2 ∗ ( 3 ∗ x−a ) / ( 6 ∗E∗ I ) ;
end

b) Write a Matlab script that defines the load w = 4000N, the moment of inertia
I = 863.65 × 10−8 m4 , the length of the beam L = 2.25m and the Young modulus
E = 2.0 × 1011 Pa. Then, it computes the displacement at the mid point of the
beam for two different positions of the applied load, namely a = L/3 and a = 2L/3.
The program compares the two values and prints on the screen a message with the
maximum of both.
Sketch

• Name: mainCantileverPunctual.m
• Data: L, w, I, E
• Computation:
– Compute x and two different values of a, say a1 and a2, as a function of L.
– Calls function mainCantileverPunctual.m twice
– Compares the two displacements
• Output: Messages on the screen (the message is decided using a conditional)

Matlab implementation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% m a i n C a n t i l e v e r P u n c t u a l .m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% C l e a r memory and t h e s c r e e n
clear all ; clc ;

% Data −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
E = 2 . 0 e11 ; % Young modulues S t e e l [ Pa ]
I = 8 6 3 . 6 5 e −8; % Moment o f i n e r t i a [mˆ 4 ]
L = 2.25; % Length o f t h e beam [m]
w = 4000; % Uniform l o a d [N]

% Computations −−−−−−−−−−−−−−−−−−−−−−−−−−−−
% Mid p o i n t
x = L/2;

% Two d i f f e r e n t p o s i t i o n s o f t h e a p p l i e d l o a d
a1 = L / 3 ;
a2 = 2∗ a1 ;

% D i s p l a c e m e n t f o r each p o s i t i o n o f t h e a p p l i e d l o a d
y1 = c a n t i l e v e r P u n c t u a l ( x , w, I , E , a1 ) ;
y2 = c a n t i l e v e r P u n c t u a l ( x , w, I , E , a2 ) ;

% Output on t h e s c r e e n −−−−−−−−−−−−−−−−−−−−
i f y1>y2
f p r i n t f ( ’Maximum y f o r a=%6.4 f m. I t i s %6.4 f m. \ n ’ , a1 , −y1 ) ;
e l s e i f y1<y2

15/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

f p r i n t f ( ’Maximum y f o r a=%6.4 f m. I t i s %6.4 f m. \ n ’ , a2 , −y2 ) ;


else
f p r i n t f ( ’ I d e n t i c a l y f o r both a=%6.4 f m and a=%6.4 f . \ n ’ , a1 , a2 ) ;
f p r i n t f ( ’ I t i s %6.4 f m. \ n ’ , −y1 ) ;
end

Worked example 2 - Loops


The displacement of a punctually loaded cantilever beam attached to a wall at x = 0
and free at x = L is given in Equation (1) that appears in the previous Worked
Example.

a) Create a Matlab script that defines the load w = 4000N, the moment of inertia
I = 863.65 × 10−8 m4 , the length of the beam L = 2.25m and the Young modulus
E = 2.0 × 1011 Pa. Then, it plots the shape of the undeformed and two deformed
configurations, with a = L/3 and a = 2L/3.
Sketch

• Name: plotCantileverPunctual.m
• Data: L, w, I, E
• User parameter: N, number of points for the plot
• Computation:
– Define a vector x with N points between 0 and L.
– Define three vectors filled in with zeros: y0, y1 and y2. The vector y0
will represent the undeformed configuration, y1 the deformed configuration
with a = L/3 and y2 the deformed configuration with a = 2L/3.
– Creates a loop to fill in y1 and y2 by calling the function cantileverPunctual.m
that was created in the previous Worked Example.
• Output: Plot the three configurations. Remember to:
– Use different colours for each configuration.
– Add labels to the x and y axis.
– Add a title.
– Add a legend.

Matlab implementation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% p l o t C a n t i l e v e r P u n c t u a l .m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% C l e a r memory and t h e s c r e e n
clear all ; clc ;

% Data −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
E = 2 . 0 e11 ; % Young modulues S t e e l [ Pa ]
I = 8 6 3 . 6 5 e −8; % Moment o f i n e r t i a [mˆ 4 ]
L = 2.25; % Length o f t h e beam [m]
w = 4000; % Uniform l o a d [N]

% User parameter
N = 1 0 0 ; % number o f p o i n t s f o r t h e p l o t

16/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

% Computations −−−−−−−−−−−−−−−−−−−−−−−−−−−−
% Two d i f f e r e n t p o s i t i o n s o f t h e a p p l i e d l o a d
a1 = L / 3 ;
a2 = 2∗ a1 ;

% Define vector of p o s i t i o n s
x = l i n s p a c e ( 0 , L ,N ) ;

% Define vectors f o r displacements


y0 = z e r o s ( 1 ,N ) ;
y1 = z e r o s ( 1 ,N ) ;
y2 = z e r o s ( 1 ,N ) ;

% Compute d i s p l a c e m e n t s u s i n g a l o o p
f o r i =1:N
y1 ( i ) = c a n t i l e v e r P u n c t u a l ( x ( i ) ,w, I , E , a1 ) ;
y2 ( i ) = c a n t i l e v e r P u n c t u a l ( x ( i ) ,w, I , E , a2 ) ;
end

% R e p r e s e n t t h e t h r e e c o n f i g u r a t i o n s −−−−−−−−
% Undeformed
p l o t ( x , y0 , ’ k ’ , ’ LineWidth ’ , 2 )
h o l d on
p l o t ( x , y1 , ’ b ’ , ’ LineWidth ’ , 2 )
p l o t ( x , y2 , ’ r ’ , ’ LineWidth ’ , 2 )
% Add a l e g e n d
l e g e n d ( ’ Undeformed ’ , ’ Deformed a=L/3 ’ , ’ Deformed a=2L/3 ’ )
x l a b e l ( ’ P o s i t i o n [m] ’ )
y l a b e l ( ’ D e f l e c t i o n [m] ’ )
t i t l e ( ’ C a n t i l e v e r beam p u n c t u a l l o a d ’ )

Worked example 3 - Loops with conditionals


Consider the following Taylor series expansion
1 1 1 1
S(x, n) = (x − 1) − (x − 1)2 + (x − 1)3 − (x − 1)4 + . . . + (−1)n−1 (x − 1)n .
2 3 4 n

Write a Matlab function that, given the value of x and a tolerance tol, returns the
number of terms k such that the absolute value of the k-th term of the series is less
or equal than tol and the value of S(x, k).
Sketch

• Name: taylorTerm.m
• Inputs: x, tol
• Outputs: S, k
• Matlab header: function [S,k]=taylorTerm(x,tolerance)

Matlab implementation
f u n c t i o n [ S , k ] = taylorTerm ( x , t o l )
%
% Inputs :
% x : point
% tol : tolerance
%
% Output :

17/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

% S:
% k : i n d e x o f t h e s e r i e s term l e s s than t o l
%

% D e f i n e a maximum number o f terms b i g enough


n = 1000;

% I n i t i a l i z e t h e sum
S = 0;

% Loop u n t i l a term s m a l l e r than t h e t o l e r a n c e i s found


f o r i =1:n
% i −th term o f t h e s e r i e s
iTerm = ( −1)ˆ( i −1)∗( x−1)ˆ i / i ;
% Accumulate t h e v a l u e o f t h e sum
S = S + iTerm ;
% Check i f t h e i −th term ( i n a b s o l u t e v a l u e i s l e s s than
% the s p e c i f i e d t o l e r a n c e
i f ( abs ( iTerm)< t o l )
% I f so , s t o r e t h e term number i n ”k” and s t o p t h e l o o p
% u s i n g ” b re ak ”
k = i;
break ;
end
end

18/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

Exercises
1. For a rectangular channel, the Manning’s formula states that
5/3
1 Au 1/2
Q= S ,
n Pu2/3 0

where Q is the discharge, Au is the cross sectional area of the channel and Pu is the
wetted perimeter, that are expressed in terms of the breath B and the normal depth
yu according to
Au = Byu , Pu = B + 2yu

a) Write a Matlab function that, given the breath, the normal depth, the channel
slope and the constant n, outputs the discharge.
b) Write a Matlab script that defines the breath B = 50m, the channel slope
S0 = 0.002 and the constant n = 0.04. Then, it plots the discharge for the
normal depth varying from yu = 0m to yu = 10m.

2. The displacement of a punctually loaded beam supported at both ends x = 0 and


x = L is given by

⎪ wx(L − a) 2 2 2

⎨ − 6LEI (L − x − (L − a) )
⎪ if x ∈ [0, a]
y=

⎪  L

⎪ w(L − a)
⎩ − (x − a)3 + L2 − (L − a)2 x − x3 if x ∈ [a, L]
6LEI L−a
where w is the punctual load, L is the length of the beam, E is the Young modulus,
I is the moment of inertia and a is the point where the punctual load is applied.

a) Write a Matlab function that, given x, L, w, I, E and a, returns the displacement


y.

b) Create a Matlab script that defines the load w = 4000N, the moment of inertia
I = 863.65 × 10−8 m4 , the length of the beam L = 2.25m and the Young modulus
E = 2.0 × 1011 Pa. Then, it plots the shape of the undeformed beam and two
deformed configurations, with a = L/3 and a = 2L/3.

3. For controlling the properties of some materials, it is useful to know the temperature
distribution of a rectangular metal plate of dimensions W × L. The temperature
depends on the function

2 2 nπx sinh(nπy/L)
w(x, y) = sin
π n L sinh(nπW/L)
n=1,3,5,...

Assuming that W = 4m and L = 3m:

a) Write a Matlab function that, given an integer number k and the point of the
plate where the temperature is required (x, y), return the value of w(x, y) where
the sum goes up to n ≤ k.
b) Write a Matlab script that defines a point x = 1 and y = 1 and plots the
temperature at this point as a function of the number of terms in the summation
k ∈ [0, 20]. What do you notice as k increases?

19/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

4. The static deflection of a square plate clamped on all four sides and subjected to
uniform load can be expressed in terms of the following constants Em that are
obtained from the truncation of the following infinite set of equations

a i Ei + bim Em = ci , i = 1, 3, 5, . . .
m=1,3,5,...

where  
1 αi
ai = tanh αi +
i cosh2 αi
 2 −1
i2
bim = 8i πm3 1+ 2
m
 
4 αi
ci = 3 4 − tanh αi
π i cosh2 αi
and αi = iπ/2.

a) Write a Matlab functions that, given i, returns the coefficient ai .


b) Write a Matlab functions that, given i and m, returns the coefficient bim .
c) Write a Matlab functions that, given i, returns the coefficient ci .

Taking the first four equations, that is i = 1, 3, 5, 7, and truncating the infinite series
also to the fourth term, that is m = 1, 3, 5, 7, we obtain the following system of
equations in matrix form
⎛ ⎞⎛ ⎞ ⎛ ⎞
a1 + b11 b13 b15 b17 E1 c1
⎜ b31 a + b b b ⎟ ⎜ ⎟ ⎜ ⎟
⎜ 3 33 35 37 ⎟ ⎜ 3 ⎟ = ⎜c 3 ⎟
E
(2)
⎝ b51 b53 a5 + b55 b57 ⎠ ⎝ E5 ⎠ ⎝ c5 ⎠
b71 b73 b75 a7 + b77 E7 c7

d) Write a Matlab script that, using the functions defined in a) and b), builds and
solves the lienar system of equations given in Equation (2). The program must
output the solution of the linear system on the screen.

5. Consider two cylinders of two different materials where one cylinder just fits inside
the other. The inner radius of the inner cylinder is a, and its outer radius is b. The
inner radius of the outer cylinder is also b, and its outer radius is c. The Young’s
modulus and the Possion ratio of the inner cylinder are E1 and ν1 , respectively, and
those of the outer cylinder are E2 and ν2 , respectively. The radial stress σrr hoop
stress σθθ and radial displacement ur are given by

Ai
σrr,i (r) = + Bi
r2
Ai
σθθ,i (r) = − + Bi (3)
r2
1 + νi 1 − νi
ur,i (r) = − Ai + rBi
rEi Ei
where i = 1 refers to the inner cylinder and i = 2 to the outer cylinder.

20/21
Dr Zia R Tahir Mechanical Engineering Department, UET Lahore

If the outer surface of the outer cylinder is subjected to a compressive radial dis-
placement U0 and the inner surface of the inner cylinder has no radial stress, then
the following conditions are used to determine Ai and Bi for i = 1, 2.

σrr,1 (a) = 0, σrr,1 (b) = σrr,2 (b)

ur,1 (b) = ur,2 (b), ur,2 (c) = −U0

Introducing these conditions into the Equations (3), the following system of equations
in matrix form is obtained:
⎛ ⎞⎛ ⎞ ⎛ ⎞
1 + a2 0 0 0 A1 0
⎜ 1 b2 −1 −b2 ⎟ ⎜B 1 ⎟ ⎜ 0 ⎟
⎜ ⎟⎜ ⎟ ⎜ ⎟
⎝−(1 + ν1 ) (1 − ν1 )b2 (1 + ν2 )E1 /E2 −(1 − ν2 )b2 E1 /E2 ⎠ ⎝A2 ⎠ = ⎝ 0 ⎠
0 0 −(1 − ν2 ) (1 − ν2 )c2 B2 −U0 E2 c

a) Write a Matlab function that, given a, b, c, E1 , ν1 , E2 , ν2 and U0 builds and


solve the linear system and writes on the screen the values of A1 , A2 , B1 and
B2 .
b) Write a Matlab function that, given r, Ai , Bi , Ei and νi , returns the radial
stress, the hoop stress and the radial displacement.
c) Write a Matlab script that, given a range of values of r and using the functions
defined in a) and b), plots in three separate figures the radial stress, the hoop
stress and the radial displacement for both the inner and the outer cylinders,
as a function of r.

21/21

Vous aimerez peut-être aussi