Solution Practice 4 - ENG2314
Solution Practice 4 - ENG2314
School of Engineering
Solution Practice 4
Q1
x=1;
x0=0;
x1=5;
%when x=1
%using the interval between 0 and 5, i.e., x1=1; x2=5;
fx0=1-exp(-x0);
fx1=1-exp(-x1);
f1=fx0+(((fx1-fx0)/(x1-x0))*(x-x0))
true_value=1-exp(-x)
clc
x=1;
x0=0;
x1=3;
x2=8;
fx0=1-exp(-x0);
fx1=1-exp(-x1);
fx2=1-exp(-x2);
b0=fx0;
b1=(fx1-fx0)/(x1-x0);
b2=(((fx2-fx1)/(x2-x1))-((fx1-fx0)/(x1-x0)))/(x2-x0);
Page 1 of 4
Taylors University
School of Engineering
f2=b0+(b1*(x-x0))+(b2*(x-x0)*(x-x1))
true_value=1-exp(-x)
Solution = 0.3934
Q3
Discuss your results = similar to Q1 solution
Q4
Discuss your results = similar to Q2 solution
Q5
Lagrange first and second order solution
% 1st order Lagrange Interpolation fx1.
clc
x=18;
x0=0;
x1=20;
fx0=3.65;
fx1=0.75;
f1=(((x-x1)/(x0-x1))*fx0)+(((x-x0)/(x1-x0))*fx1)
%2nd order Lagrange Interpolation.
x2=40;
fx2=0.242;
p1=((((x-x1)*(x-x2))/((x0-x1)*(x0-x2)))*fx0);
p2=((((x-x0)*(x-x2))/((x1-x0)*(x1-x2)))*fx1);
p3=((((x-x0)*(x-x1))/((x2-x0)*(x2-x1)))*fx2);
f2=p1+p2+p3
f1 = 1.0400
f2 = 0.9324
Page 2 of 4
Taylors University
School of Engineering
Q6.
i=2
x = 4.5
y = 1.0
i=3
x = 7.0
y = 2.5
y = mx + c
2.51.0
1.5
c = 1.7
Q7.
clc
%a1x^2+b1x+c1
%a2x^2+b2x+c2
%a3x^2+b3x+c3
r_1=[9 3 1
r_2=[4.5^2
r_3=[0 0 0
r_4=[0 0 0
r_5=[0 0 0
r_6=[0 0 0
r_7=[4.5*2
r_8=[0 0 0
r_9=[1 0 0
3.0<=x<=4.5
4.5<=x<=7.0
7.0<=x<=9.0
0 0 0 0 0 0];
4.5 1 0 0 0 0 0
4.5^2 4.5 1 0 0
7^2 7 1 0 0 0];
0 0 0 7^2 7 1];
0 0 0 9^2 9 1];
1 0 -4.5*2 -1 0
7*2 1 0 -7*2 -1
0 0 0 0 0 0];
0];
0];
0 0 0];
0];
A=[r_1;r_2;r_3;r_4;r_5;r_6;r_7;r_8;r_9];
S=[2.5 1.0 1.0 2.5 2.5 0.5 0 0 0]';
k=A\S
a2=k(4,1);
b2=k(5,1);
c2=k(6,1);
x=5;
y=a2*x.^2+(b2*x)+c2
Solution = 0.66
Page 3 of 4
Taylors University
School of Engineering
Challenging Question
8.
Linear interpolation
1 = (0 ) +
1 = 90 +
(1 ) (0 )
( 0 )
1 0
81.2 90
( 3000)
6100 3000
when x = 5000,
1 = 84.3226
Quadratic Interpolation
0 = (0 ) = 90
1 =
(1 ) (0 )
81.2 90
=
= 0.002838
1 0
6100 3000
(2 ) (1 ) (1 ) (0 )
75.6 81.2
81.2 90
6100 3000
2 1
1 0
7900
6100
2 =
=
= 5.5737 108
2 0
7900 3000
When x = 5000,
2 () = 0 + 1 ( 0 ) + 2 ( 0 )( 1 )
2 () = 90 0.002838( 3000) 5.5737 108 ( 3000)( 6100)
2 (5000) = 90 0.002838(5000 3000) 5.5737 108 (5000 3000)(5000 6100)
2 (5000) = 84.4449
Page 4 of 4