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

EX2 Answers

Uploaded by

Anass Aloulou
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)
31 views3 pages

EX2 Answers

Uploaded by

Anass Aloulou
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
You are on page 1/ 3

4WM20 - Exercises Lecture 2 - ANSWERS

Note that your script might be a little bit different, but accomplishes the same
goal.

1 Creating your own bode plot

1.1 custombode.m

You do still need to add a for loop to solve step 9.


1 clearvars ; clc ; close all ;
2 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % Script for custom bode plotting
4 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6 % % Initialization
7 w = logspace ( -2 ,2 ,100) ; % Create a logspaced frequancy vector
8 sys = tf (1 ,[1 2 1]) ; % Define the transfer function
9 bode ( sys ) % Visualize the bode plot
10
11 % % Computation
12 [ mag , phase ] = bode ( sys , w ) ; % Obtain the magnitude and phase
13
14 mag = squeeze ( mag ) ; % Remove dimensions of size 1
15 phase = squeeze ( phase ) ; % Remove dimensions of size 1
16
17 magdB = 20* log10 ( mag ) ; % Convert the magnitude to decibel
18
19 % % Visualisation
20 h = figure ; % Create figure handle
21 subplot (2 ,1 ,1) % Create subplot 1
22 semilogx (w , magdB ) % Plot the magnitude versus the frequency
23 ylim ([ -80 0]) % Change the y limits
24 set ( gca , ’ xticklabel ’ ,[]) % Remove x tick labels
25 ylabel ( ’ Magnitube ( dB ) ’) % Add a y - axis label
26 title ( ’ Bode Diagram ’) % Add a title
27
28 subplot (2 ,1 ,2) % Create subplot 2
29 semilogx (w , phase ) % Plot the phase versus the frequency
30 ylim ([ -180 0]) % Change the y - axis limits
31 ylabel ( ’ Phase ( deg ) ’) % Add a y - axis label
32 xlabel ( ’ Frequency ( rad / s ) ’) % Add a x - axis label

1
2 Determining pi to the nth decimal

2.1 determinepi.m

1 clearvars ; clc ; close all ;


2 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % Script to determine pi to the nth digit
4 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6 % % Initialisation
7 n_max = 7; % Define decimal place of pi to be calculated
8 pi_nthdec = zeros (1 , n_max ) ; % Create an empty vector for storing pi
9 numberit = zeros (1 , n_max ) ; % Empty vector for storing number iterations
10 seriestype = ’ Machin ’;
11
12 % % Computation
13 for n = 1: n_max % for loop from 1 to 7
14 [ pi_nthdec ( n ) , numberit ( n ) ] = piseries (n , seriestype ) ; % Call piseries . m
15
16 % % Visualization
17 formatspec = [ ’ pi to the %i - th decimal place is :\ n %. ’ , num2str ( n ) , ’f \ n ’ ];
18 fprintf ( formatspec ,n , pi_nthdec ( n ) ) ; % print pi to the workspace
19 end
20 semilogy (1: n , numberit , ’b * ’) ; % Plot the number of iterations
21 xlabel ( ’ Decimal place accuracy ’) % x - axis label
22 ylabel ( ’ Number of iterations ’) % y - axis label
23 title ( ’ Number of iterations to determine pi to the n - th decimal place ’)

2.2 piseries.m

1 function [ pi_nthdec , numberit ] = piseries (n , seriestype )


2 % PISERIES Function to calculate pi to the n - th digit
3 % The input is the number of the decimal place it need to calculate it to
4 % and the outputs are the value of pi and the number of iterations it
5 % took to determine it
6
7 % % Initialisation
8 pi_nthdec = 0; % set to 0
9 sum = 0; % set sum to 0
10 p = 0; % set iteration number to 0
11
12 % % Loop
13 switch seriestype
14 case ’ Euler ’ % if Euler series is selected
15 while abs ( pi_nthdec - pi ) > 1*10^( -( n +1) )
16 sum = sum + (1/( p +1) ^2) ; % add next term to sum
17
18 pi_nthdec = sqrt (6* sum ) ; % determine pi
19 p = p +1; % increase n iterations
20 end % while
21 case ’ Machin ’ % if Machin series is selected
22 while abs ( pi_nthdec - pi ) > 1*10^( -( n +1) )
23 sum = sum + (4* arctangentterm (1/5 , p ) - arctangentterm (1/239 , p ) ) ;
24 pi_nthdec = sum *4; % determine pi
25 p = p +1; % increase n iterations
26 end % while
27 otherwise % if something else

2
28 pi_nthdec = NaN ; % then output a NaN
29 end % switch
30 numberit = p ;
31 end % function

2.3 arctangentterm.m

1 function term = arctangentterm (x , m )


2 % ARCTANGENTTERM (X , M ) Determines the n - th term of the infinite series that
3 % calculates the arctangent of x .
4 % x is the arctangent and m is the mth - term . It outputs the term .
5 term = ( -1) .^ m * ( x .^(2* m +1) ) /(2* m +1) ;
6 end

You might also like