Matlab polynomial interpolation

In summary, the individual is trying to perform polynomial interpolation using the function (1-6*x^2)^-1 on 21 equidistant points. They have created a function using Matlab and are receiving an error when trying to use the function with x^2. The expert suggests using x.^2 instead to square every element in x.
  • #1
kappa
7
0
I have this function (1-6*x^2)^-1 and i want to polynomial interpolation (lagrange and spline) in 21 equidistant points [-1,1]
I made this function

x =linspace(-1,1,21);
y = (1-6*x^2)^-1;

z=[-1:0.01:1]
c=polyfit(x,y,20)
p=polyval(c,z)
s=spline(x,y,z)
plot(z,(1-6*x^2)^-1, z, p, z, s);

and I receive error at y = (1-6*x^2)^-1;
if I use a function with x only instead of x^2 it works.
How can I fix it?
 
Physics news on Phys.org
  • #2
kappa said:
I have this function (1-6*x^2)^-1 and i want to polynomial interpolation (lagrange and spline) in 21 equidistant points [-1,1]
I made this function

x =linspace(-1,1,21);
y = (1-6*x^2)^-1;

z=[-1:0.01:1]
c=polyfit(x,y,20)
p=polyval(c,z)
s=spline(x,y,z)
plot(z,(1-6*x^2)^-1, z, p, z, s);

and I receive error at y = (1-6*x^2)^-1;
if I use a function with x only instead of x^2 it works.
How can I fix it?
I don't have much experience using matlab, but your problem might be that you need parentheses around your exponent, like so.
y = (1-6*x^2)^(-1);
 
  • #3
Mark44 said:
I don't have much experience using matlab, but your problem might be that you need parentheses around your exponent, like so.
y = (1-6*x^2)^(-1);

doesn t work it says something that matrix must be square
 
  • #4
It needs to be x.^2 not x^2

x^2 is literally taking the matrix x and multiplying it by itself (which you can't), x.^2 is squaring every element in x
 
  • #5
Office_Shredder said:
It needs to be x.^2 not x^2

x^2 is literally taking the matrix x and multiplying it by itself (which you can't), x.^2 is squaring every element in x

thanks it worked
 

FAQ: Matlab polynomial interpolation

1. What is polynomial interpolation in Matlab?

Polynomial interpolation in Matlab is a method for fitting a polynomial curve to a set of data points. It involves finding a polynomial function that passes through all the given data points and can be used to estimate the value of the function at any point within the given range.

2. How do I perform polynomial interpolation in Matlab?

To perform polynomial interpolation in Matlab, you can use the "polyfit" function. This function takes in two input arguments: the data points and the degree of the desired polynomial. It returns the coefficients of the polynomial, which can then be used to construct the polynomial function using the "polyval" function.

3. What is the difference between polynomial interpolation and polynomial regression in Matlab?

The main difference between polynomial interpolation and polynomial regression in Matlab is the purpose of their use. Polynomial interpolation is used to fit a polynomial curve to a set of given data points, while polynomial regression is used to find a polynomial function that best fits a given dataset by minimizing the sum of squared errors.

4. Can I perform polynomial interpolation on non-uniformly spaced data in Matlab?

Yes, you can perform polynomial interpolation on non-uniformly spaced data in Matlab by using the "interp1" function. This function allows you to specify the x and y coordinates of the data points and the desired output points, and it will use polynomial interpolation to estimate the value at the output points.

5. Is polynomial interpolation in Matlab affected by outliers in the data?

Yes, polynomial interpolation in Matlab can be affected by outliers in the data. Outliers can significantly alter the shape of the polynomial curve, leading to inaccurate results. It is important to remove or address outliers before performing polynomial interpolation to ensure accurate results.

Similar threads

Replies
2
Views
3K
Replies
1
Views
2K
Replies
2
Views
3K
Replies
3
Views
1K
Replies
1
Views
826
Back
Top