Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
142 views

Introduction To Matlab Tutorial 11

The document discusses using Matlab for numerical analysis techniques such as data smoothing, interpolation, correlation, curve fitting, and optimization. It covers functions for filtering and smoothing data, interpolating data using interp1, computing correlation coefficients with corrcoef, fitting curves with polyfit and other tools, and using the optimization toolbox for problems like least squares fitting.

Uploaded by

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

Introduction To Matlab Tutorial 11

The document discusses using Matlab for numerical analysis techniques such as data smoothing, interpolation, correlation, curve fitting, and optimization. It covers functions for filtering and smoothing data, interpolating data using interp1, computing correlation coefficients with corrcoef, fitting curves with polyfit and other tools, and using the optimization toolbox for problems like least squares fitting.

Uploaded by

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

Introduction to Matlab

& Data Analysis

Tutorial 11:
Using Matlab for Numerical Analysis

Please change directory to directory E:\Matlab (cd E:\Matlab;)


From the course website
(http://www.weizmann.ac.il/midrasha/courses/MatlabIntro//course_outline.htm )
Download:
t11.zip

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

 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

3
Smoothing –
Moving Average

span span

Remark: The Span should be odd 4


Smoothing –
Behavior at the Edges

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');

Like very low pass filter


Different method

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

“plot” – Performs linear interpolation between the data points


8
Interpolating Data Using
interp1 Function
x_full = linspace(0, 2.56 * pi, 32);
y_full = sin(x_full);
x_missing = x_full;
x_missing([14:15,20:23]) = NaN;
y_missing = sin(x_missing);

x_i = linspace(0, 2.56 * pi, 64);

Data points which we want to interpolate


not_nan_i = ~isnan(x_missing);
y_i = …
interp1(x_missing(not_nan_i),…
y_missing(not_nan_i),…
x_i);

Default: Linear interpolation


9
interp1 Function Can Use
Different Interpolation Methods

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;

 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
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

 Polyfit on the log of


the data:
x = sort(repmat(linspace(0,1,11),1,20));
y_exp = exp(5 + 2*x + (rand(size(x))-0.5).*x);

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

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
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

 Assume we have the following data:


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];
 We want to fit the data with our model:
ydata(i)  c(1)  ec ( 2)*xdata(i )
 Steps:
 Write a function which implements the above model:
function y_hat = lsqcurvefitExampleFunction(c,xdata)
 Solve:
c0 = [100; -1] % Starting guess
[c,resnorm] =
lsqcurvefit(@lsqcurvefitExampleFunction,c0,xdata,ydata) 29
What about solving non linear
system of equations?

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:

Optimization tool box


has several features:
 Minimization
 Curve fitting
 Equations solving

Weizmann 2010 © 32
A taste of Symbolic matlab:

Derivatives and integrals

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

f = sym('a*x^2 + b*x + c')


 Substituting variables:

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…

 If you know what you want to do –


You will find the right tool!

38

You might also like