Fit data with function in MATLAB

Click For Summary

Discussion Overview

The discussion revolves around fitting data points using MATLAB, specifically focusing on polynomial fitting and the challenges associated with fitting non-polynomial functions. Participants explore various MATLAB commands and functions for data fitting.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant provides a simple example of data points and asks how to fit them using a linear function in MATLAB.
  • Another participant suggests using the 'cftool' command for curve fitting.
  • A participant mentions using the 'polyfit' function with the syntax polyfit[x, y, 1] for linear fitting and refers to the documentation for further details.
  • There is a query about fitting data to a non-polynomial function of the form y=(x-a)^n + b, and the participant seeks guidance on how to apply 'polyfit' in this case.
  • A response clarifies that 'polyfit' is not suitable for non-polynomial functions and suggests using a polynomial fit instead, while also discussing the challenges of determining the exponent 'n' in such fitting problems.

Areas of Agreement / Disagreement

Participants express differing views on the applicability of 'polyfit' for non-polynomial functions, indicating a lack of consensus on how to approach fitting such functions in MATLAB.

Contextual Notes

Participants mention the limitations of 'polyfit' in fitting non-polynomial functions and the complexities involved in solving for exponents in least squares fitting problems.

Who May Find This Useful

Users interested in data fitting techniques in MATLAB, particularly those dealing with polynomial and non-polynomial functions.

quin
Messages
50
Reaction score
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
cftool
 
x=[1 2 3 4];
y=[2 4 6 8];
polyfit[x, y, 1]

also

doc polyfit
 
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:
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K