Vous êtes sur la page 1sur 6

CURVE FITTING Polynomials that do not necessarily pass through any of the points:

Curve fitting with polynomials is done in MATLAB with the polyfit functions, which uses the least squares method. p = polyfit(x,y,n) where: p is the vector of the coefficients of the polynomial that fits the data. x is a vector with the horizontal coordinate of the data points (independent variable). y is a vector with the vertical coordinate of the data points (dependent variable). n is the degree of the polynomial.

Curve Fitting with Functions other than Polynomials


Other forms: y=bxm (power function) y=bemx or y=b10mx (exponential function) y=mln(x) + b or y=m log(x) + b (logarithmic function) y= 1/mx + b (reciprocal function) Note: rewrite the functions in a form y=mx + b ln(y) = mln(x) + lnb ln(y)=mx + ln (b) or log (y)=mx + log(b) 1/y = mx + b

Function polyfit function form power y=bxm p=polyfit(log(x),log(y),1) exponential y=bemx p=polyfit (x,log(y), 1) y=b10mx p=polyfit(x, log10(y),1) logarithmic y=mln(x) + b p=polyfit(log(x),y,1) y=m log(x) + b p=polyfit(log10(x),y,1) reciprocal y=1/(mx+b) p=polyfit(x,1./y,1) The result of the polyfit function is assigned to p, which is a two element vector. The first element, p(1), is the constant m, and the second element is b for the logarithmic and reciprocal functions, ln(b) or log(b) for the exponential function and ln(b) for the power function (b=ep(2) or b=10p(2) for the exponential function and b=ep(2) for the power function).

x axis linear logarithmic linear logarithmic


linear

y axis linear logarithmic logarithmic linear

linear plot (1/y) other considerations when choosing a function are: exponential functions can not pass through the origin exponential functions can only fit data with all positively ys or all negative ys logarithmic functions can not model x=0, or negative values of x. for the power function y=0 when x=0 the reciprocal equation can not model y=0

function linear y=mx + b power y=bxm exponential y=bemx or y=b10mx logarithmic y=mln(x) + b or y=mlog(x) + b reciprocal y=1/(mx + b)

example:
The following data points are given. Determine a function w=f(t) (t is the independent variable, w is the dependent variable) with a form discussed in this section that best fits the data.
t 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0

6.0

4.83

3.70

3.15

2.41

1.83

1.49

1.21

0.96

0.73 0.64

The Plot Command


The plot command is used to create two-dimensional plots. The simplest form of the command is : plot (x,y) where x and y are vectors. PLOT WITH LOGARITHMIC AXES semilogy(x,y) plots y versus x with a log (base 10) scale for the y axis and linear scale for the x axis semilogx(x,y) plots y versus x with a log (base 10) scale for the x axis and linear scale for the y axis. loglog(x,y) plots y versus x with a log (base 10) scale for both axes

Vous aimerez peut-être aussi