Docsity

MATLAB Script for Solving Engineering Problems: Cantilever Beams and Channels, Cheat Sheet of Computer Science

Toggle sidebar
bg1
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.
Dr Zia R Tahir
Mechanical Engineering Department, UET Lahore
1/21
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download MATLAB Script for Solving Engineering Problems: Cantilever Beams and Channels and more Cheat Sheet Computer Science in PDF only on Docsity!

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^

8*3^2/

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

(4 - 2)/2/

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

1e

This is equivalent to 1 × 104.

8.324e This is equivalent to 8. 324 × 108.

2.1e-

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.

Variables

Define the following variables in the Matlab command window

x = 1.

y = -2.

z = 4.

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

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 + v

w-v

w3-w

v*s

v*w

v*w

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)

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

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 - A

A*C

A*A

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)

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^ a) 40 b) 400 c) 100 d) None of these

  2. 4 + 2*16/2^ 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

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

y = − wL^4 24 EI

X^4 − 4 X^3 + 6X^2

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) % % I n p u t s : % x : p o s i t i o n % L : l e n g t h o f t h e beam % w : u niform l o a d % I : moment o f i n e r t i a % E : Young modulus % % Output : % y : d i s p l a c e m e n t %

% Non−d i m e n s i o n a l p o s i t i o n X = x/L ; % D i s p l a c e m e n t 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 m^4 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 c l e a r a l l ; c l c ;

% 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. 2 5 ; % Length o f t h e beam [m] w = 4 0 0 0 ; % 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 ) ;

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

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

IR =

bh^3 (1)

where b is the width of the section and h is its height. For a beam with circular section, the moment of inertia is

IC =

πd^4 (2)

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.

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

y =

wx^2 6 EI (3a − x) if x ∈ [0, a]

wa^2 6 EI

(3x − a) if x ∈ [a, L]

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 ) % % I n p u t s : % x : p o s i t i o n % w : u niform l o a d % 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

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 ) ; e l s e 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 m^4 , 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 y 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 c l e a r a l l ; c l c ;

% 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. 2 5 ; % Length o f t h e beam [m] w = 4 0 0 0 ; % 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

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

% D e f i n e v e c t o r o f p o s i t i o n s x = l i n s p a c e ( 0 , L ,N ) ;

% D e f i n e v e c t o r s f o r d i s p l a c e m e n t s 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

S(x, n) = (x − 1) −

(x − 1)^2 +

(x − 1)^3 −

(x − 1)^4 +... + (−1)n−^1

n (x − 1)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 ) % % I n p u t s : % x : p o i n t % t o l : t o l e r a n c e % % Output :

Exercises

  1. For a rectangular channel, the Manning’s formula states that

Q =

n

A^5 u/^3 P (^) u^2 /^3

S^10 / 2 ,

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 S 0 = 0.002 and the constant n = 0.04. Then, it plots the discharge for the normal depth varying from yu = 0m to yu = 10m.

  1. The displacement of a punctually loaded beam supported at both ends x = 0 and x = L is given by

y =

wx(L − a) 6 LEI

(L^2 − x^2 − (L − a)^2 ) if x ∈ [0, a]

w(L − a) 6 LEI

( L

L − a (x − a)^3 +

L^2 − (L − a)^2

x − x^3

if x ∈ [a, L]

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 m^4 , 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.

  1. 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

w(x, y) =

π

n=1, 3 , 5 ,...

n

sin

nπx L

sinh(nπy/L) sinh(nπW/L)

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?

  1. 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

aiEi +

m=1, 3 , 5 ,...

bimEm = ci, i = 1, 3 , 5 ,...

where ai =

i

tanh αi + αi cosh^2 αi

bim = 8i

πm^3

i^2 m^2

ci =

π^3 i^4

αi cosh^2 αi

− tanh α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 ⎛ ⎜ ⎜ ⎝

a 1 + b 11 b 13 b 15 b 17 b 31 a 3 + b 33 b 35 b 37 b 51 b 53 a 5 + b 55 b 57 b 71 b 73 b 75 a 7 + b 77

E 1

E 3

E 5

E 7

c 1 c 3 c 5 c 7

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.

  1. 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 E 1 and ν 1 , respectively, and those of the outer cylinder are E 2 and ν 2 , respectively. The radial stress σrr hoop stress σθθ and radial displacement ur are given by

σrr,i(r) =

Ai r^2

  • Bi

σθθ,i(r) = − Ai r^2

  • Bi (3)

ur,i(r) = −

1 + νi rEi Ai +

1 − νi Ei rBi

where i = 1 refers to the inner cylinder and i = 2 to the outer cylinder.