Matlab polynomial interpolation

  • MATLAB
  • Thread starter kappa
  • Start date
  • #1
7
0

Main Question or Discussion Point

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?
 

Answers and Replies

  • #2
33,097
4,796
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
7
0
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
Office_Shredder
Staff Emeritus
Science Advisor
Gold Member
3,750
99
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
7
0
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
 

Related Threads for: Matlab polynomial interpolation

  • Last Post
Replies
0
Views
7K
  • Last Post
Replies
9
Views
34K
  • Last Post
Replies
0
Views
5K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
1
Views
12K
Replies
1
Views
6K
Replies
0
Views
10K
Top