Create a function with the header: function [Y] = myinterpfunction(x,y,X,method)
ID: 3834445 • Letter: C
Question
Create a function with the header:
function [Y] = myinterpfunction(x,y,X,method)
Where x and y are column vectors containing experimental data points, and X is an array. Assume that x and X are in ascending order and have unique elements. The output argument, Y, should be a vector, the same size as X, where Y(i) is the interpolation of X(i) using the method specified.
The options for method should be
‘linear’ : uses linear interpolation
‘spline’ : uses cubic spline interpolation
‘nearest’ : uses nearest neighbor interpolation
Hint1: you should use the function strcmp to compare the given method to the different options. If the output of strcmp is = 1, then the two strings are the same.
Example: if strcmp(method, ‘linear’) == 1 %the method is linear
Hint2: for the spline method, you may want to start with a matrix of zeros sized 4(n-1) x 4(n-1) - you can use the zeros(rows, columns) function for this
Your function should plot the data points (x,y) as open circles and should plot the interpolation as a black line. Be sure to include a title, legend, and axis labels.
You can use the test case and plots below to test your function:
Type the following into the command window (or into a new script)-
x = [0 0.1 0.15 0.35 0.6 0.7 0.95 1];
y = [1 0.8187 0.7408 0.4966 0.3012 0.2466 0.1496 0.1353];
X = linspace(0,1,100);
[Y] = myinterpfunction(x,y,X, ‘nearest’)
-without using matlab built in interp1 function
Explanation / Answer
#include <iostream>
using namespace std;
#define m 3
#define n 3
int main()
{
int i, j;
int x[m][n]={{10,25,33}, {21,32,43},{20,42,51}};
cout<<" 3x3 arrays' subscripts and ";
cout<<"their respective elements ";
cout<<"-------------------------- ";
// the outer for loop, reading the row by row...
for(i=0; i<m; i++)
// the inner loop, for every row, read every column by column...
for(j=0; j<n; j++)
cout<<"["<<i<<"]"<<"["<<j<<"]"<<"="<<x[i][j]<<" ";
return 0;
}
#include <iostream>
using namespace std;
#define m 4
#define n 5
int main()
{
int i, j, total = 0;
// a 4x5 or [4][5] array variable...
int q[m][n]={{4,5,6,2,12},{10,25,33,22,11},
{21,32,43,54,65},{3,2,1,5,6}};
float average;
// the outer for loop, read row by row...
for(i=0; i<m; i++)
// the inner for loop, for every row, read column by column
for(j=0; j<n; j++)
// the get the summation of the array elements.
{
// the display the array...
cout<<"q["<<i<<"]["<<j<<"] = "<<q[i][j]<<endl;
total=total + q[i][j];
}
// calculate the average, notice the simple typecast casted from int to float...
average = (float)total/(float) (m*n);
cout<<" This program will calculate the average of the";
cout<<" 4 x 5 array, which means the sum of the";
cout<<" array's element, divide the number of the";
cout<<" array's element....";
cout<<" Processing.... PLEASE WAIT ";
// display the average
cout<<"Average = "<<total<<"/"<<m*n<<endl;
cout<<" The Average = "<<average<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.