Report For Numerical Techniques Assignments
Report For Numerical Techniques Assignments
I hereby certify, that this report, submitted as a part of the BEng (Hons) Mechanical Engineering,
is entirely the work of the author and that any resources used for the completion of the work
done are fully referenced within the text of this report.
Signature: Date:
Q1:
Plot f(x) = 1 / 48 (693x⁶ - 945x⁴ + 315x² - 15) in GeoGebra to find the starting value
for the roots:
The starting value for the roots are -2, -0.7, -0.3, 0.3, 0.7, 2.
1
Results are shown in the spreed sheet below:
2
error for-0.23861919 is 1 10 8
error for0.23861919 is 1 10 8
Q2
Plot f(x) = ℯ^x + 1.138 - x³ and g(x) = 0.28561x⁴ + 0.39546x³ - 1.51255x² + 0.156x
+ 0.72 in GeoGebra to find the starting value for the roots:
3
Results are shown in the spreed sheet below:
4.4986933(the error is 1 10 7 )
4
(ii) Starting value for roots of 0.28561x⁴ + 0.39546x³ - 1.51255x² + 0.156x +
0.72=0 are -4, -1, 1.
5
Roots for 0.28561x⁴ + 0.39546x³ - 1.51255x² + 0.156x + 0.72=0 are
-3.076923077(the error is 1 10 9 )
-0.615384615(the error is 1 10 9 )
1.153846136(the error is 1 10 9 )
Q3
6
Do eight times iteration to improve the initial guess:
(2.731642044,1.644875573) error: 1 10 9
7
Q4
(a)
Change into coupled pair of first order ODEs:
dx
z
dt
dz
x3 x z
dt
(b)
Plot the displacement and velocity of the particle and display the global error bound:
8
Q5
Parameters for my spring-damp system:
M1=180;
M2=50;
K1=15000;
K2=10000;
B=30;
A=20;
w=2;
Results for the displacements and velocities of M1 and M2:
9
Appendix
Code for Q4
Driver:
%NAME: ZHUYU; STUDENT NUMBER:X00133408
% DEdriverPair- Programme to compute solution of a pair of ODEs
% using the 4th order Runge-Kutta method.
% It calls a METHOD function rk4 which in tern will call the
% DETAILS function rhsPairdetails which contains the right side of
the
% system of ODEs written in the vector form dy/dx=f(x,y)
% where y can be a vector of dependent variables.
% variable names:
% indep - stores independent variable
% state - stores dependent variable vector
%
% indepplot - vector to store indep values produced each step
% stateplot - matrix to store dep values produced each step
clear;
help DEdriverPair;
format long;
10
nstep=(final-indep0)/step;% calculating the number of steps to be
taken.
%% main loop
for i=0:nstep
indepplot(i+1,1)=indep; %x
stateplot(i+1,1)= state(1) ;%y
stateplot(i+1,2) = state(2); % z
% call to rk4 function which does work -the sequence of the inputs
to the
% function is the crucial element
state=rk4(indep,state,step,'rhsPairdetails',fred);
end
%% plotting answers
subplot(121)
plot(indepplot,stateplot(:,1),'*')
grid
xlabel('time')
ylabel('displacement')
title('Displacement of the particle' )
text (indep0, dep0, [' global error is' ' '
num2str(globalerrorestimate)] )
subplot(122)
plot(indepplot,stateplot(:,2),'.')
11
grid
xlabel('time')
ylabel('velocity')
title('Velocity of the particle')
Method:
%NAME: ZHUYU; STUDENT NUMBER:X00133408
function Yout=rk4(x,Y, h,derivsRK,param)
% 4th order Runge-Kutta integrator
% Input arguments -
% x=independent variable
% Y=current value of vector of dependent variables
% h=step size
% derivsRK=RHS of the ODE called as derivsRK(x,Y,param)
% param = spare parameter
% Output arguments -
% Yout=new value of Y, vector of dependent variables
%
% Refer to the lecture notes for the meaning of each term
%
hh=0.5*h;
k1=h*feval(derivsRK,x,Y, param);
xhalf=x+hh;
Ytemp= Y+k1/2.;
k2=h*feval(derivsRK,xhalf,Ytemp, param);
Ytemp= Y+k2/2.;
k3=h*feval(derivsRK,xhalf,Ytemp, param);
xfull=x+h;
Ytemp= Y+k3;
k4=h*feval(derivsRK,xfull,Ytemp,param);
Yout= Y+(k1+2.*k2+2.*k3+k4)/6.0;
return;
Details:
%NAME: ZHUYU; STUDENT NUMBER:X00133408
function rhsderiv=rhsPairdetails(w,s, q)
% this function stores the details of the right hand side of a SET
of ODEs
% of the form ds/dw = rhsderiv which is to to be solved numerically,
% where:
% w = independent variable
% s = vector of dependent variables
% q = to allow for the passing of extra parameters
% rhsderiv, contains the details of the right hand side of the
12
SET of ODEs
rhsderiv=[s(2),-s(1)^3-s(1)-s(2)];
return;
Code for Q5
Driver:
%NAME: ZHUYU; STUDENT NUMBER:X00133408
% DEdriverPair- Programme to compute solution of a pair of ODEs
% using the 4th order Runge-Kutta method.
% It calls a METHOD function rk4 which in tern will call the
% DETAILS function rhsPairdetails which contains the right side of
the
% system of ODEs written in the vector form dy/dx=f(x,y)
% where y can be a vector of dependent variables.
% variable names:
% indep - stores independent variable
% state - stores dependent variable vector
%
% indepplot - vector to store indep values produced each step
% stateplot - matrix to store dep values produced each step
clear;
help DEdriverPair;
format long;
13
%% Establishing step size and number of steps
final=input('Enter final value of time you want to calculate:');
step=input('Enter stepsize of time:');
localerrorestimate= step^5 % local error for rk4
globalerrorestimate = step^4 % global error for rk4
nstep=(final-indep0)/step;% calculating the number of steps to be
taken.
%% main loop
for i=0:nstep
indepplot(i+1,1)=indep; %t
stateplot(i+1,1)= state(1) ;%x1
stateplot(i+1,2) = state(2); %x2
stateplot(i+1,3)= state(3) ;%v1
stateplot(i+1,4) = state(4); %v2
% call to rk4 function which does work -the sequence of the inputs
to the
% function is the crucial element
state=rk4(indep,state,step,'rhsPairdetails',fred);
end
%% plotting answers
subplot(221)
plot(indepplot,stateplot(:,1),'*')
grid
xlabel('t')
ylabel('x1')
14
title('Displacement of M1' )
text (indep0, dep0, [' global error is' ' '
num2str(globalerrorestimate)] )
subplot(222)
plot(indepplot,stateplot(:,2),'*')
grid
xlabel('t')
ylabel('x2')
title('Displacement of M2' )
subplot(223)
plot(indepplot,stateplot(:,3),'.')
grid
xlabel('t')
ylabel('v1')
title('Velocity of M1')
subplot(224)
plot(indepplot,stateplot(:,4),'.')
grid
xlabel('t')
ylabel('v2')
title('Velocity of M2')
Method:
%NAME: ZHUYU; STUDENT NUMBER:X00133408
function Yout=rk4(x,Y, h,derivsRK,param)
% 4th order Runge-Kutta integrator
% Input arguments -
% x=independent variable
% Y=current value of vector of dependent variables
% h=step size
% derivsRK=RHS of the ODE called as derivsRK(x,Y,param)
% param = spare parameter
% Output arguments -
% Yout=new value of Y, vector of dependent variables
%
% Refer to the lecture notes for the meaning of each term
%
hh=0.5*h;
k1=h*feval(derivsRK,x,Y, param);
xhalf=x+hh;
15
Ytemp= Y+k1/2.;
k2=h*feval(derivsRK,xhalf,Ytemp, param);
Ytemp= Y+k2/2.;
k3=h*feval(derivsRK,xhalf,Ytemp, param);
xfull=x+h;
Ytemp= Y+k3;
k4=h*feval(derivsRK,xfull,Ytemp,param);
Yout= Y+(k1+2.*k2+2.*k3+k4)/6.0;
return;
Details:
%NAME: ZHUYU; STUDENT NUMBER:X00133408
function rhsderiv=rhsPairdetails(w,s, q)
% this function stores the details of the right hand side of a SET
of ODEs
% of the form ds/dw = rhsderiv which is to to be solved numerically,
% where:
% w = independent variable
% s = vector of dependent variables
% q = to allow for the passing of extra parameters
% rhsderiv, contains the details of the right hand side of the
SET of ODEs
M1=180;
M2=50;
K1=15000;
K2=10000;
B=30;
A=20;
r=2;
16