MATLAB Interpolation, Curve Fitting, and Optimization Problems

Verified

Added on  2023/05/28

|56
|6920
|97
Homework Assignment
AI Summary
This assignment explores various interpolation techniques, curve fitting, and optimization methods using MATLAB. The solution begins with linear and quadratic interpolation using given points and their corresponding function values. It then delves into cubic interpolation applied to density and velocity measurements, including discussions on extrapolation. The assignment further utilizes MATLAB's polyfit and polyval functions to find the best-fitting polynomials and estimate values, comparing the results with cubic interpolation. Lagrange interpolation is implemented and compared to the initial interpolation results. Additionally, the assignment covers Chebyshev interpolation and its error analysis, followed by a comparison with Lagrange interpolation. The document also includes explanations of polynomial and Lagrange interpolation, as well as spline interpolation. Furthermore, it addresses the concepts of minima, maxima, and optimization, including the application of the bisection method to find the root of a function and the use of MATLAB to find the optimum of a given function. Finally, it discusses the relationships between derivatives and the nature of maxima and minima.
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
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
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
tabler-icon-diamond-filled.svg

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
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
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')
tabler-icon-diamond-filled.svg

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
chevron_up_icon
1 out of 56
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]