EE407- Chap2
EE407- Chap2
& Optimization
LECTURES:
Motivation.
Important background.
Graphical method.
Bracketing methods.
Bisection method.
Matlab example.
elseif(temp < 0)
C(2) = approx(i);
else
C(1) = approx(i);
end
Et(i) = abs((true-approx(i))/true)*100;
if(i > 1)
Ea(i) = abs((approx(i)-approx(i-1))
/approx(i))*100;
end
end
.
Any Questions?
.
Any Questions?
Example:
𝑓 𝑥 = 𝑒 −𝑥 − 𝑥 = 0
We can set the formula:
𝑥𝑖+1 = 𝑒 −𝑥𝑖
f = @(xi) exp(-xi);
true = 0.5671433;
N = 20;
x = zeros(1,N);
Et = zeros(1,N);
Ea = zeros(1,N); Ea(1) = 100;
for i = 2:N
x(i) = f(x(i-1));
Et(i) = abs((true-x(i))/true)*100;
Ea(i) = abs((x(i)-x(i-1))/x(i))*100;
end
f = @(xi) exp(-xi)-xi;
fp = @(xi) -exp(-xi)-1;
true = 0.5671433;
N = 20;
x = zeros(1,N); Et = zeros(1,N);
Ea = zeros(1,N); Ea(1) = 100;
for i = 2:N
x(i) = x(i-1)-f(x(i-1))/fp(x(i-1));
Et(i) = abs((true-x(i))/true)*100;
Ea(i) = abs((x(i)-x(i-1))/x(i))*100;
end
Example:
𝑓 𝑥 = 𝑥 10 − 1 = 0
We can set the formula:
𝑥𝑖10 − 1
𝑥𝑖+1 = 𝑥𝑖 −
10𝑥𝑖9
Any Questions?
𝑓 𝑥 = 𝑒 −𝑥 − 𝑥
Example:
𝑓 𝑥 = 𝑒 −𝑥 − 𝑥 = 0
We may initially take three
arbitrary points
𝑥0 = 0.5
𝑥1 = 0.6
𝑥2 = 0.4
The complete algorithm is
lengthy. However, we can take
the same code for the secant
method and replace the
formula.
Numerical Methods for EE (EE407) | Main
End of Lecture!
121
Any Questions?
Polynomials in engineering.
Evaluation of polynomials.
Deflation of polynomials (synthetic division).
Müller’s method.
Examples.
Matlab functions for solving equations.
Consider:
𝑑2 𝑦 𝑑𝑦 2 − 3𝑟 + 2 = 0
− 3 + 2𝑦 = 0 → 𝑟
𝑑𝑡 2 𝑑𝑡
The eigenvalues are
3 ± 32 − 8 1
𝑟= =ቊ → 𝑦 𝑡 = 𝑐1 𝑒 𝑡 + 𝑐2 𝑒 2𝑡
2 2
Assuming 𝑦 0 = 1 and 𝑦 ′ 0 = 0, we get the exact solution:
𝑦 𝑡 = 2𝑒 𝑡 − 𝑒 2𝑡
Verification:
2𝑒 𝑡 − 4𝑒 2𝑡 − 3 2𝑒 𝑡 − 2𝑒 2𝑡 + 2 2𝑒 𝑡 − 𝑒 2𝑡 = 0
Müller
Secant
𝑓 𝑥1 − 𝑓 𝑥0 𝑓 𝑥2 − 𝑓 𝑥1
𝛿0 = = 62.25, 𝛿1 = = 69.75
𝑥1 − 𝑥0 𝑥2 − 𝑥1
We can now find the parabola coefficients as
𝛿1 − 𝛿0
𝑎= = 15, 𝑏 = 𝑎ℎ1 − 𝛿1 = 62.25, 𝑐 = 48
ℎ1 + ℎ0
Hence, the new estimate is
−2𝑐
𝑥3 = 𝑥2 + = 3.976487
2
𝑏 ± 𝑏 − 4𝑎𝑐
𝑥3 − 𝑥2
𝜀𝑎 = × 100% = 25.74%
𝑥3
f = @(xi) xi^3-13*xi-12;
true = 4; N = 8;
x = zeros(1,N); x(1) = 4.5; x(2) = 5.5; x(3) = 5;
Et = zeros(1,N); Ea = zeros(1,N); Ea(3) = 100;
for i = 4:N
h0 = x(i-2)-x(i-3); h1 = x(i-1)-x(i-2);
delta0 = (f(x(i-2))-f(x(i-3)))/h0;
delta1 = (f(x(i-1))-f(x(i-2)))/h1;
a = (delta1-delta0)/(h1+h0);
b = a*h1+delta1; c = f(x(i-1));
x(i) = x(i-1)-2*c/(b+sqrt(b^2-4*a*c));
Et(i) = abs((true-x(i))/true)*100;
Ea(i) = abs((x(i)-x(i-1))/x(i))*100;
end
Any Questions?
What is optimization?
Types of optimization.
Unconstrained optimization of one-dimensional functions.
The golden section method.
Newton’s optimization method.
Solved examples and Matlab code.
f = @(x) 2*sin(x)-x^2/10;
syms x;
fx = 2*sin(x)-x^2/10;
fxp = diff(fx,x);
true = vpasolve(fxp==0,[0 4]);
xl = 0; xu = 4;
N = 6; es = 0.5*10^(2-N);
maxiter = 50;
R = (sqrt(5)-1)/2;
iter = 1;
d = R*(xu-xl);
x1 = xl+d; x2 = xu-d;
f1 = f(x1); f2 = f(x2);
iter = iter + 1;
if(f1 > f2)
x_opt(iter) = x1; fx = f1;
else
x_opt(iter) = x2; fx = f2;
end
if(x_opt(iter) ~= 0)
Ea(iter) = (1-R)*abs(x_int/x_opt(iter))*100;
Et(iter) = (true-x_opt(iter))/true*100;
end
if(Ea(iter) <= es || iter >= maxiter)
break;
end
end
fp = @(x) 2*cos(x)-x/5;
fpp = @(x) -2*sin(x)-1/5;
syms x;
fx = 2*sin(x)-x^2/10;
fxp = diff(fx,x);
true = vpasolve(fxp==0,[0 4]);
N = 7;
x = zeros(1,N); x(1) = 2.5;
Et = zeros(1,N);
Ea = zeros(1,N); Ea(1) = 100;
for k = 2:N
x(k) = x(k-1)-fp(x(k-1))/fpp(x(k-1));
Et(k) = abs((true-x(k))/true)*100;
Ea(k) = abs((x(k)-x(k-1))/x(k))*100;
end
Any Questions?
f = @(x,y) y-x-2*x.^2-2*x.*y-y.^2;
xl = -2; xu = 2; yl = 1; yu = 3;
N = 100000; maxf = -1e9;
for i = 1:N
x = xl + (xu-xl)*rand;
y = yl + (yu-yl)*rand;
fn = f(x,y);
if(fn > maxf)
maxf = fn;
maxx = x; maxy = y;
end
end
X1 = xl:0.01:xu; Y1 = yl:0.01:yu;
[X,Y] = meshgrid(X1,Y1);
figure; contourf(X,Y,f(X,Y)); hold on;
scatter(maxx,maxy);
f = @(x,y) 2*x.*y+2*x-x.^2-2*y.^2;
fg = @(x,y) [2*y+2-2*x;2*x-4*y];
true = [2;1];
N = 50;
XY = zeros(2,N);
Et = XY; Ea = XY;
h = 0.25;
for i = 2:N
XY(:,i) = XY(:,i-1)+h*fg(XY(1,i-1),XY(2,i-1));
Et(:,i) = abs((true-XY(:,i))./true)*100;
Ea(:,i) = abs((XY(:,i)-XY(:,i-1))./XY(:,i))*100;
end
Any Questions?
Constrained optimization.
Linear Programming.
Graphical solution.
Alternative scenarios.
An overview of the simplex method.
Any Questions?