Lecture 9
Lecture 9
Nonlinear Regression: a form of regression analysis in which observational data are modeled by a
function which is a nonlinear combination of the model parameters and depends on one or more
independent variables.
xi - Independent variables
ai - Parameters in the non-linear function
yi - Given y values (or dependent variables)
ycalc - Calculated y values
X = lsqcurvefit(fun,x0,xdata,ydata)
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];
plot(xdata, ydata, ‘ko’)
fun = @(s,xdata)s(1)*exp(s(2)*xdata);
s0 = [100,-1];
S = lsqcurvefit(fun,s0,xdata,ydata)
S = 498.8309 -0.1013
Optimization solvers
[X, fval] = fminbnd (myfun, 𝐱𝐋𝐁 , 𝐱 𝐔𝐁 ) Bounded Min. problem
[X, fval] = fminsearch (myfun, 𝒙𝟎 ) Min. problem
[X, fval] = fminunc (myfun, 𝒙𝟎 ) Min. problem
myfun – function to be minimized in terms of x
𝐱 𝐋𝐁 - Lower bound on x
𝐱 𝐔𝐁 - Upper bound on x
𝒙𝟎 - Initial guess for x
X – Optimal values of x and fval – Corresponding objective function.
Example: 𝐦𝐢𝐧 𝒇 𝒙
𝒙
2 2
=100(𝑥2 −𝑥1 ) + (1−𝑥1 )2
x0 = [-1.2,1];
[x,fval] = fminsearch(fun,x0)
[x,fval] = fminunc(fun,x0)
function f = myfun(x)
end
[X, fval] = fminsearch (@myfun, 1.5)
[X, fval] = fminunc (@myfun, 1.5)
Optimizer Decision
Variables
myfun
Objective
function
Department of Chemical Engineering
Unconstrained Function Minimization in MATLAB
function f = myfun(x)
end
Example
[X,fval] = fmincon(@FUN,X0,A,B,Aeq,Beq,LB,UB,@NONLCON)
function f = FUN(x)
f = 9.82*x(1)*x(2)+2*x(1);
end
c(2) = 2500/(pi*x(1)*x(2))-
(pi^2*(x(1)^2+x(2)^2))/0.5882;
[X,fval] = fmincon(@FUN,[0.2,0.2],A,B,Aeq,Beq,LB,UB,@NONLCON)
Try this
Min x
2
1
2
x 2 11 x1 x 2 7
2
2
x1, x 2
s.t.
x12 x 22 25
5 x1 , x 2 5
Use any initial guess for both k1 and k 2 . Plot concentration vs time with the optimal values of k1 and k 2 .
function f = optim_param(k,t,data)
for i = 1:10
a(i) = exp(-(k(1) + k(2)) * t(i));
b(i) = (k(1) / (k(1) + k(2))) * (1 - exp(-(k(1) + k(2)) * t(i)));
c(i) = (k(2) / (k(1) + k(2))) * (1 - exp(-(k(1) + k(2)) * t(i)));
fcn(i) = (data(i,1) - a(i))^2 + (data(i,2) - b(i))^2 + (data(i,3) - c(i))^2;
end
f = sum(fcn);
end
clc
function f = clear all
optim_param(k,t,data) data = load('rxn_data.txt');
for i = 1:10 n = 10;
a(i) = exp(-(k(1) + k(2)) * t = 0.1:0.1:1;
t(i)); fun = @(k)optim_param(k,t,data);
b(i) = (k(1) / (k(1) + k(2))) * x0 = [0.2,0.1];
(1 - exp(-(k(1) + k(2)) * t(i))); [X,fval] = fminunc(fun,x0);
c(i) = (k(2) / (k(1) + k(2))) *
(1 - exp(-(k(1) + k(2)) * t(i))); % For plotting
fcn(i) = (data(i,1) - a(i))^2 +
(data(i,2) - b(i))^2 + (data(i,3) - for i = 1:10
c(i))^2; a(i) = exp(-(X(1) + X(2)) * t(i));
end b(i) = (X(1) / (X(1) + X(2))) * (1 - exp(-(X(1) + X(2)) * t(i)));
f = sum(fcn); c(i) = (X(2) / (X(1) + X(2))) * (1 - exp(-(X(1) + X(2)) * t(i)));
end end
plot(t,a,'-*',t,b,'-o',t,c,'-d')
Scatter Plot
x = randn(1000,1);
y = randn(1000,1);
scatter(x,y,'filled')
plot(x,y,'bo')
Data Visualization in MATLAB
Line Plot
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
Data Visualization in MATLAB
Heatmap
Create a matrix of data.
Then create a heatmap of the
matrix values.
t = 0:pi/50:10*pi;
st = sin(t);
ct = cos(t);
plot3(st,ct,t)
Data Visualization in MATLAB plot3(x,y,z,'bo')
[X,Y,Z] = sphere(16);
x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];
scatter3(x,y,z)
Data Visualization in MATLAB
High Dimensional Data plot (> 3-D) Read a 4-D data from MATLAB.
load fisheriris
plotmatrix(meas)
Data Visualization in MATLAB
Commonly used –
i. Principal Component Analysis (PCA)
ii. t-Stochastic Distributed Neighbors (t-SNE)
Data Visualization in MATLAB
load fisheriris
[coeff,score,~,~,estimated] = pca(meas);
scatter(score(:,1),score(:,2),'filled')
Data Visualization in MATLAB
t-Stochastic Distributed Neighbors (t-SNE) - High Dimensional Data Plot
load fisheriris
rng(8)
Y = tsne(meas);
scatter(Y(:,1),Y(:,2),'filled')
Y = tsne(meas,'NumDimensions',3);
scatter3(Y(:,1),Y(:,2),Y(:,3),'filled')
Y three-dimensional embedding
of the high-dimensional data ‘meas’.