Fit data with function in MATLAB

In summary: However, if you are comfortable with the concept of solving systems of linear equations, then you can often get very close to a solution by using a computer algebra system such as Mathworks Maxima or Maple.
  • #1
quin
50
0
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
y=a*x+b
(here I know that the answer is a=2 and b=0)
what should I type in MATLAB command window?

thanks
 
Physics news on Phys.org
  • #2
cftool
 
  • #3
x=[1 2 3 4];
y=[2 4 6 8];
polyfit[x, y, 1]

also

doc polyfit
 
  • #4
polyfit

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?

thanks
 
Last edited:
  • #5
quin said:
and if I want to fit my points with such a finction:
y=(x-a)^n + b
what should I type in polyfit?

Since that isn't a polynomial you don't type anything into polyfit.

polyfit[x,y,3]
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.
 

1. How do I fit data with a function in MATLAB?

To fit data with a function in MATLAB, you can use the fit function. This function takes in the data points as well as the desired function and returns the parameters for the best-fit curve.

2. What types of functions can I use to fit my data in MATLAB?

MATLAB allows you to fit data with various types of functions, including polynomial, exponential, logarithmic, and trigonometric functions. You can also define your custom function and use it to fit your data.

3. How can I visualize the fit between my data and the function in MATLAB?

You can use the plot function to plot your data points and the fitted curve on the same graph. This will allow you to visually assess the fit and make any necessary adjustments to the function or data points.

4. Can I fit data with multiple variables in MATLAB?

Yes, MATLAB allows you to fit data with multiple variables by using the fit function with multiple input arguments. You can also use the lsqcurvefit function to fit data with multiple variables.

5. How can I evaluate the goodness of fit for my data and function in MATLAB?

MATLAB provides various tools for evaluating the goodness of fit, including the rsquare function, which calculates the coefficient of determination (R-squared value). You can also visually assess the fit by plotting the residuals (difference between the data points and the fitted curve) and ensuring they are randomly distributed around zero.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
684
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
803
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
940
Back
Top