ProductsLogo
LogoStudy Documents
LogoAI Grader
LogoAI Answer
LogoAI Code Checker
LogoPlagiarism Checker
LogoAI Paraphraser
LogoAI Quiz
LogoAI Detector
PricingBlogAbout Us
logo

MATLAB Interpolation Techniques

Verified

Added on  2023/05/28

|56
|6920
|97
AI Summary
This article explains how to perform various interpolation techniques using MATLAB, including linear, quadratic, cubic, Lagrange, spline, and Hermit interpolations. It also discusses the differences between polynomial and Lagrange interpolation and Lagrange and spline interpolation.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
1. Given two points x=[1 2] and their functions f=[3 4], using MAT LAB
to find the linear interpolation function and plot the function (3
points).
clc
clear all
close all
x=[1 2]'
f=[3 4]'
x1=[1.1:0.1:1.9]'
y1=interp1q(x,f,x1)
figure(1)
plot(x,f,'o-g',x1,y1,'*--b')
1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2
3
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
2. Given three points: x=[1 2 3] and f=[2 4 3], using MATLAB to find
the quadratic interpolation and plot the function (3 points).
clc
clear all
close all
x=[1 2 3]
f=[2 4 3]
y=polyfit(x,f,2)
x1=[1:0.1:3]
y1=polyval(y,x1)
figure(2)
plot(x,f,'o',x1,y1,'r')
Document Page
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
2
2.5
3
3.5
4
4.5
3. Given the following table showing the density and velocity
measurements of some rocks. Use MATLAB and (a) find the cubic
interpolation for the relationship between the densities and velocities,
(b) plot the curve showing the interpolation function over the range [2,
3.5], and (c) discuss the results about the extrapolations of density < 2
and >3.2 (6 points).
Density (kg/m^3) 2.3 2.5 2.7 3.2
Velocity (km/s) 5.0 4.2 4.8 5.5
clc;clear all; close all
D=[2.3 2.5 2.7 3.2]
V=[5 4.2 4.8 5.5]
y=polyfit(D,V,3)
x1=[2.3:0.01:3.5]
y1=polyval(y,x1)
Document Page
figure(3)
plot(D,V,'o',x1,y1,'r')
2.2 2.4 2.6 2.8 3 3.2 3.4 3.6
0
1
2
3
4
5
6
7
%Extrapolations
y=polyfit(D,V,7)
x1=[2:0.01:3.2]
y1=polyval(y,x1)
figure(4)
plot(D,V,'o',x1,y1,'r')
2 2.2 2.4 2.6 2.8 3 3.2 3.4
4
4.5
5
5.5
6
6.5
7
7.5
8
8.5

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
4. Use MATLAB function: a=polyfit(x,y,n), (a) find the best fitting
polynomial for the data given in table of Question 3, and then (b)
apply MATLAB function f=polyval(a, xp) to estimate the values at
xp=linspace(2,3.5,20) and plot the curve and the original data, (c)
compare the results with one obtained in Question 3.
clc;clear all; close all
D=[2.3 2.5 2.7 3.2]
V=[5 4.2 4.8 5.5]
y=polyfit(D,V,12)
x1=[2:0.1:3.9]
p1=linspace(2,3.5,20)
y1=polyval(y,p1)
length(x1)
length(p1)
figure(5)
plot(D,V,'o',x1,y1,'r',x1,y1,'*')
Document Page
2 2.2 2.4 2.6 2.8 3 3.2 3.4 3.6 3.8 4
-180
-160
-140
-120
-100
-80
-60
-40
-20
0
20
5. Use the two points given by Question 1 and (a) find the Lagrange
interpolation function of the points and (b) prove it same as the results
of Question 1 (2 points).
clc
clear all
close all
x=[1 2]'
f=[3 4]'
x1=[1.1:0.1:1.9]'
y1=interp1q(x,f,x1)
figure(6)
subplot(2,1,1)
plot(x1,y1,'o-g')
subplot(2,1,2)
Document Page
fp=interp_lagrange(x,f,x1)
length(fp)
plot(x1,fp)
function fp=interp_lagrange(x,f,xp)
n=length(x);
m=length(f);
L=length(xp);
if n~=m,error('x and f must have same length'),
end
for k=1:L
fp(k)=0;
for i=1:n
p=f(i)
for j=1:n
if j~=i
p=p*(xp(k)-x(j))/(x(i)-x(j));
end
end

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
fp(k)=fp(k)+p;
end
end
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
3
3.2
3.4
3.6
3.8
4
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
3
3.2
3.4
3.6
3.8
4
6. Using the three points given in Question 2 and (a) find the Lagrange
interpolation function of the points and (b) prove it same as the result
of Question 2 (2 points).
clc
clear all
close all
x=[1 2 3]
f=[2 4 3]
y=polyfit(x,f,2)
x1=[1:0.1:3]
y1=polyval(y,x1)
subplot(2 , 1,1)
Document Page
plot(x1,y1,'r')
subplot(2,1,2)
fp=interp_lagrange(x,f,x1)
length(fp)
plot(x1,fp)
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
2
2.5
3
3.5
4
4.5
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
2
2.5
3
3.5
4
4.5
7. Given the following 20 points:
x -
10
-
9
-
8
-
7
-
6
-
5
-
4
-
3
-
2
-
1
1 2 3 4 5 6 7 8 9 10
f -1 -
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
1 1 1 1 1 1 1 1 1 1
Use the Lagrange function given below to (a) perform interpolations for
the points of xp=linspace(-10,10,40) and plot the result; (b) calculate the
errors at all the points of xp for the true function f=sing(x), and plot the
errors (4 points). function fp=Interp_Lagrange(x,f,xp)
n=length(x); m=length(f); L=length(xp)
if n~=m, error(‘x and f must have same length’), end
for k=1:L
fp(k)=0;
for i=1:n
p=f(i);
for j=1:n
If j~=i
p=p*(xp(k)-x(j))/(x(i)-x(j));
end
end
fp(k)=fp(k)+p;
end
end
Document Page
clc
clear all
close all
x=[1 2]
f=[3 4]
xp=linspace(-10,10,40)
fp=interp_lagrange(x,f,xp)
plot(x,f,'o',xp,fp,'--r')
f1=sin(xp)
erf(f1)
plot(f1,erf(f1),'p--b')

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
8. n Chebyshev points in an interval [ xa , xb ] can be found by the
following three steps: (1) use the radio of R=( xbxa)/2 and draw the
half circle at the middle point ( xa+ xb )/2 (see Fig.1); (2) divide into
(n-1) equal angles:
=
/(n-1); (3) the x-coordinates of the nodes on
the half circle are Chebyshev points:
xi= xa+ xb
2 + Rcos( π(i1) Δθ ), ¿(i=1,2 , .. . ,n ). ¿ .
Given the interval [ xa , xb ] =[-10, 10], (a) find 20 Chebyshev points in
this interval and show their distributions; (b) perform the Chebyshev
interpolation for the function f (x )=sign( x ) at the points: xp=linspace(-
10,10,40); (c) calculate the errors of the interpolation at the points xp,
plot the error curve and compare with the result of Question 7 (6
points).
Fig.1
Document Page
clc
clear all
close all
xa=-10
xb=10
p=[-10:1:10];
y=linspace(-10,10,20);
xp=linspace(-10,10,40)
n=length(p)
xp=linspace(-10,10,40);
f=sign(y)
r=(xb-xa)/2
m=(xb+xa)/2
theta=pi/(n-1)
for i=1:20
x(i)=m+r*cos(pi-(i-1).*theta)
end
Document Page
length(x(i))
figure(1)
plot(x,y)
y=[linspace(-10,10,20)]'
xp=[linspace(-10,10,40)]'
f=sign(y)
figure(2)
y1=interp1q(y,f,xp)
plot(y,f,'o--r',xp,y1,'*')
hold on
erf(f)
plot(y,erf(f),'+--b')
hold off
-10 -8 -6 -4 -2 0 2 4 6 8 10
-10
-8
-6
-4
-2
0
2
4
6
8
10

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
-10 -8 -6 -4 -2 0 2 4 6 8 10
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
9. Explain the differences between polynomial interpolation and
Lagrange interpolation (2 points)
Polynomial Interpolation:
It is interpolation of given dataset of the lowest possible degree that
passes through the given points of dataset(range).
Lagrange interpolation
It is use for polynomial interpolation. It is lowest degree that assume at
each value of (x)
10. Explain the main difference between the Lagrange interpolation and
Spline interpolation (2 points).
Spine Interpolation:
Document Page
It is use all of the available data to construct acubic b/w each pair of point
that is continuous with first and second derivatives.
Lagrange Interpolation:
It is simply interpolation with a cubic polynomial with the two points
below the regional and two points above the region.
11.Given n points ¿ ¿ , where f i , f i
'
and f i
' '
are the
function value and the 1st and 2nd derivations at the point xi . If p( x ) ,
L( x ) , S(x) and H(x) represent the cubic polynomial, Lagrange, Spline
and Hermit interpolations, identify the following equations are true or
false (12 points):
(a) p( xi)=f i , ( True ) (b) p' (xi )=f i
'
,( false ) (c) p' ' ( xi )=f i
' '
;( false )
(d) L( xi )=f i , ( True ) (e) L' ( xi )=f i
'
, ( false ) (f) L' ' ( xi)=f i
' '
;(
false )
(g) S ( xi )=f i , ( True ) (h) S' ( xi )=f i
'
, ( false ) (i) S' '( xi )=f i
''
;( false
)
(j) H ( xi)=f i , (True ) (k) H' ( xi )=f i
'
, ( false ) (l) H' ' ( xi )=f i
' '
( false)
12. As number of points n increases, ___polynomial____________ and
____________Lagrange__ interpolations have oscillation problem
Document Page
that may cause large errors; _______Spline_____ and
________Hermit___ interpolations remove the oscillation by
remaining the slopes and convex or concaves of the original function;
Chebyshev interpolation reduce the oscillation by applying ______
Chebyshev _______ points (5 points).
(a) Spline, (b) Hermit, (c) Polynomial, (d) Lagrange, (e) Chebyshev.
13. 2D Lagrange interpolation formula is given by
f (x , y )=
i=1
nx

