MCD4140 Computing for Engineers Self Study Exercise 5
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
MCD4140 Computing for Engineers
Self Study Exercise 5
Note: The debugging technique discussed in Lecture 10 can be very useful while solving the problems below.
Task 1
Create an anonymous function for 10e−2x and use it to plot the function over the range 0 ≤ x ≤ 2.
Task 2
Trace the M-file below and show the output of running this M-file. Assume that the user input is 7.
M-file:
% My_M_File z = input(‘Type an Integer . . ’); x = MyFun(MyFun(z)); disp(‘The value of x is .. ’); disp(x); |
Function M-File:
function [z] = MyFun(x); z = x - 2; |
Task 3
f (x, y) = 2 sin(x) + 3cos(y)
Use anonymous function to evaluate the mathematical function given above for x = pi/3 and y = pi/4?
Task 4
Factorials are used for many different applications, such as Taylor series, combinatorial probability, permutations and modelling rate of growth. The factorial of a positive integer n is defined as:
n
n! = ∏k
k =1
Alternatively, the factorial of n can be written recursively as
(n≤ 1 1
n! =〈
n > 1 n(n −1)!
For example, 5! = 5 x 4 x 3 x 2 x 1 = 120 and 0! = 1
Write a function that calculates the factorial using a loop:
function f = MyFactorial(x)
Does your code handle the cases where x <= 0?
Task 5
The base of the natural logarithm e can be approximated by summing up a series of terms.
1 1 1
e = 1+ + + + ......
1! 2! 3!
Write a function that calculates e up to the 5! term. Note that MyExp has no inputs. Use the MyFactorial function from Task 4 to find factorials.
function e_val = MyExp
Extend your function so that the number of terms, n, is also an input
function e_val = MyExp2(x)
So MyExp2(3) will calculate:
1 1 1
e = 1+ + +
1! 2! 3!
Task 6
Write MyExp as an Anonymous function. Can you do this for MyExp2?
Task 7
Rewrite the functions from Task 6 with the following modifications.
1) Rewrite MyExp so that it calls a Subfunction called fact() that calculates the factorial. Call the new primary function MyExpVer2.
2) Rewrite MyExp2 using nested loops without calling any functions . Call the new function MyExp2Ver2.
Task 8
A simply-supported beam is loaded by forces and moments as shown in the figure above. Note that x = 0 at the left end of the beam and the dimensions shown are in meter. The forces shown are in Newton [N]. Using “singularity functions”, the displacement along the beam can be expressed by the beam equation below:
uy (x) = − x − 04 − x − 54 + x − 83 + 75x − 72 + x3 − x
where x is the distance along the beam starting from the left end of the beam and where the singularity function is defined as:
x − a n =〈(|(x − a )n when x > a
| 0 when x ≤ a
for any constants a and n .
STEP 1
Write a function (named this function “singu”) to implement the singularity function. Input to this function is scalar x, n and a. The output of this function is also a scalar value. Check your function for cases where x>a, x=a and x<a separately.
x − a n =〈|((x − a )n
| 0
when x > a
when x ≤ a
STEP 2
Write a function to implement the beam equation (named this function “beam”). The input to this function is an array, x. The output of this function is the displacement of the beam, Uy(x), which is a scalar. Include the singularity function written in STEP 1 as the subfunction of beam().
STEP 3
Write an m-file (means not a function file) to test your function, beam(). Name this script m-file ‘beam_eq.m’Use the beam() function to calculate the displacement of the beam from x=0 to x=8, use step size of 0.1. Plot the displacement of the beam. Make sure your plot is a red color solid line with point markers.
Task 9
1) Convert the decimal integers 15 and 16 to binary numbers.
2) Add the binary numbers. Perform the addition in binary!
3) If the computer stores numbers using 5 bits (unsigned), will the result of the addition be correct? Why?
HINT: You can check your answers in MATLAB using bin2dec() and dec2bin(). Do the problem by hand first before using MATLAB!
Task 10
Repeat Task 9 again with the decimal integers 29 and 13.
What is the largest unsigned (positive) integer than can be represented with N bits?
HINT: What is the biggest number that can be represented with 5 bits? What about 6 bits?
2022-12-01