0% found this document useful (0 votes)
12 views

4WM20 - Exercises Lecture 2: 1 Creating Your Own Bode Plot

Uploaded by

Anass Aloulou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

4WM20 - Exercises Lecture 2: 1 Creating Your Own Bode Plot

Uploaded by

Anass Aloulou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4WM20 - Exercises Lecture 2

1 Creating your own bode plot

Bode plots are often used to visualize the response of a dynamic system to a stimulus. Matlab
has a build in function called bode(sys), but it does not leave much room for customization.
Therefore, we would like to create our own bode plotting code.
For testing our code, we will need a simple dynamic system. We will consider a mass-spring-
damper system, of which the equation of motion is:
F (t) = mẍ + cẋ + kx (1.1)
in which m = 1 kg, c = 2 Nm/s and k = 1 N/m.
We can derive the transfer function of this system (response to a system by an external
force), using the Laplace transform to each term. Details on this derivation is outside the
scope of this course, but we will end up with:
1
H= (1.2)
ms2 + cs + k
In MATLAB we can define the transfer function by the in-build function sys = tf(numerator,
denumerator) (open doc tf for more information).
Define your system using tf, then try bode(sys). Look at the resulting plot. The goal is
to recreate this result with your own custom visualization code.
The steps to follow are:

1. Create a script called custombode.m. Follow the standard coding etiquette’s, such as
a header and workspace management. Create the sections: initialization, computation
and visualization.
2. Define a frequency vector w which runs from 0.01 to 100 rad/s. (note that the response
in the end will be plotted on a logarithmic frequency axis)
3. Use [mag,phase] = bode(sys,w); to obtain the magnitude and angle of the system
at frequencies w.
4. In the visualization section, create a figure with handle h, which contains two subfigures
on top of each other.
5. Plot the magnitude on the top subfigure and the angle on the bottom figure, both as
function of the frequency. The x-axis should be in logarithmic scale. Do you get an
error? Have a look in your workspace, you might need to reduce/swap dimensions in
some of your values. Which comments do that? (Tip: see lecture 1, slide 22).
6. You might have seen that the magnitude is not in Decibel units. Convert your magni-
tude into Decibel units and plot again.
7. Place correct x and y labels, as well as a title to the figure.
8. in bode, there are no numbers below the top figure. These are called tick labels. Find
out how to remove these labels (NOTE: Do not remove the ticks, only the tick labels)
9. Plot the result for m = 1, m = 5 and m = 10 kg in the same graph. (use hold on/off)

1
2 Determining pi to the nth decimal

PART 1: Matlab has its build in number of pi, but for this exercise you are going to write
your own function to determine it. Multiple infinite series exist to determine pi accurately,
we will use the one from Euler:

π2 X 1
= (2.1)
6 m2
m=1

To determine π to accurately, we have to set a criteria to know when our nth -decimal place
is correct. The best way it to do this by comparing it by the in-build value of pi. What
could this criteria then look like?
We would like to display pi with the correct amount of decimals in the command window.
The process of writing your own code is:

1. Create a script determinepi.m and a function piseries.m. Make sure that they are
placed in the same folder (your current folder, for instance).

2. In both files, hold yourself to the clean coding style. So, start with a header and define
in determinepi.m three sections: Initialization, Computation and Visualization.

3. In function piseries.m define as your input variable n and as output pi nthdec.

4. In script determinepi.m, put in initialization section the defenition of the nth digit (n =
;), in your computation section you call function piseries.m and in the visualization
section, print the outcome of the function to the workspace using fprintf. Which
formatSpec do you need to use? (Tip: your ’%.nf’ needs to be adaptive, so predefine
your formatSpec as a string, and input that into fprintf(formatSpec,n,pi nthdec).)

5. Implement the Euler series of Equation 2.1 in piseries.m. You might need to use
loops and conditional statements. Which one(s) do you need? Implement the criteria
of when to stop adding higher terms. Do you need to store things in a array or not?

6. Try your script by using small digits (≤ 5). Does it work? Compare your result to the
build in definition of π.

Once you have done this, you can extend your code in the following ways:

1. Adapt your function, such that it also outputs the amount of iterations you need, to
get the required decimal place correct.

2. Plot the amount of iterations your function need to go through, as function of the
required digit. Don’t go to high numbers of n, maybe only up to 7 decimal places.

3. Add labels and a title to your figure. (Tip: You will need to use a for-loop to call your
function multiple times and also store the outcome in a vector.)

2
PART 2: A faster way of solving this problem is by using the Machin series:
π 1 1
= 4 arctan − arctan (2.2)
4 5 239
in which the arctangent can be approximated by:

X x2m+1
arctan(x) = (−1)m (2.3)
2m + 1
m=0

We would like to build this series into our function. Beware of that atan(x) is an inbuild
function in Matlab, but I am not asking you to use that one. I ask you to use the infinite
series.
To extend your code:

• Extend your function in that it requires also the input seriestype, which variable
type is a string.

• Implement a switch condition inside your function, with cases are ’Euler’ and ’Machin’,
it switches based on the inputted seriestype.

• Write a new function(-file) arctangentterm.m which calculates one term of the sum
in equation 2.3.

• You can now call y = arctangentterm(x,m) within piseries.m under the case ’Machin’

• Return NaN if the seriestype does not match the two cases.

• Compare the number of iterations required for determining the same digit with either
the Euler of Machin series. (you can also plot the difference)

You might also like