Вы находитесь на странице: 1из 49

Optimization using Matlab

Introductory Tutorial

Jan 18, 2016

K.Rameshkumar
Department of Mechanical
Engineering
Amrita School of Engineering
Coimbatore 641 112

Schematic diagram of MATLABs


main features
Matlab
Matlab
programmi
ng
language
USER WRITTEN FUNCTIONS
BUILT IN FUNCTION

GRAPHICS

Jan 18, 2016

COMPUTATIO
NS

EXTERNAL
INTERFACE
(Mex-Files)

TOOLBOXES

Main Matlab Window

Jan 18, 2016

Some of the important features


and special characters used in
MATLAB
This is the default prompt symbol in
MATLAB
;
A semicolon at the end of a line avoids the
echoing the information entered before the
semicolon
. . . Three periods at the end of a line indicates
the continuation of the code in the next line
% Any text after this symbol is considered a
comment and will not be operational
MATLAB is case sensitive. Uppercase and
Jan 18, 2016
4
lowercase letters are treated separately

Some of the important features


and special characters used in
MATLAB

MATLAB assumes all variables to be arrays. As such,


separate dimension statements are not needed. Scalar
quantities need not be given as arrays.
Variable names should start with a letter and can
have a length of up to 31 characters in any
combination of letters, digits, and underscores.
The symbols for the basic arithmetic operations of
addition, subtraction, multiplication, division, and
exponentiation are +, , , /, and , respectively.
MATLAB has some built-in variable names and, as
such, we should avoid using those names for variables
in writing a MATLAB program or m-file. Examples of
built-in names: pi (for ), sin (for sine of an angle), etc.
Jan 18, 2016

>> clear
% removes any old variables from the workspace
>> clear all
% removes any old variables from the workspace
>> clc
Clears command window & command history
>> format short
% outputs results in decimal form (5 decimal places of
accuracy)
>> format short e : (Floating point format with 5 digits)
>> format long : (Scaled fixed point format with 15 digits)
>> help format : prints the contents of the manual % for
the command ``format'.

FORMAT
FORMAT
FORMAT
FORMAT
FORMAT
FORMAT

Jan 18, 2016

Default. Same as SHORT.


SHORT Scaled fixed point format with 5 digits.
LONG Scaled fixed point format with 15 digits.
SHORT E Floating point format with 5 digits.
LONG E Floating point format with 15 digits.
SHORT G Best of fixed or floating point format with 5 digits.

FORMAT LONG G Best of fixed or floating point format with 15 digits .

Lesson 1 : A Minimum MATLAB


Session

Arithmetic Operations : (interactive


mode)
Compute
a) (25/(210))
b) A=r2 where r=1/3-1
c) e163
d) Solve 3x=17 for x
e) (1+3i)/(1-3i)
Jan 18, 2016

Jan 18, 2016

Creating and working with Arrays


of Numbers
>> x=[1,2,3]
x=
1
2
3
>> x=[1 2 3]
x=
1
2
3
>> y=[2;1;5]
y=
2
1
5
>> z=x+y
??? Error using ==>
plus
Jan 18, 2016
Matrix dimensions

>> y=y'
y=
2
1
5
>> z=x+y
z=
3
3
8
>> linspace(0,10,5)
ans =
0 2.500 5.000 7.500
10.000
>> t=1:2:10
t=
1
3
5

9
9

Lesson 2 : Creating and working


with Arrays of Numbers
Ex1:
Equation of Straight Line : The equation of
Straight Line is y=mx+c where m and c are
constants. Compute y coordinates of a line with
slope m=0.5 and intercept c=-2 at the following
x-coordinates : x = 0, 1.5, 3, 4, 5, 7, 9, and 10.
Ex2:
All points with coordinates : x=rcos and
y=rsin, where r is the constant, lie on a circle
with radius r, i.e., they satisfy the equation
x2+y2=r2. Create a column vector with the
Jan 18, 2016
values
0,/4, /2, 3/4, and 5/4. Take r=2. 10

Solutions
Ex1
>> x=[0, 1.5, 3, 4, 5, 7, 9,10]
x=
0 1.50 3.00 4.00 5.00 7.00
9.00 10.00
>> y=0.5*x-2
y = -2.00 -1.25 -0.50
0 0.50 1.50
2.50 3.00
Ex2
>> theta =[0,pi/4, pi/2, 3*pi/4, pi, 5*pi/4]
theta =
0 0.7854 1.5708 2.3562
3.1416 3.9270
>> r=2
r =Jan 18, 2016
2

11

Lesson 3 :Creating
Simple plots
Ex1
Plot y=sinx, 0 x 2, taking 100 linearly
spaced points in the given interval. Label
the axes and put Plot created by me
Ex2
Plot y=e0.4xsinx, 0 x 4, taking 10,50,
and 100 points in the interval
Ex3
Plot 3D plot using plot3(x,y,z) to plot the
circular helix, x(t)=sint, y(t)=cost, z(t)=t, 0
2 20
Jan 18, 2016

