MATLAB Matlab polynomial interpolation

AI Thread Summary
The discussion revolves around a MATLAB coding issue related to polynomial interpolation using the function (1-6*x^2)^-1. The user encounters an error when trying to compute y using this function due to improper syntax. The solution involves correcting the exponentiation syntax by using element-wise operations. Specifically, changing x^2 to x.^2 resolves the issue, as x^2 attempts to multiply the matrix by itself rather than squaring each element individually. After implementing this fix, the user successfully executes the polynomial interpolation without further errors.
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

Back
Top