1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab polynomial interpolation

  1. Jun 15, 2011 #1
    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?
     
  2. jcsd
  3. Jun 15, 2011 #2

    Mark44

    Staff: Mentor

    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);
     
  4. Jun 15, 2011 #3
    doesn t work it says something that matrix must be square
     
  5. Jun 15, 2011 #4

    Office_Shredder

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  6. Jun 15, 2011 #5
    thanks it worked
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab polynomial interpolation
  1. Interpolation method (Replies: 2)

Loading...