0% found this document useful (0 votes)
16 views

Matlab Curve Fitting

Uploaded by

mansour
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Matlab Curve Fitting

Uploaded by

mansour
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Session 8: Polynomials and Data Fitting

%% Polynomials and finding roots P = polyfit(x, y, 3); % n=4,5,10


% Over-fit
% P = an*x^n + ... a1*x + a0
xf = 0:0.1:9;
A = [1 -5 -3 6]; yf = polyval(P, xf);
% x^3 -5x^2 - 3x + 6
plot(x, y, '--ro', xf, yf)
P0 = roots(A)
%% Fitting using figure
%% Derive polynomial by having
roots x = [1 2 4 5 6.5 8];
y = [8 2 -1 -7 0.5 -3];
P0 = [1 -2 3];
P = poly(P0) plot(x, y, '--ro')

% Evaluting Polynomials: % figure > tools > basic fitting


Pv = polyval(P, 2); %@3 = 0, of
course %% Cubic Hermite vs Cubic Spline
(shape-perseerving)
%% Derivative and Integration
% Hermite: P(x0), P'(x0) are
Pd = polyder(P) % Derivative continous
Pi = polyint(P) % integration % Spline: P(x0), P'(x0) and
P''(x0) are continous
%% conv: multiplying two
polynomials %% Overfitting
% cubic vs 5th Poly: -> Overfitting
T = conv([1 2], [2 3 4]) (Compare Norm of residuals)
%% deconv: dividing two polynomials % How to check Overfitting:
split data in two groups:
[q, r] = deconv([6 7 2],[3 -1]) 1) train, 2) test
% q: quotient, r: remainder
%% Fitting using cftool ----------
===========================
cftool
%% Fitting
% 1] Polynomials %% Fitting using code ------------
% 2] Fitting using UI: plot-figure, cftool
% 3] Fitting using code
FT = fittype('a + b*sin(x)',
%% [1] Ployfit: data fitting 'independent',{'x'});

x = [1 2 4 5 6.5 8]; FitM = fit(x',y', FT,


y = [8 2 -1 -7 0.5 -3]; 'StartPoint', [0,0])

plot(x, y, '--ro') plot(FitM, x,y, 'o');

%%-------------------------------

MATLAB Course By: Mansour Torabi

You might also like