Introduction To Matlab Tutorial 11
Introduction To Matlab Tutorial 11
Introduction To Matlab Tutorial 11
Tutorial 11:
Using Matlab for Numerical Analysis
Weizmann 2010 © 1
Outline
Data Smoothing
Data interpolation
Correlation coefficients
Curve Fitting
Optimization
Derivatives and integrals
2
Filtering and Smoothing
Assume we measured the response in time or other input
factor, for example:
Reaction product as function of substrate
response
factor
3
Smoothing –
Moving Average
span span
5
The Smooth Function
x = linspace(0, 4 * pi, len_of_vecs);
y = sin(x) + (rand(1,len_of_vecs)-0.5)*error_rat;
Data:
y
Generating Function:
sin(x)
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');
7
Data Interpolation Definition
Interpolation -
A way of estimating values of a function
between those given by some set of data points.
Interpolation
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
xx = -2:.5:2;
yy = -2:.5:3;
[X,Y] = meshgrid(xx,yy);
Z = X.*exp(-X.^2-Y.^2); Surf uses linear
interpolation
figure;
surf(X,Y,Z);
hold on;
plot3(X,Y,Z+0.01,'ok', 'MarkerFaceColor','r') 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
Optimization and Curve Fitting
Weizmann 2010 © 13
Curve Fitting –
Assumptions About The Residuals
y ŷ
Y Residual
X
Residual = Response – fitted response: r y y
n n
2
Sum square of residuals S ri ( yi yi )
2
1 i 1
Two assumptions: This is i what we want to minimize
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
corrcoef Computes the
Correlation coefficients
Consider the following data:
x = sort(repmat(linspace(0,10,11),1,20));
y_p = 10 + 3*x + x.^2 + (rand(size(x))-0.5).*x*10;
figure;
plot(x,y_p,'b.');
xlabel('x');ylabel('y');
title(['Correlation: ' num2str(cor)]); 15
Curve fitting Using a GUI
Tool (Curve Fitting Tool Box)
cftool – A graphical tool
for curve fitting
Example:
Fitting
x_full =
linspace(0, 2.56 * pi, 32);
y_full = sin(x_full);
With cubic polynomial
16
polyfit Fits a Curve By a
Polynomial of the Variable
Find a polynomial fit: We can estimate
poly_y_fit1 = polyfit(x,y_p,1); the fit quality by:
poly_y_fit1 = 12.6156 X + ( -3.3890 ) mean((y_fit1-y_p).^2)
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);
17
We Can Use polyfit to Fit Exponential
Data Using Log Transformation
poly_exp_y_fit1 =
1.9562 5.0152
poly_exp_y_fit1 = polyfit(x,log(y_exp),1);
y_exp_fit1 = exp(polyval(poly_exp_y_fit1,x)) 18
What about fitting a Curve
with a linear function of several
variables?
Can we put constraints on the
coefficients values?
yˆ c1 x1 c2 x2 c3 x3
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 Can Solve Many
Types of Optimization Problems
Optimization Toolbox –
Extends the capability of the MATLAB numeric computing
environment.
The toolbox includes routines for many types of
optimization including:
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
Optimization Toolbox GUI
Can Generate M-files
Weizmann 2010 © 23
Solving Constrained Square
Linear Problem
lsqlin (Least Square under Linear constraints)
[] – if no constraint
Starting point
24
Simple Use Of Least Squares
Under No Constrains
Assume a response that is a linear combination of two variables
vars = response =
[ 1 1 [ 0.2
-1 1.5 0.4
… …
] ]
1
min sum((vars coeff_lin response ) 2 )
x 2
coeff_lin = lsqlin(vars,response,[],[]);
We can also put constraints on the value of the coefficients:
coeff_lin = lsqlin(vars,response,[],[],[],[],[-1 -1],[1 1]);
25
Simple Use Of Least Sum of
Squares Under No Constraints
xx = -2:.1:2;
yy = -2:.1:2;
[X,Y] = meshgrid(xx,yy);
Z = coeff_lin(1)*X+
coeff_lin(2)*Y;
coeff_lin =
-0.2361
-0.8379
figure;
mesh(X,Y,Z,'FaceAlpha',0.75);colorbar;
hold on;
plot3(vars(:,1),vars(:,2),response,
'ok', 'MarkerFaceColor','r')
26
What about fitting a Curve
with a non linear function?
Weizmann 2010 © 27
We Can Fit Any Function
Using Non-Linear Fitting
You Can fit any non linear function using:
nlinfit (Non linear fit)
lsqnonlin (least squares non-linear fit)
lsqcurvefit (least squares curve fit)
Example: @func:
Hougen-Watson model Function handle –
A way to pass a function as
an argument!
Write an M-file:
function yhat = hougen(beta,x)
Starting point
Run:
betafit = nlinfit(reactants,rate,@hougen,beta)
470 300 10 8.55 1.00
285 80 10 3.79 0.05
([x1 x2 x3])… (y)… (coefficients)…
28
Optimization Toolbox – Example
Fitting a Curve With a Non Linear Function
Example
m
for using lsqcurvefit, We will fit the data :
min
c
( F (c, xdata) ydata)
i 1
2
Weizmann 2010 © 30
Solving Non Linear System
of Equations Using fsolve
x1
2 x1 x2 e
Assume we want to solve:
x2
We can express it as: x1 2 x2 e
Solving it:
x1
Write the function M-file: 2 x1 x2 e 0
function f = fSolveExampleFunc(x)
f = [2*x(1) - x(2) - exp(-x(1)); x1 2 x2 e x2 0
-x(1) + 2*x(2) - exp(-x(2))];
Choose initial guess: x0 = [-5; -5];
Run matlab optimizer:
options=optimset('Display','iter');
% Option to display output
[x,fval] = fsolve(@fSolveExampleFunc,x0,options)
x = [ 0.5671 0.5671] 31
Summary:
Weizmann 2010 © 32
A taste of Symbolic matlab:
Weizmann 2010 © 33
What Is Symbolic Matlab?
“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
Defining Symbolic Variables
and Functions
Define symbolic variables:
a_sym = sym('a')
b_sym = sym('b')
c_sym = sym('c')
x_sym = sym('x')
Define a symbolic expression
g = subs(f,x_sym,3)
g = 9*a+3*b+c
35
We Can Derive And Integrate
Symbolic Functions
Deriving a function:
This is a good
diff(f,x_sym)
diff('sin(x)',x_sym) place to stop
Integrate a function:
int(f,x_sym)
Symbolic Matlab can do
much more…
36
Summary
Matlab is not Excel…
38