Write a script that finds the intermediates points based on the interpolations u
ID: 3697338 • Letter: W
Question
Write a script that finds the intermediates points based on the interpolations using an initial data, based on different methods. The initial data is as follows: X=0:2*pi/10:2*pi; Y=cos(x) This means that the data is coming from a cosine function (the actual correlation between x and y is demonstrated by a cosine function). The new Xdata points are as follows: Xnew=0:2*pi/50:2*pi; - Find the corresponding Ynew points using 4 different interpolation techniques - Create a figure window divided into 4 windows. - Plot the initial data points with asterisk (or any other types of symbols). - Plot the results from interpolation for the new data points at the same figure window (plot a line for every scheme). - Tile every plot with the corresponding interpolation technique (nearest, linear, spline, cubic). Create the following data set in a ".daf" or excel file. The data represents the census years from 1999 to 2013 and the corresponding US population in millions of people. Write a script that Reads/loads the data file. Calculates the US population for a year in between the maximum and minimum years mentioned in the data set (you need to use max and min function to find the boundary of the years included in your table).Explanation / Answer
x = 0:2*pi/10:2*pi;
y = cos(x);
xq = 0:pi/16:2*pi;
figure
vq1 = interp1(x,y,xq);
plot(x,y,'o',xq,vq1,':.');
xlim([0 2*pi]);
title('(Default) Linear Interpolation');
figure
vq2 = interp1(x,v,xq,'spline');
plot(x,v,'o',xq,vq2,':.');
xlim([0 2*pi]);
title('Spline Interpolation');
vq2 = interp1(x,v,xq,'nearest');
ynew calculation using different interpolation techniques
Linear Interpolation
Given a data set, we can perform linear interpolation between each pair of data points to any desired resolution using the MATLAB function interp1
• Understanding how linear interpolation works is still very important if you are writing a custom algorithm or want to check the results of a MATLAB calculation
• The function
y_new = interp1(x,y,x_new,’linear’);
returns a vector y_new of the same size as x_new – The vectors x and y contain the raw data – If the fourth function argument, ‘linear’, is omitted interp1 defaults to linear interpolation – It is assumed that the x_new vector has the same minimum and maximum values as x, but in x_new the points can be spaced to any desired resolution – The vector y_new contains the linearly interpolated values corresponding to x_new, and of course will match the original y vector values at the corresponding x values
Cubic-Spline Interpolation
• As we can see from the previous example, linear interpolation produces a rather jagged result if the data points are not closely spaced and don’t lie in a straight line
• An improved interpolation procedure is to replace the straight line connecting the data points with a third degree polynomial
• The third degree polynomial is of the form – As with linear interpolation a new set of coefficients must be used for each interval between the available data points – This polynomial is known as a cubic spline function – It is very special since the coefficients are chosen to give a smooth transition as we pass from one point and the next – This smooth behavior is accomplished by computing the polynomial coefficients for each interval using more than just the adjacent data points (recall linear interpolation uses just the interval end points to determine and )
• The MATLAB function interp1 implements cubic spline interpolation by simply changing the fourth argument from ‘linear’ to ‘spline’
The interp1 command is a MATLAB M-file. The 'nearest' and 'linear' methods have straightforward implementations.
For the 'spline' method, interp1 calls a function spline that uses the functions ppval, mkpp, and unmkpp. These routines form a small suite of functions for working with piecewise polynomials. spline uses them to perform the cubic spline interpolation. For access to more advanced features, see the spline reference page, the M-file help for these functions, and the Spline Toolbox.
For the 'pchip' and 'cubic' methods, interp1 calls a function pchip that performs piecewise cubic interpolation within the vectors x and y. This method preserves monotonicity and the shape of the data.
Code:
x = 0:2*pi/10:2*pi;
y = cos(x);
X_new=0:2*pi/50:2*pi;
y_newlinear = interp1(x,y,X_new,'linear')
y_newspline= interp1(x,y,X_new,'spline')
y_newcubic= interp1(x,y,X_new,'cubic')
y_newpchip= interp1(x,y,X_new,'pchip')
Output::
ynew calcutation
y_newlinear =
Columns 1 through 16
1.0000 0.9618 0.9236 0.8854 0.8472 0.8090 0.7090 0.6090 0.5090 0.4090 0.3090 0.1854 0.0618 -0.0618 -0.1854 -0.3090
Columns 17 through 32
-0.4090 -0.5090 -0.6090 -0.7090 -0.8090 -0.8472 -0.8854 -0.9236 -0.9618 -1.0000 -0.9618 -0.9236 -0.8854 -0.8472 -0.8090 -0.7090
Columns 33 through 48
-0.6090 -0.5090 -0.4090 -0.3090 -0.1854 -0.0618 0.0618 0.1854 0.3090 0.4090 0.5090 0.6090 0.7090 0.8090 0.8472 0.8854
Columns 49 through 51
0.9236 0.9618 1.0000
y_newspline =
Columns 1 through 16
1.0000 0.9950 0.9720 0.9324 0.8776 0.8090 0.7281 0.6362 0.5348 0.4252 0.3090 0.1876 0.0631 -0.0626 -0.1873 -0.3090
Columns 17 through 32
-0.4257 -0.5356 -0.6373 -0.7289 -0.8090 -0.8761 -0.9294 -0.9682 -0.9919 -1.0000 -0.9919 -0.9682 -0.9294 -0.8761 -0.8090 -0.7289
Columns 33 through 48
-0.6373 -0.5356 -0.4257 -0.3090 -0.1873 -0.0626 0.0631 0.1876 0.3090 0.4252 0.5348 0.6362 0.7281 0.8090 0.8776 0.9324
Columns 49 through 51
0.9720 0.9950 1.0000
y_newcubic =
Columns 1 through 16
1.0000 0.9843 0.9541 0.9125 0.8631 0.8090 0.7393 0.6463 0.5381 0.4229 0.3090 0.1917 0.0649 -0.0649 -0.1917 -0.3090
Columns 17 through 32
-0.4229 -0.5381 -0.6463 -0.7393 -0.8090 -0.8643 -0.9160 -0.9593 -0.9890 -1.0000 -0.9890 -0.9593 -0.9160 -0.8643 -0.8090 -0.7393
Columns 33 through 48
-0.6463 -0.5381 -0.4229 -0.3090 -0.1917 -0.0649 0.0649 0.1917 0.3090 0.4229 0.5381 0.6463 0.7393 0.8090 0.8631 0.9125
Columns 49 through 51
0.9541 0.9843 1.0000
y_newpchip =
Columns 1 through 16
1.0000 0.9843 0.9541 0.9125 0.8631 0.8090 0.7393 0.6463 0.5381 0.4229 0.3090 0.1917 0.0649 -0.0649 -0.1917 -0.3090
Columns 17 through 32
-0.4229 -0.5381 -0.6463 -0.7393 -0.8090 -0.8643 -0.9160 -0.9593 -0.9890 -1.0000 -0.9890 -0.9593 -0.9160 -0.8643 -0.8090 -0.7393
Columns 33 through 48
-0.6463 -0.5381 -0.4229 -0.3090 -0.1917 -0.0649 0.0649 0.1917 0.3090 0.4229 0.5381 0.6463 0.7393 0.8090 0.8631 0.9125
Columns 49 through 51
0.9541 0.9843 1.0000c
Create a figure window divided into four windows
Code:
x = 0:2*pi/10:2*pi;
y = cos(x);
X_new=0:2*pi/50:2*pi;
y_newlinear = interp1(x,y,X_new,'linear')
y_newspline= interp1(x,y,X_new,'spline')
y_newcubic= interp1(x,y,X_new,'cubic')
y_newpnearest= interp1(x,y,X_new,'nearest')
figure
plot(x,y,'*',X_new,y_newlinear)
figure
plot(x,y,'*',X_new,y_newspline)
figure
plot(x,y,'*',X_new,y_newcubic)
figure
plot(x,y,'*',X_new,y_newpnearest)
Dispaying all are one window
code
x = 0:2*pi/10:2*pi;
y = cos(x);
X_new=0:2*pi/50:2*pi;
y_newlinear = interp1(x,y,X_new,'linear')
y_newspline= interp1(x,y,X_new,'spline')
y_newcubic= interp1(x,y,X_new,'cubic')
y_newpchip= interp1(x,y,X_new,'pchip')
% plot(x,y,'o',X_new,y_new)
figure
subplot(2,2,1)
plot(y_newlinear,'*')
title('Subplot liear')
subplot(2,2,2)
plot(y_newspline)
title('Subplot spline')
subplot(2,2,3)
plot(y_newcubic)
title('Subplot cubic')
subplot(2,2,4)
plot(y_newpchip)
title('Subplot pchip')
plot the initial points with asteric
x = 0:2*pi/10:2*pi;
y = cos(x);
X_new=0:2*pi/50:2*pi;
y_newlinear = interp1(x,y,X_new,'linear')
y_newspline= interp1(x,y,X_new,'spline')
y_newcubic= interp1(x,y,X_new,'cubic')
y_newpnearest= interp1(x,y,X_new,'nearest')
figure
subplot(2,2,1)
plot(x,y,'*',X_new,y_newlinear)
subplot(2,2,2)
plot(x,y,'*',X_new,y_newspline)
subplot(2,2,3)
plot(x,y,'*',X_new,y_newcubic)
subplot(2,2,4)
plot(x,y,'*',X_new,y_newpnearest)
or
x = 0:2*pi/10:2*pi;
y = cos(x);
figure
subplot(2,2,1)
plot(x,y,'*')
subplot(2,2,2)
plot(x,y,'*')
subplot(2,2,3)
plot(x,y,'*')
subplot(2,2,4)
plot(x,y,'*')
tiling code:
x = 0:2*pi/10:2*pi;
y = cos(x);
X_new=0:2*pi/50:2*pi;
y_newlinear = interp1(x,y,X_new,'linear')
y_newspline= interp1(x,y,X_new,'spline')
y_newcubic= interp1(x,y,X_new,'cubic')
y_newpnearest= interp1(x,y,X_new,'nearest')
figure
plot(x,y,'*',X_new,y_newlinear)
figure
plot(x,y,'*',X_new,y_newspline)
figure
plot(x,y,'*',X_new,y_newcubic)
figure
plot(x,y,'*',X_new,y_newpnearest)
imagetile(figure1,figure2,figure3,figure4)
2)answer
creation of us.dat file and reading calculation of minimum and maximum values in that boundary
% first read two csv files using
% y=csvread('year.csv')
% x=csvread('population.csv')
% % save('us1.dat','x','y')
load('us.dat') %reading the data
plot(x,y)
% calculation of minimum and maximum values in that boundary
indexmin = find(min(y) == y);
xmin = x(indexmin);
ymin = y(indexmin);
indexmax = find(max(y) == y);
xmax = x(indexmax);
ymax = y(indexmax);
strmin = ['Minimum = ',num2str(ymin)];
text(xmin,ymin,strmin,'HorizontalAlignment','left');
strmax = ['Maximum = ',num2str(ymax)];
text(xmax,ymax,strmax,'HorizontalAlignment','right');
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.