12

Plot - Ex1
>>
>>
>>
>>

x=linspace(0,2*pi,100);
plot(x,sin(x))
xlabel('x'),ylabel('sin(x)')
title('Plot Created by RAMESH')
Plot Created by RAMESH

sin(x)

0.5

-0.5

-1
Jan 18, 2016

4
x

8
13

Plot - Ex2
>> x=linspace(0,4*pi,100);
>> y=exp(-0.4*x).*sin(x);
>> plot(x,y)
0.6
0.5
0.4
0.3
0.2
0.1
0
-0.1
-0.2

Jan 18, 2016

10

12

14

14

Plot - Ex3
>> t=linspace(0,20,100);
>> plot3(sin(t),cos(t),t)
20
15
10
5
0
1
0.5
0
-0.5

Jan 18, 2016

-1

-1

-0.5

0.5

15

Plot 4
>>x=0:10:1000;
>>y=x.^3;
>>semilogx(x,y)
>>semilogy(x,y)
>>loglog(x,y)

10

10

10

10

10

10

x 10

10

8
7

10

100

200

300

400

500

600

700

800

900

1000

5
4

10

10

10

10

0 1
10

10

10

10

10

Jan 18, 2016

10 1
10

10

10

16

Lesson 4 : Creating &


Executing script file
Create a script file (m file) titled circle.m using
the editor
===
%CIRCLE - A script file to draw a unit circle
theta=linspace(0,2*pi,100);
x=cos(theta);
y=sin(theta);
plot(x,y);
title('Circle of unit radius')
===
1

0.8
0.6
0.4
0.2
0

-0.2
-0.4
-0.6
-0.8

-1
-1

-0.8

-0.6

-0.4

-0.2

0.2

0.4

0.6

0.8

The file can be executed directly from the editor


or typing the file name circle in the command
window
Jan 18, 2016

17

Linear programming
problem

Jan 18, 2016

18

Linear programming
problem
Vector Form

A . x b,

min f x such that Aeq . x beq


x
ib x ub

f, x, b, beq, lb, and ub are


vectors, and A and Aeq are
matrices.
Jan 18, 2016

19

Solving Linear
programming problem

Find x that minimizes


f x 5 x1 4 x2 6 x3
Subject to
x1 x2 x3 20
3 x1 2 x2 4 x3 42
3 x1 2 x2 30
0 x1 ,0 x2 ,0 x3
Jan 18, 2016

f = [-5 ;-4 ;-6]


A = [1 -1 1 ;3 2 4;
3 2 0];
b = [20; 42; 30];
lb = zeros(3,1);
20

Jan 18, 2016

21

LPP Ex1

Jan 18, 2016

22

Matrices and Vectors

Jan 18, 2016

23

Jan 18, 2016

24

Matlab solution for


Unconstrained NLP fminunc
min f x e
x

x1

2
4 x1

2
2 x2

4 x1 x2 2 x2 1

Step 1: Write an M-file ucnlp.m.