j =1
ny
li ( x )lj ( y )f ( xi , y j )
,
where nx and n y are the total numbers of the points in the x- and y-
direction, respectively. (a) Find the expressions of li( x ) and lj ( y ) as
n3=3 and n y=3 , i.e. x=[ x1 , x2 , x3 ] and y=[ y1 , y2 , y3 ]; (b) the
coordinates and nine values of function given by above table, find the
Lagrange interpolation in the range [2, 4] [4, 8] (13 points).
F(2,4)=7.2
F(3,4)=7.5
F(4,4)=8
F(2,6)=7.7
F(3,6)=7.4
F(4,6)=8.2
F(2,8)=6.5

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
F(3,8)=7.0
F(4,8)=7.2
f(x,y)/f(xi,yi)=l1(x)*l1(y)+ l2(x)*l2(y)+ l3(x)*l3(y)
14.What are optima? Distinguish local minima and global minimum and
local maxima and global maximum in the followingdiagram (6
points).
Optima is the best or most favourable point, degree and amount etc in
given range of data set.
Maxima: It is largest value of function
Minima: It is smallest value of function
Local Maxima
Global Maxima
Global Minima
Local Minima
Document Page
15.Explain why the first derivatives are zeros at minima and maxima? (5
points, hint: using graph and slopes nearby minima and maxima)
Because tangent at Maxima
or Minima is parallel to x-axis.
Since first derivative represent slope and slop of the line paralleled to
x axis is zero.
dy/dx=positive
dy/dx=Zero
dy/dx=Negetive
Document Page
16.Explain why the second derivatives f ' ' ( x¿)>0 at the minima x¿
and
f ' ' ( x¿)<0 at the maxima x¿
(4 points, hint: apply Taylor series:
f (x¿±Δx )f ( x¿ )±f '( x¿ ) Δx +f ' ' ( x¿) Δx2 ).
In that case if f’’(x*) is negative at the stationary point then that point
must be a maximum turning point.
if f’’(x*) is positive at the stationary point then that point must be a
minimum turning point.
if f’’(x*) is 0 at the stationary point then that point must be a
minimum turning point.
From taylor series
f (x¿±Δx ) =f(x*)+[fk(x*) Δx k/2!]
If k=even or 2
Δx 2 = positive
So f (x¿±Δx ) ≥f(x*)

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
17.Given the function f (x )=x2+8 x12 , find its optimum by hand and
using MATLAB respectively, and show the minimum or maximum (4
points).
clc
clear all
close all
f= @ (x) (-x.^2)+(8*x)-12
p=[-1 8 -12]
f1=polyder(p)
f2=polyder(f1)
r=roots(f1)
froot=(-r.^2)+(8*r)-12
froot1=-2.*(r)+8
froot2=-2
if froot2>=0
fprintf('Minima at %d',r)
else
Document Page
fprintf('Mixima at %d',r)
end
f =
@(x)(-x.^2)+(8*x)-12
p =
-1 8 -12
f1 =
-2 8
Document Page
f2 =
-2
r =
4
froot =
4
froot1 =
0
froot2 =

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
-2
Mixima at 4>>
18.Explain why the minimization of a function f (x ) can be applied to
find the maxima of a function f (x ) (2 points).
If our point is maxima, we can that this slope starts off positive,
decreases to zero at the point, than becomes negative. As we move
through and past the point. Our slope is decreasing throughout this
movement. So that we must have f’’(X*) <0
19.(a) Describe the steps or draw a flowchart of the bisection method to
find the root of a function f (x ) in an interval [ xa , xb ] , and (b) then
write a M-function of the method; in addition, (c) apply the M-
Document Page
function to find minimum of f ( x )=3+6 x +5 x2+3 x3 +4 x4
in
x
[-2,1] (10 points).
clc
clear all
close all
a=input('Enter the function','s')
f=inline('a')
xa=input('enter the first value')
xb=input('enter the second value')
er=input('enter allow error')
if f(xb)*f(xa)<0
else
fprintf('guess is incorrect')
xa=input('enter the first value of guess
interval \n')
xb=input('enter the last value of guess
interval \n')
end
for i=2:1000
xr=(xa+xb)/2
if f(xb)*f(xr)<0
Document Page
xa=xr;
else
xb=xr;
end
if f(xa)*f(xr)<0
xb=xr;
else
xa=xr;
end
xnew(1)=0;
xnew(i)=xr;
if abs(xnew(i)-xnew(i-1))/(xnew(i))<
er,break,end
end
str=['roots of eq is ',num2str(xr)]
Enter the
function3+6*x+5*(x.^2)+3*(x.^3)+4*(x.^4)
a =

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
3+6*x+5*(x.^2)+3*(x.^3)+4*(x.^4)
f =
Inline function:
f(a) = a
enter the first value-2
xa =
-2
enter the second value1
xb =
1
enter allow error0.001
er =
1.0000e-03
xr =
Document Page
-0.5000
str =
roots of eq is -0.5
20.(a) Describe the steps or draw a flowchart of the Golden search
method for optimization of a function f (x ) in x [ xa , xb ] , and then (b)
write a M-function of the method; In addition, (c) apply the M-
function to find the minima of the function:
f ( x )=3+6 x +5 x2+3 x3 +4 x4
in x
[-2,1] (10 points).
function
[x,fx,er,it]=goldmin(f,xa,xb,es,maxit,varargin)
if nargin<3
error('three value required');
Document Page
end
if nargin<4|isempty(er)
es=0.0001
end
if nargin<5|isempty(maxit)
maxit=50
end
phi=(1+sqrt(5))/2
it=0
while(1)
d=(phi-1)*(xa-xb);
x1=xa+d;
x2=xb-d;
if f(xa,varargin{:})<f(x2,varargin{:})
xo=x1;
xa=x2;
else
xo=x2;
xb=x1;
end

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
it=it+1;
if xo~=0
er=(2-phi)*abs((xa-xb)/xo)*100;
end
if er<=es| it>=maxit,break,end
end
x=xo
fx=f(xo,varargin{:});
clc
clear all
close all
f=@(x) (3+6*x+5*(x.^2)+3*(x.^3)+4*(x.^4))
[Xmin,fmin,er,it]=goldmin(f,-2,1)
f =
@(x)(3+6*x+5*(x.^2)+3*(x.^3)+4*(x.^4))
es =
Document Page
1.0000e-04
maxit =
50
phi =
1.6180
it =
0
x =
5.8540
Document Page
Xmin =
5.8540
fmin =
5.5090e+03
er =
7.2113e-05
it =
26
>>

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
21.The following diagram shows the contour of a function f(x,y). Answer
the following questions (10 points):
(a) Indicate where the gradient become zero (f=0) and the miniumn
and maximum are;
(b) Point out the positive (+f) and negative (-f) gradient directions
at the red arrows.
Gradient=0
Gradient=Minimum
Gradient=maximum
Negative Gradient
Direction
Positive Gradient
Direction
Document Page
22.According to the flowchart shown below and complete the M-function
of the Newton method to find the minima (10 points):
clc
clear
syms X Y;
f =input(‘enter function’)
x(1) = input(‘Enter value’);
y(1) = input(‘Enter value’);
e = input(‘Enter value’);
i = input(‘Enter value’);
df_dx = diff(f, X);
df_dy = diff(f, Y);
J = [subs(df_dx,[X,Y], [x(1),y(1)]) subs(df_dy, [X,Y],
[x(1),y(1)])];
Document Page
ddf_ddx = diff(df_dx,X);
ddf_ddy = diff(df_dy,Y);
ddf_dxdy = diff(df_dx,Y);
ddf_ddx_1 = subs(ddf_ddx, [X,Y], [x(1),y(1)]);
ddf_ddy_1 = subs(ddf_ddy, [X,Y], [x(1),y(1)]);
ddf_dxdy_1 = subs(ddf_dxdy, [X,Y], [x(1),y(1)]);
H = [ddf_ddx_1, ddf_dxdy_1; ddf_dxdy_1,
ddf_ddy_1];
S = inv(H);
while norm(J) > e
I = [x(i),y(i)]';
x(i+1) = I(1)-S(1,:)*J';
y(i+1) = I(2)-S(2,:)*J';
i = i+1;
J = [subs(df_dx,[X,Y], [x(i),y(i)]) subs(df_dy, [X,Y],
[x(i),y(i)])];
ddf_ddx_1 = subs(ddf_ddx, [X,Y], [x(i),y(i)]);
ddf_ddy_1 = subs(ddf_ddy, [X,Y], [x(i),y(i)]);
ddf_dxdy_1 = subs(ddf_dxdy, [X,Y], [x(i),y(i)]);
H = [ddf_ddx_1, ddf_dxdy_1; ddf_dxdy_1,
ddf_ddy_1];

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
S = inv(H);
end
Iter = 1:i;
X_coordinate = x';
Y_coordinate = y';
Iterations = Iter';
T = table(Iterations,X_coordinate,Y_coordinate);
fcontour(f, 'Fill', 'On');
hold on;
plot(x,y,'*-r');
grid on;
fprintf('Initial Objective Function Value: %d\n\
n',subs(f,[X,Y], [x(1),y(1)]));
if (norm(J) < e)
fprintf('Minimum succesfully obtained...\n\n');
end
fprintf('Number of Iterations for Convergence: %d\
n\n', i);
fprintf('Point of Minima: [%d,%d]\n\n', x(i), y(i));
Document Page
fprintf('Objective Function Minimum Value after
Optimization: %f\n\n', subs(f,[X,Y], [x(i),y(i)]));
disp(T)
23. Given the function f ( x , y )=8 x+ x2+12 y +4 y22 xy , determine the
minimum by (a) graphically, (b) analytically and (c) numerically using
Newton method written in Question 6; (d) finally use MAT LAB in-
build function: ‘fminsearch(f,x0)’ (8 points).
clc
clear
syms X Y;
f =-8.*X+X.^2+12.*Y+4*Y.^2-2.*X.*Y;
x(1) = 1;
y(1) = -2;
e = 10^(-8);
i = 1;
df_dx = diff(f, X);
df_dy = diff(f, Y);
Document Page
J = [subs(df_dx,[X,Y], [x(1),y(1)]) subs(df_dy, [X,Y],
[x(1),y(1)])];
ddf_ddx = diff(df_dx,X);
ddf_ddy = diff(df_dy,Y);
ddf_dxdy = diff(df_dx,Y);
ddf_ddx_1 = subs(ddf_ddx, [X,Y], [x(1),y(1)]);
ddf_ddy_1 = subs(ddf_ddy, [X,Y], [x(1),y(1)]);
ddf_dxdy_1 = subs(ddf_dxdy, [X,Y], [x(1),y(1)]);
H = [ddf_ddx_1, ddf_dxdy_1; ddf_dxdy_1,
ddf_ddy_1];
S = inv(H);
while norm(J) > e
I = [x(i),y(i)]';
x(i+1) = I(1)-S(1,:)*J';
y(i+1) = I(2)-S(2,:)*J';
i = i+1;
J = [subs(df_dx,[X,Y], [x(i),y(i)]) subs(df_dy, [X,Y],
[x(i),y(i)])];
ddf_ddx_1 = subs(ddf_ddx, [X,Y], [x(i),y(i)]);
ddf_ddy_1 = subs(ddf_ddy, [X,Y], [x(i),y(i)]);
ddf_dxdy_1 = subs(ddf_dxdy, [X,Y], [x(i),y(i)]);

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
H = [ddf_ddx_1, ddf_dxdy_1; ddf_dxdy_1,
ddf_ddy_1];
S = inv(H);
end
Iter = 1:i;
X_coordinate = x';
Y_coordinate = y';
Iterations = Iter';
T = table(Iterations,X_coordinate,Y_coordinate);
fcontour(f, 'Fill', 'On');
hold on;
plot(x,y,'*-r');
grid on;
fprintf('Initial Objective Function Value: %d\n\
n',subs(f,[X,Y], [x(1),y(1)]));
if (norm(J) < e)
fprintf('Minimum succesfully obtained...\n\n');
end
fprintf('Number of Iterations for Convergence: %d\
n\n', i);
Document Page
fprintf('Point of Minima: [%d,%d]\n\n', x(i), y(i));
fprintf('Objective Function Minimum Value after
Optimization: %f\n\n', subs(f,[X,Y], [x(i),y(i)]));
disp(T)
Initial Objective Function Value: 43
Minimum succesfully obtained...
Number of Iterations for Convergence: 2
Point of Minima: [3.333333e+00,-6.666667e-01]
Objective Function Minimum Value after Optimization: -17.333333
Iterations X_coordinate Y_coordinate
__________ ________________ __________________
1 1 -5
2 3.33333333333333 -0.666666666666667
Document Page
24.Seismic wave travel-time from the source to receiver (see Figure) is
calculated by
t (x )=1
V 1
x2+h1
+1
V 2
(x Rx )2+( zR h1 )2 .
Here If V 1=2700 m/s , V 2=3000 m/s, h1=40 m, xR=80 m and
zR=60 m . Perform the following calculations (5 points):
(a) Analytically calculate the 1st derivative: t ' (x )=dt /dx and
graphically find the solution of t ' (x )=0 .
(b) Apply MATLAB function fzero to t ' (x )=0 so as to find x that
gives the minimum travel-time at Receiver.
(c) Use the bisection method to find x so that seismic wave first
arrives at the receiver.
(d) Apply the Golden Search to determination of x that gives the
minimum travel-time from source to receiver.
clc
clear all
close all

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
v1=2700
v2=3000
h1=40
xr=80
zr=60
syms x
coff= coeffs((1/v1).*((x.^2+h1).^(1/2))+(1/v2).*[(xr-
x).^2+(zr-h1).^2].^(1/2))
diff=polyder([1/3000, 1/2700])
roots(diff)
fun = @f;
x0 = 2;
z = fzero(fun,x0)
%...................
clc
clear all
close all
a=input('Enter the function','s')
f=inline('a')
xa=input('enter the first value')
xb=input('enter the second value')
Document Page
er=input('enter allow error')
if f(xb)*f(xa)<0
else
fprintf('guess is incorrect')
xa=input('enter the first value of guess
interval \n')
xb=input('enter the last value of guess
interval \n')
end
for i=2:1000
xr=(xa+xb)/2
if f(xb)*f(xr)<0
xa=xr;
else
xb=xr;
end
if f(xa)*f(xr)<0
xb=xr;
else
xa=xr;
Document Page
end
xnew(1)=0;
xnew(i)=xr;
if abs(xnew(i)-xnew(i-1))/(xnew(i))<
er,break,end
end
str=['roots of eq is ',num2str(xr)]
((1/2700).*((x.^2+40).^(1/2))+(1/3000).*[(80-
x).^2+(60-h1).^2].^(1/2))
Enter the function((1/2700).*((x.^2+40).^(1/2))+(1/3000).*[(80-
x).^2+(60-h1).^2].^(1/2))
a =
((1/2700).*((x.^2+40).^(1/2))+(1/3000).*[(80-x).^2+(60-h1).^2].^(1/2))
f =
Inline function:

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
f(a) = a
enter the first value-2
xa =
-2
enter the second value1
xb =
1
enter allow error0.001
er =
1.0000e-03
xr =
Document Page
-0.5000
str =
roots of eq is -0.5
25.Complete the following sentence: trapezoidal method calculate the
area of the ______Triangle & rectangle_ that has width ___
Δxi=xi+1xi _ and high ______ f ( xi ) ___ to approach the integral in an
interval [ xi , xi+1 ] (3 points).
(a) Triangle, (b) rectangle, (c) Circle, (d) Δxi=xi+1xi , (e) xi,
(f) xi+1, (g) f (xi ) , (h) f (xi +1) , (i) [ f ( xi)+ f ( xi +1 )] /2 .
26.Given the following integrals:
I1=
6
6
x2 cos ( x )dx , I2=
0
2
x2 sin( x )dx
,
I3=
0
0 . 8
(0 .2+25 x200 x2+675 x3900 x4+ 400 x5 )dx
clc
clear all
Document Page
close all
I=@(x) ((x.^2).*cos(x))
I1=integral(I,-6,6)
p=@(x) (0.2+(25*x)-(200*x.^2)+(675*x.^3)-
(900*x.^4)+(400*x.^5))
I3=integral(I,0,0.8)
q=@(x) ((x.^2).*sin(x))
I2=integral(I,0,2)
(a) Write a M-function that applies the trapezoidal method for a
definite integral over the interval [ xa , xb ] , which are divided into n
intervals; (b) Apply the function to calculate the above integrals; (c)
use MATLAB in-built function ‘int(f,xa,xb)’ check the result (9
points).
clc
clear all
close all
I=@(x) ((x.^2).*cos(x))
I1=integral(I,-6,6)

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
p=@(x) (0.2+(25*x)-(200*x.^2)+(675*x.^3)-
(900*x.^4)+(400*x.^5))
I3=integral(I,0,0.8)
q=@(x) ((x.^2).*sin(x))
I2=integral(I,0,2)
I =
@(x)((x.^2).*cos(x))
I1 =
4.0438
p =
Document Page
@(x)(0.2+(25*x)-(200*x.^2)+(675*x.^3)-(900*x.^4)+(400*x.^5))
I3 =
0.1391
q =
@(x)((x.^2).*sin(x))
I2 =
0.1540
>>
Document Page
27.Simpsom method applies polynomial interpolations to approaching a
function f (x ) in an interval [xi, xi+1]. The polynomial may has the
general form (15 points):
f i ( x )=
k =1
n
ak xk 1 ,¿ x [ xi , xi +1 ] ¿
.
(a) Give the linear, quadratic and cubic expressions of f i ( x ) ;
(b) According to the three polynomial forms, how many points
{xk , f i( xk )} are required for the expressions? and using one
example to show how to determine the coefficients ak in [xi, xi+1];
(c) Calculate the three integrals over [xi, xi+1], i.e.
Linear Simpson: Fi=
xi
xi +1
f i( x ) dx =?
Quadratic Simpson: Fi =
xi
xi+1
f i( x )dx =?
Cubic Simpson: Fi=
xi
xi +1
f i( x ) dx =?
clc
clear all

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
close all
x=linspace(0,2,10)
y1=2*x
y2=2*x.^2
y3=2*x.^3
E1=simps(x,y1,2)
E2=simps(x,y2,2)
E2=simps(x,y3,2)
x =
Columns 1 through 6
0 0.2222 0.4444 0.6667 0.8889 1.1111
Columns 7 through 10
1.3333 1.5556 1.7778 2.0000
Document Page
y1 =
Columns 1 through 6
0 0.4444 0.8889 1.3333 1.7778 2.2222
Columns 7 through 10
2.6667 3.1111 3.5556 4.0000
y2 =
Columns 1 through 6
0 0.0988 0.3951 0.8889 1.5802 2.4691
Columns 7 through 10
3.5556 4.8395 6.3210 8.0000
Document Page
y3 =
Columns 1 through 6
0 0.0219 0.1756 0.5926 1.4047 2.7435
Columns 7 through 10
4.7407 7.5281 11.2373 16.0000
E1 =
4
E2 =
5.3333

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
E2 =
8.0012
28.Gauss integration formula over an interval [ xi , xi+1 ] is given by
Ii =
xi
xi +1
f ( x )dx =( xi +1xi )
2
k=1
n
wk f ( xk )
where wk is the weights and xk =[( xi+xi+1 )+( xi+1xi )ξk ]/2 determined
by Gauss points ξk . The following table gives the weights wk and
Gauss points
k as n=5.
Document Page
k wk
0
0.53846931010568 0.47862867048630
0.90617984593866 0.23692688505618
(a) Apply the Gauss integration formula, the table and MATLAB to
calculations of the integrals given in Question 2: I1 , I 2 & I3 ; (b) Divide
the original interval into two sections, i.e. the original integral consists
of two parts:

xa
xb
f ( x )dx =
xa
( xa+ xb )/ 2
f (x )dx +
(xa +xb )/2
xb
f ( x )dx
Apply the Gauss integration formula and the table to each part and
recalculate the three integrations I1 , I 2 & I 3 , and compare the results
with the ones obtained in (a) and Question 2 (12 points).
clc
clear all
close all
Ek=linspace(0,2,10)
0.56888888888
889
Document Page
y1=0.568888888888889*2*Ek
E1=simps(Ek,y1,2)
Ek =
Columns 1 through 6
0 0.2222 0.4444 0.6667 0.8889 1.1111
Columns 7 through 10
1.3333 1.5556 1.7778 2.0000
y1 =
Columns 1 through 6
0 0.2528 0.5057 0.7585 1.0114 1.2642

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
Columns 7 through 10
1.5170 1.7699 2.0227 2.2756
E1 =
2.2756
>>
1 out of 56
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]