Documente Academic
Documente Profesional
Documente Cultură
959646
EG-264
ASSIGNMENT 1
Section A
Description of problems and explanation of solutions
Section B
m-files with outputs (including graphs) in the following order:
1. compositetrapezoid.m
2. f.m
3. cumulative_distance.m
4. main.m
5. bisection_method
6. z.m
7. graph2.m
I used the four following m-files to solve question 1:
1. compositetrapezoid.m
2. f.m
3. cumulative_distance.m
4. main.m
I will briefly describe how I solved the problems by summarising my approach in each m.
file.
Question 1
Problem description
Part 1 requires the use of the composite trapezoidal rule calculate an approximate distance
travelled by the Camaro in metres with a relative error less than 0.00002% and the doubling
of the number of separations if the relative error condition is not met.
Displays of the integral value of the distance calculated, the number of sections used and
the relative error is to be produced for each run-through of the loop using the fprintf
commands
Part 2 asks for a single figure with two subplots showing a speed (m/s) vs time (t) graph of
the combined speed equations in one plot and a cumulative distance (m) vs time(t) in the
second subplot.
Part 3 asks for a figure showing the total distance calculated against the number of
separations used in each numerical integration calculation; use a logarithmic x-axis scale on
the resulting plot.
compositetrapezoid.m
Define a function to calculate the integral using the composite integral rule
Define the integration limits
Define the number of sub-intervals to be used
Define the length of sub-intervals being used
Set initial values for the variables integral and integral2
I encountered a challenge with finding an initial value for the relative error before
initiating a loop therefore I created a variable integral 2 with twice as many sub-
intervals to allow me to calculate an initial relative error and simplify the
programming process for calculating relative error.
Define a sub- function to approximate the integral using the composite trapezoid
rule
Use a for loop to add the contribution of each sub-interval in turn
Call the integral functions to run using both integral and integral 2
Calculate the initial relative error by using the in built function abs to obtain the
absolute value of a calculation :
relative_error = abs(((integral2-integral)/integral2))
The relative error percentage by multiplying the relative error by 100
Use a while loop to run the composite trapezoid integration method until the
relative error percentage is less than 0.00002
The number of sub-intervals n and n_2 used for both integral and integral 2 will
double every time the loop runs through and hence the length of the sub-intervals h
and h_2 will also changed every time the loop runs through, the integral functions
using the composite trapezoid rule will be called afterwards and a new relative error
and relative error percentage will be calculated until the relative error percentage is
less than 0.00002
fprintf commands are the used to produce the outputs for the calculations in the
loops until the while loop condition is met.
f.m
Define a function to calculate the speed value for different values of time using the
conditional statements if, elseif and else. Multiply the functions by a vconvert speed from
mph to m/s
cumulative_distance.m
Define a function to calculate the cumulative distance and store the values in an array by
approximating the integral using the composite trapezoid rule
main.m
Use a for loop to calculate and store speed in a vector for time from 0 seconds to 25 seconds
in intervals of 0.01
Redefine the integration limits
Define the number of sub-intervals calculated from the compositetrapezoid m file
Define the length of sub-intervals being used
Call the cumulative distance function
Plot the speed against time graph and the cumulative distance against time graph as
subplots using the subplot and indexes, using xlim, ylim, title, xlabel and ylabel to label the
axes of the graphs
Plot the total distance against log of the number of separations
Question 2
Problem description
An undamped system is harmonically excited using an external forcing frequency 𝜔, which
produces vibration of the system. The displacement of the system over time (x(t)) can be
calculated using the equation provided , when using the parameter values detailed in the
table provided.
The acting driving frequency 𝜔 however is unknown.
Part 1 requires the use of the bisection method to determine an approximation of the value
of 𝜔, with an absolute error of less than 1 × 10−6 ; when the displacement x = 0.0386m, at
time t = 4.54s, limiting the useable values of 𝜔 between: 5.12 < 𝜔 < 6.45. Display the
resulting value of 𝜔, and the absolute error obtained.
Part 2 requires a a plot of the displacement x(t), between 0 < 𝑡 < 6𝑠, using the driving
frequency 𝜔 calculated in part 1.
bisection_method.m
Define a function to find the root of a function using the bisection method.
Define the initial intervals and ensure that their product is less than or equal to zero.
Call the function z from z.m file to find the function value at the interval values.
Set an initial value for the variable root.
Call the function that calculates the maximum absolute error.
Define the function to calculate the maximum absolute error.
Set an initial value for the maximum absolute error .
Use a while loop to run the bisection method and calculate the root and maximum
absolute error until the maximum absolute error is less than 10^-6.
z.m
graph2.m
function [f_value]=f(x)
if x < 5
f_value = (0.1553567*(x^6)-2.0416*(x^5)+9.1837*(x^4)-14.829*(x^3)-1.3703*(x^2)+32.
821*(x)-1.3155)*0.44704;
f_value = (0.003980879*(x^5)-0.2247*(x^4)+4.8682*(x^3)-50.442*(x^2)+254.67*(x)
-430.66)*0.44704;
f_value = (-0.073*(x^2)+6.1802*(x)+40.423)*0.44704;
else
end
end
08/11/18 22:49 C:\Users\danie\Do...\cumulative_distance.m 1 of 1
%function to calculate the cumulative distance by approximating the integral using the
composite trapezoid rule
function [cumulative_distance]=cumulative_distance(n,a,h)
integral=0.0;
for i=1:n
x_left = a+(i-1)*h;
x_right = a+i*h;
f_left=f(x_left);
f_right=f(x_right);
integral=integral+(h/2.0)*(f_left+f_right);
cumulative_distance(i,1) = integral;
cumulative_distance(i,2) = x_left;
end
end
08/11/18 22:53 C:\Users\danie\Documents\MATLAB\z.m 1 of 1
% Stiffness ? N/m
k=1007.564;
%Mass ? kg
m=98.668;
%Time ? 4.54 s
t=4.54;
f_o = F_o/m;
%Displacement ? 0.0386 m
x_t=0.0386;
end