Introduction To Matlab Tutorial 11
Introduction To Matlab Tutorial 11
Outline
Data Smoothing Data interpolation Correlation coefficients Curve Fitting Optimization Derivatives and integrals
Assume we measured the response in time or other input factor, for example: Reaction product as function of substrate Cell growth as function of time response factor
Our measuring device has some random noise One way to subtract the noise from the results is to smooth each data point using its close environment
span
Remark: The Span should be odd
span
Smoothed data:
smooth(x,y)
6
The Smooth Quality Is Affected By The Smooth Function And The Span
y_smooth = smooth(x,y,11,'rlowess');
Data points
y_i=interp1(x_missing,y_missing,x_i,'cubic');
10
2D Functions Interpolation
Also 2D functions can be interpolated Assume we have some data points of a 2D function
= = = = -2:.5:2; -2:.5:3; meshgrid(xx,yy); X.*exp(-X.^2-Y.^2);
xx yy [X,Y] Z
11
2D Functions Interpolation
interp2 function
xx_i = -2:.1:2; yy_i = -2:.1:3; [X_i,Y_i] = meshgrid(xx_i,yy_i); Z_i = interp2(xx,yy,Z,X_i,Y_i,'cubic'); Data points Points to interpolate figure; surf(X_i,Y_i,Z_i); hold on; plot3(X,Y,Z+0.01,'ok', 'MarkerFaceColor','r')
12
Weizmann 2010
13
Residual
Two assumptions:
The error exists only in the response data, and not in the predictor data. The errors are random and follow a normal (Gaussian) distribution with zero mean and constant variance.
14
Consider the following data: In many cases we start with computing the correlation between the variables:
cor_mat = corrcoef(x , y_p); cor = cor_mat(1,2); figure; plot(x,y_p,'b.'); xlabel('x');ylabel('y'); title(['Correlation: ' num2str(cor)]);
15
Fitting
16
poly_y_fit1 = polyfit(x,y_p,1);
poly_y_fit1 = 12.6156 X + ( -3.3890 )
y_fit1 = polyval(poly_y_fit1,x); y_fit1 = 12.6156*x-3.3890 poly_y_fit2 = polyfit(x,y_p,2); y_fit2 = polyval(poly_y_fit2,x); poly_y_fit3 = polyfit(x,y_p,3); y_fit3 = polyval(poly_y_fit3,x);
mean((y_fit1-y_p).^2)
17
18
What about fitting a Curve with a linear function of several variables? Can we put constraints on the coefficients values?
c1 x1 c2 x2 c3 x3 y
Weizmann 2010 19
For this type of problems (and much more) lets learn the optimization toolbox
http://www.mathworks.com/products/optimization/description1.html
Weizmann 2010 20
Optimization Toolbox
Unconstrained nonlinear minimization Constrained nonlinear minimization, including goal attainment problems and minimax problems Semi-infinite minimization problems Quadratic and linear programming Nonlinear least-squares and curve fitting Nonlinear system of equation solving Constrained linear least squares Sparse structured large-scale problems
==
21
The GUI contains many options. Everything can be done using coding.
22
Lets learn some of the things the optimization tool box can do
Weizmann 2010
23
Starting point
24
Weizmann 2010
27
nlinfit (Non linear fit) lsqnonlin (least squares non-linear fit) lsqcurvefit (least squares curve fit)
Hougen-Watson model
Example:
@func:
Write an M-file: function yhat = hougen(beta,x) Starting point Run: betafit = nlinfit(reactants,rate,@hougen,beta)
xdata = [0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3]; ydata = [455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
ydata(i ) c(1) e
c ( 2 )*xdata ( i )
Steps:
29
Weizmann 2010
30
2 x1 x2 e
2 x1 x2 e
x1
x1 x2
x1 2 x2 e
x1 2 x2 e x2 0
Summary:
Weizmann 2010
32
Weizmann 2010
33
Symbolic Math Toolbox uses symbolic objects to represent symbolic variables, expressions, and matrices. Internally, a symbolic object is a data structure that stores a string representation of the symbol.
34
g = 9*a+3*b+c
35
Deriving a function:
diff(f,x_sym) diff('sin(x)',x_sym)
Integrate a function:
int(f,x_sym)
36
Summary
38