Matlab polynomial interpolation

Click For Summary

Discussion Overview

The discussion revolves around polynomial interpolation using MATLAB, specifically focusing on the function (1-6*x^2)^-1 and the challenges faced when implementing Lagrange and spline interpolation at 21 equidistant points in the interval [-1, 1].

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes an error encountered when trying to compute y = (1-6*x^2)^-1, suggesting that the issue may relate to the use of exponentiation without proper parentheses.
  • Another participant proposes that using parentheses around the exponent might solve the problem, suggesting y = (1-6*x^2)^(-1).
  • A different participant points out that the error persists, indicating a matrix must be square, implying a misunderstanding of matrix operations in MATLAB.
  • Another participant clarifies that the correct syntax should use element-wise operations, specifically recommending x.^2 instead of x^2 to avoid matrix multiplication errors.
  • A later reply confirms that using x.^2 resolved the issue for the original poster.

Areas of Agreement / Disagreement

Participants generally agree on the need for element-wise operations in MATLAB, but there is some initial confusion regarding the correct syntax and the nature of the errors encountered.

Contextual Notes

Limitations: The discussion does not resolve the underlying mathematical properties of the function (1-6*x^2)^-1 or the implications of polynomial interpolation in this context.

kappa
Messages
7
Reaction score
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
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);
 
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
 
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
 
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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
7K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K