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.
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 clearall closeall 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') 11.11.21.31.41.51.61.71.81.92 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.
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 clearall closeall 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')
11.21.41.61.822.22.42.62.83 2 2.5 3 3.5 4 4.5 3.Giventhefollowingtableshowingthedensityandvelocity 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.32.52.73.2 Velocity (km/s)5.04.24.85.5 clc;clearall; closeall 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)
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 functionf=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;clearall; closeall 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,'*')
22.22.42.62.833.23.43.63.84 -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 clearall closeall 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)
fp=interp_lagrange(x,f,x1) length(fp) plot(x1,fp) functionfp=interp_lagrange(x,f,xp) n=length(x); m=length(f); L=length(xp); ifn~=m,error('x and f must have same length'), end fork=1:L fp(k)=0; fori=1:n p=f(i) forj=1:n ifj~=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.
fp(k)=fp(k)+p; end end 1.11.21.31.41.51.61.71.81.9 3 3.2 3.4 3.6 3.8 4 1.11.21.31.41.51.61.71.81.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 clearall closeall 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)
plot(x1,y1,'r') subplot(2,1,2) fp=interp_lagrange(x,f,x1) length(fp) plot(x1,fp) 11.21.41.61.822.22.42.62.83 2 2.5 3 3.5 4 4.5 11.21.41.61.822.22.42.62.83 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 12345678910 f-1- 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 1111111111 Use the Lagrange function given below to (a) perform interpolations for the points ofxp=linspace(-10,10,40) and plot the result; (b) calculate the errors at all the points ofxpfor 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
-1-0.8-0.6-0.4-0.200.20.40.60.81 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 8.nChebyshev points in an interval[xa,xb]can be found by the following three steps: (1) use the radio ofR=(xb−xa)/2and draw the half circle at the middle point(xa+xb)/2(see Fig.1); (2) divideinto (n-1) equal angles: = /(n-1); (3) the x-coordinates of the nodes on the half circle are Chebyshev points: xi=xa+xb 2+R⋅cos(π−(i−1)Δθ),¿(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 functionf(x)=sign(x)at the points:xp=linspace(- 10,10,40); (c) calculate the errors of the interpolation at the pointsxp, plot the error curve and compare with the result of Question 7 (6 points). Fig.1
-10-8-6-4-20246810 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 9.Explainthedifferencesbetweenpolynomialinterpolationand 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:
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.Givennpoints¿¿, wherefi,fi ' andfi '' are the function value and the 1stand 2ndderivations at the pointxi. Ifp(x), L(x),S(x) andH(x) represent the cubic polynomial, Lagrange, Spline and Hermit interpolations, identify the following equations are true or false (12 points): (a)p(xi)=fi, (True) (b)p'(xi)=fi ' ,(false) (c)p''(xi)=fi '' ;(false) (d)L(xi)=fi, (True) (e)L'(xi)=fi ' , (false)(f)L''(xi)=fi '' ;( false) (g)S(xi)=fi, (True) (h)S'(xi)=fi ' , (false) (i)S''(xi)=fi '' ;(false ) (j)H(xi)=fi, (True)(k)H'(xi)=fi ' , (false) (l)H''(xi)=fi '' (false) 12.As number of pointsnincreases,___polynomial____________ and ____________Lagrange__interpolations have oscillation problem
thatmaycauselargeerrors;_______Spline_____and ________Hermit___interpolationsremovetheoscillationby 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,yj) , wherenxandnyare the total numbers of the points in thex- andy- direction, respectively. (a) Find the expressions ofli(x)andlj(y)as n3=3andny=3,i.e.x=[x1,x2,x3]andy=[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
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 localmaximaandglobalmaximuminthefollowingdiagram(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
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
16.Explain why the second derivativesf''(x¿)>0at the minimax¿ and f''(x¿)<0at the maximax¿ (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*)Δxk/2!] If k=even or 2 Δx2= positive Sof(x¿±Δx)≥f(x*)
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
17.Given the functionf(x)=−x2+8x−12, find its optimum by hand and using MATLAB respectively, and show the minimum or maximum (4 points). clc clearall closeall 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 iffroot2>=0 fprintf('Minima at %d',r) else
fprintf('Mixima at %d',r) end f = @(x)(-x.^2)+(8*x)-12 p = -18-12 f1 = -28
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
-2 Mixima at 4>> 18.Explain why the minimization of a functionf(x)can be applied to find the maxima of a functionf(x)(2 points). If our point is maxima, we can that this slope starts offpositive, 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 functionf(x)in an interval[xa,xb], and (b) then write a M-function of the method; in addition, (c) apply the M-
function to find minimum off(x)=3+6x+5x2+3x3+4x4 in x [-2,1] (10 points). clc clearall closeall 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') iff(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 fori=2:1000 xr=(xa+xb)/2 iff(xb)*f(xr)<0
xa=xr; else xb=xr; end iff(xa)*f(xr)<0 xb=xr; else xa=xr; end xnew(1)=0; xnew(i)=xr; ifabs(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.
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 =
-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 functionf(x)inx∈[xa,xb], and then (b) write a M-function of the method;In addition, (c) apply the M- functiontofindtheminimaofthefunction: f(x)=3+6x+5x2+3x3+4x4 inx [-2,1] (10 points). function [x,fx,er,it]=goldmin(f,xa,xb,es,maxit,varargin) ifnargin<3 error('three value required');
end ifnargin<4|isempty(er) es=0.0001 end ifnargin<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; iff(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
it=it+1; ifxo~=0 er=(2-phi)*abs((xa-xb)/xo)*100; end ifer<=es| it>=maxit,break,end end x=xo fx=f(xo,varargin{:}); clc clearall closeall 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 =
1.0000e-04 maxit = 50 phi = 1.6180 it = 0 x = 5.8540
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.
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
22.According to the flowchart shown below and complete the M-function of the Newton method to find the minima (10 points): clc clear symsXY; 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)])];
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'); holdon; plot(x,y,'*-r'); gridon; 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));
fprintf('Objective Function Minimum Value after Optimization: %f\n\n', subs(f,[X,Y], [x(i),y(i)])); disp(T) 23.Given the functionf(x,y)=−8x+x2+12y+4y2−2xy, 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 symsXY; 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);
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'); holdon; plot(x,y,'*-r'); gridon; 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)); 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 IterationsX_coordinateY_coordinate ____________________________________________ 11-5 23.33333333333333-0.666666666666667
24.Seismic wave travel-time from the source to receiver (see Figure) is calculated by t(x)=1 V1 √x2+h1 +1 V2 √(xR−x)2+(zR−h1)2. HereIfV1=2700m/s,V2=3000m/s,h1=40m,xR=80mand zR=60m.Perform the following calculations (5 points): (a)Analyticallycalculatethe1stderivative:t'(x)=dt/dxand graphically find the solution oft'(x)=0. (b)Apply MATLAB functionfzerotot'(x)=0so as to findx that givesthe minimum travel-time at Receiver. (c)Use the bisection method to findxso that seismic wave first arrives at the receiver. (d)Apply the Golden Search to determination ofxthat gives the minimum travel-time from source to receiver. clc clearall closeall
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
v1=2700 v2=3000 h1=40 xr=80 zr=60 symsx 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 clearall closeall 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') iff(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 fori=2:1000 xr=(xa+xb)/2 iff(xb)*f(xr)<0 xa=xr; else xb=xr; end iff(xa)*f(xr)<0 xb=xr; else xa=xr;
end xnew(1)=0; xnew(i)=xr; ifabs(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)) Enterthefunction((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.
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 =
-0.5000 str = roots of eq is -0.5 25.Complete the following sentence: trapezoidal method calculate the areaofthe______Triangle&rectangle_thathaswidth___ Δxi=xi+1−xi_ 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+1−xi, (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 x2cos(x)dx,I2=∫ 0 2 x2sin(x)dx , I3=∫ 0 0.8 (0.2+25x−200x2+675x3−900x4+400x5)dx clc clearall
closeall 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 inton 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 clearall closeall 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
27.Simpsom method applies polynomial interpolations to approaching a functionf(x)in an interval [xi,xi+1]. The polynomial may has the general form (15 points): fi(x)=∑ k=1 n akxk−1,¿x∈[xi,xi+1]¿ . (a)Give the linear, quadratic and cubic expressions offi(x); (b)Accordingtothethreepolynomialforms,howmanypoints {xk,fi(xk)}arerequiredfortheexpressions?andusing one example to show how to determine the coefficientsakin [xi,xi+1]; (c)Calculate the three integrals over [xi,xi+1], i.e. Linear Simpson:Fi=∫ xi xi+1 fi(x)dx=? Quadratic Simpson:Fi=∫ xi xi+1 fi(x)dx=? Cubic Simpson:Fi=∫ xi xi+1 fi(x)dx=? clc clearall
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
E2 = 8.0012 28.Gauss integration formula over an interval [xi,xi+1] is given by Ii=∫ xi xi+1 f(x)dx=(xi+1−xi) 2∑ k=1 n wkf(xk) wherewkis the weights andxk=[(xi+xi+1)+(xi+1−xi)ξk]/2determined by Gauss pointsξk. The following table gives the weightswkand Gauss points kas n=5.
kwk 0 0.538469310105680.47862867048630 0.906179845938660.23692688505618 (a) Apply the Gauss integration formula, the table and MATLAB to calculations of the integrals given in Question 2:I1,I2&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 integrationsI1,I2&I3, and compare the results with the ones obtained in (a) and Question 2 (12 points). clc clearall closeall Ek=linspace(0,2,10) 0.56888888888 889
y1=0.568888888888889*2*Ek E1=simps(Ek,y1,2) Ek = Columns 1 through 6 00.22220.44440.66670.88891.1111 Columns 7 through 10 1.33331.55561.77782.0000 y1 = Columns 1 through 6 00.25280.50570.75851.01141.2642
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.