Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fit data with function in MATLAB

  1. Jun 29, 2013 #1
    Dear users
    I have some points with coordinate x and y
    forexample :
    x=[1 2 3 4]
    and y=[2 4 6 8]
    and then I want to fit these 4 pair of points with some function
    for example
    (here I know that the answer is a=2 and b=0)
    what should I type in matlab command window?

  2. jcsd
  3. Jul 1, 2013 #2
  4. Jul 1, 2013 #3
    x=[1 2 3 4];
    y=[2 4 6 8];
    polyfit[x, y, 1]


    doc polyfit
  5. Jul 2, 2013 #4

    and if I want to fit my points with such a finction:
    y=(x-a)^n + b
    what should I type in polyfit?
    I mean I have some x and y values but I find the relation between x and y in a way that I have a function like y=(x-a)^n + b
    and then I want to find the exact value of a,b, n?

    Last edited: Jul 2, 2013
  6. Jul 2, 2013 #5
    Since that isn't a polynomial you don't type anything into polyfit.

    is going to find the values a,b,c,d for a*x^3+b*x^2+c*x+d to fit your data in a least squared sense.

    You could go back to an old style calculus text that shows how least squared fitting is derived from taking the difference between the function and your data point, squaring that, summing that over all your data, differentiating with respect to each of your unknown coefficient variables, setting all those results equal to zero and then solving for all the coefficient variables.

    Once you clearly understand how to do that then you can try to use Matlab to do some of the calculations for you.

    In my experience, solving for the exponent in a least squared fit problem, n in your example, often runs into problems and it is difficult or impossible to find a solution.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Fit data with function in MATLAB