function f = ucnlp(x) f = exp(x(1))*(4*x(1)^2
2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
Step 2: Starting guess.
x0 = [-1,1]; % Starting guess
unconstrained minimization routine fminunc.

Jan 18, 2016

25

Jan 18, 2016

26

Constrained Optimization
problem
Design a uniform column
of tubular section, with hinge joints at
both ends, to carry a compressive load P = 2500 kgf for minimum
cost. The column is made up of a material that has a yield stress
(y ) of 500 kgf/cm2, modulus of elasticity (E) of 0.85 106
kgf/cm2, and weight density () of 0.0025 kgf/cm3. The length of
the column is 250 cm. The stress induced in the column should
be less than the buckling stress as well as the yield stress. The
mean diameter of the column is restricted to lie between 2 and
14 cm, and columns with thicknesses outside the range 0.2 to 0.8
cm are not available in the market. The cost of the column
includes material and construction costs and can be taken as 5W
+ 2d, where W is the weight in kilograms force and d is the mean
diameter of the column in centimetres.

Jan 18, 2016

27

Problem Formulation
The design variables are the mean diameter (d) and
tube thickness (t ):

The Objective function to be minimized

Jan 18, 2016

28

Mathematical Model

Jan 18, 2016

29

Optimum Solution

Jan 18, 2016

30

Matlab solution using


fmincon
%Step 1 : Write an M-file cnlp.m for the
objective function.
function f= cnlp (x)
f= 9.82*x(1)*x(2)+2*x(1);
%Step 2 : Write an M-file cnlp_constraints.m
for the constraints.
function [c, ceq] = cnlp_constraints(x)
% Nonlinear inequality constraints
c = [2500/(pi*x(1)*x(2))-500;2500/(pi*x(1)*x(2))- ...
(pi^2*(x(1)^2+x(2)^2))/0.5882;-x(1)+2;x(1)-14;x(2)+0.2;x(2)-0.8];
% Nonlinear equality constraints
ceq = [];
Jan 18, 2016

31

Jan 18, 2016

32

Constrained Minimization
Using GA

Minimize the fitness function of


two variables x1 and x2,
Min f x 100

x12

2 2
x2

1 x1 2

Subject to the following nonlinear


inequality constraints and bounds
x1 x2 x1 x2 1.5 0 Non Linear

10 x1 x2 0 Non Linear
0 x1 1
2 x2 13
Jan 18, 2016

Bound
Bound

33

Step 1 :Create an Objective


function (M-File)
function y = simple_fitness(x)
y = 100*(x(1)^2 - x(2))^2 + (1 x(1))^2;

Step 2 : Create a constraint


function (M-File)

function [c, ceq] =


simple_constraint(x)
c = [1.5 + x(1)*x(2) + x(1) - x(2);x(1)*x(2) + 10];
ceq = [];
Jan 18, 2016

34

Jan 18, 2016

35

Jan 18, 2016

36

Optimization using SA

600
400
200

100

0
-100

50
-50

0
0

-50

50
100

Jan 18, 2016

-100

function f =
sa_dejong(x)
f= 3*sin(x(1))

37

Jan 18, 2016

38

Optimization of Cutting
Parameters of Turning
Process using Multi
Objective Genetic Algorithm

Jan 18, 2016

39

Problem Definition
To optimize the cutting parameters of
turning operation based on Surface
Roughness

and

Material

removal

Rate

Jan 18, 2016

40

Factors and levels for the


experiments
Level

Spindle
Speed rpm

Feed (mm)

Depth of
Cut (mm)

1100

0.08

0.6

1600

0.20

1.0

2300

0.32

1.6

Jan 18, 2016

41

L9(3*3) Array

Jan 18, 2016

Speed in
rpm

Feed in
mm/rev

Depth of
cut in mm

1100

0.08

0.6

1100

0.2

1100

0.32

1.6

1600

0.08

1600

0.2

1.6

1600

0.32

0.6

2300

0.08

1.6

2300

0.2

0.6

2300

0.32

Taguchi Array

42

Factor combination &


Response
Run

Speed in
rpm

Feed

Doc

Ra

Predicte
d Ra

1100

0.08

0.6

4.08

3.95

1100

0.2

1.9

2.23

1100

0.32

1.6

3.39

3.41

1600

0.08

0.69

0.78

1600

0.2

1.6

1.44

1.14

1600

0.32

0.6

3.23

3.06

2300

0.08

1.6

0.59

0.64

2300

0.2

0.6

1.43

1.39

2300

0.32

3.25

3.40

Jan 18, 2016

43

Regression analysis
Regression
Statistics
Multiple R
R Square
Adjusted R
Square
Standard Error
Observations

0.9893
0.9787
0.9147
0.3758
9.0000

ANOVA

Regression
Residual
Total

Intercept
V
f
d
V*f
Jan 18, 2016
f*d

df
6
2
8
Coefficient
s
16.2631
-0.0050
-9.3597
-18.8819
-0.0066
32.7558

SS
12.9598
0.2824
13.2422
Standard
Error
2.1121
0.0010
6.1062
3.3220
0.0044
6.4781

Significanc
F
eF
15.2982
0.0626

MS
2.1600
0.1412

t Stat
7.7000
-4.8828
-1.5328
-5.6838
-1.5179
5.0564

P-value
0.0165
0.0395
0.2650
0.0296
0.2684
0.0370

44

Objective Function
F(x) = [F1(x), F2(x)]
F1(x) =Ra
= 16.26-0.005(V)-9.36(f)-18.88(d)-0.006(V x f)
+32.76(f x d) +0.006(Vxd)
F2(x) =Material removal rate (Mr)
= 1000 x V x f x d
Min F(x) = [F1(x), -F2(x)]
Jan 18, 2016

45

Constraints
Factors
Bounds

Spindle
speed
rpm

Lower

1100

Upper

2300

Feed,
mm/rev
0.08
0.32

Depth
of Cut,
mm
0.6
1.6

Vmin V
vmax
Fmin f
Jan 18, 2016

fmax

46

Multi Objective GA
function
% Multi objective GA function
function f=mga(x)
f(1)=16.26-0.005*x(1)-9.36*x(2)18.88*x(3)-0.006*(x(1)*x(2))
+32.76*(x(2)*x(3))+0.006*(x(3)*x(1));
f (2)=-1*x(1)*x(2)*x(3)*3.14*40;

Jan 18, 2016

47

Jan 18, 2016

48

Thank you

Jan 18, 2016

49

Вам также может понравиться