4 Teaching Optimization Techniques Using Matlab and Mathematica A Comparative Study
4 Teaching Optimization Techniques Using Matlab and Mathematica A Comparative Study
ABSTRACT
This paper aims to explore how technology-based teaching (with Matlab and
Mathematica) helps students in understanding optimization methods. Students
solve linear programming problems using Matlab and Mathematica. Some
examples and case study are used to discuss and explain issues that have been
observed while using technology in completing this course. Results show that
Mathematica linear programming algorithm is better than the algorithm for the
same command in Matlab. Students also learn to counter check important results
of optimization problems at hand. This sort of experimental mathematics helped
students in improving their confidence levels in attacking optimization problems.
1. INTRODUCTION
Computer algebra systems (CAS) are gaining increasing interest in the every day work
of students. The use of these application software help students a lot in understanding the
concepts of physics and mathematics. However, students erroneously assume that CAS
tools always know more than they do about how to compute a mathematical expression.
It is recommended for students to perform calculations in more than one ways in order to
counter check final results of problems at hand (Aguirregabiria JM, Hernandez A, and
Rivas M, 1994).
Research (Hoyles C. & Noss R., 2003), (Kendal M. & Stacey K., 1999) shows that students
learn more from an organized lecture when they are technology-based. Other group says
that (Wicks, R. J., 1999), (Wicks, R. J., 1996) Mathematica and Maple are two such systems
with which we can create rich learning experience for our students. There has been a wide
* The material presented by the authors does not necessarily portray the viewpoint of the editors
and the management of the Institute of Business and Technology (IBT) or Federal Urdu University of Arts,
Science and Technology.
* Muhammad Arif Hussain : wmarif2002@yahoo.com
* Ingila Rahim : Ingila.rahim@gmail.com
C
range of computer activities (Shaw W T and Tigg J., 1994)(Davis B, Porta H and Uhl J.,
1994) used in teaching mathematical concepts. In order to enhance students' understanding
of abstract linear algebra concepts, the author worked on classroom activities supported
by Matlab and Mathematica to provide inquiry-based learning environments. Mathematica
ahs also powerful graphics capability (Maeder R, 1999).
According to a comparison of the two CAS tools, Mathematica can do all the numeric
math and matrix work that Matlab can do, as fast and as accurately. Many of Mathematica's
functions switch automatically between different algorithms, whereas in Matlab one must
always choose the algorithm. Matlab requires one to manually linearize ordinary differential
equations (ODEs) to a system of first order equations before it can solve them, but
Mathematica accepts any order ODE. Matlab can do integration up to only three dimensions
(with triplequad), but Mathematica handles n-dimensional
(http://www.helium.com/items/895074-comparing-matlab-and-mathematica, 2012).
Other viewpoint is that Mathematica is easier to use. It is better for symbolic and analytic
job, whereas Matlab is good for numeric computations
(http://www.physicsforums.com/showthread.php?t=196740 , 2012).
Section 2 gives some detail of linear programming mathematical formulation. In Section
3, basics commands of Matlab and Mathematica is discussed to attack mathematical
problems. Finally, Section 4 concludes this paper
where l is a vector of Lagrange multipliers (Smith K. J. and Bradley G. L., 1999) and s
is a vector of dual slack variables. These two problems are intimately related, and algorithms
typically solve both of them simultaneously. When the vectors x *, l *, and s * satisfy the
following optimality conditions:
then x * solves the primal problem and (l * , s* )solves the dual problem.
In the next section we summarize the basics of Matlab and Mathematica commands which
help to solve linear and nonlinear problems.
19
2
0
9
0
0
3
>> c'
ans =
1
2
3
2
0
9
0
0
3
The commas in the specification of b can be replaced with spaces, as depicted by matrix
c. Square brackets refer to vectors and parentheses are used to refer to elements within a
matrix. The command det and inv return the determinant and inverse of a matrix
respectively. The ' performs the transpose of a matrix. Transpose of matrices b and c, as
shown above.
The help command returns information on different commands.
3.1.1. Calculus with MATLAB
Differentiation
For constructing symbolic objects, we define
>> syms x
20
ans =
2*x
For its second derivative we write
>> y=x*x;diff(y,x,2)
ans =
2
Integration
MATLAB uses int command to integrate. For example, int(S, x) is the indefinite integral
of S with respect to 'x'. Similarly, int(S, v, a, b) is the definite integral of S with respect
to v from a to b. We demonstrate these with few examples.
>>
>>
syms x t;
int(1/(1+x^2))
ans =
atan(x)
3.1.2. Linear Programming in Matlab
In Matlab LINPROG (or linprog) command is used for Linear programming. The syntax
is as follows (Venkataraman P., 2001).
X=LINPROG(f,A,b) attempts to solve the linear programming problem:
min f'*x
21
3)
Matlab linprog command fails to solve this simple example. The same set of examples
will be solved by Mathematica in the next section.
3.2. Symbolic Computations in Mathematica
[Input]: x + x
[Output]:2x
[Input]: x = 3; y = 5; x + y
[Output]:8
Mathematica has many powerful features which go far beyond even what is needed for
calculus. We define some functions for the purpose of evaluation. The syntax for defining
function is as follows:
g[x_] := x^3 + 5;
h[x_] := -2*x + 3;
22
Here, we defined two functions g(x) and h(x). It is necessary to use := and the underline
after the variable. It is also important to note that functional dependence in Mathematica
is always denoted by square brackets [ ].
[Input]:
[Output]:
g[2]
13
Similarly,
[Input]: h[2]
[Output]: -1
Sign ? (or ??, for more detail) can be used to get help on any command. For instance, to
get help on FindRoot command in Mathematica do the following.
[Input]: ?FindRoot
[Output]: FindRoot[lhs==rhs, {x, x0}] searches for a numerical solution to the
equation lhs==rhs. Similarly, FindRoot[{eqn1, eqn2, }, {{x, x0},{y, y0},}] searches
for a numerical solution to the simultaneous equations eqni.
3.2.1. Calculus in Mathematica
Differentiation
The derivative g'(x) can be obtained by using command: D[ ] as given below.
[Input]: D[g[x],x]
[Output]:3x2
Integration
We define another function f(x):
f[x_] := 1 / (x + 5);
Now, we perform integration in Mathematica environment.
[Input]: Integrate[f[x],x]
[Output]:Log[x+5]
The above result is exact. One can also obtain numerical integration of the same function
as follows.
[Input]: NIntegrate[f[x], {x,2,4}]
[Output]:0.251314
3.2.2. Linear Programming in Mathematica
In Mathematica, there are several ways to solve Linear Programming problems. One
command which is based matrix formulation is given below (Wolfram Research, Inc.,
2003).
LinearProgramming[c, m, b]. This command finds vector x which minimizes cx.
Subject to the constraints mx $ b and x $ 0
Similarly, one can use
LinearProgramming[c, m, b, l]. This command finds vector x which minimizes cx.
Subject to the constraints mx$ b and x $ l
23
Mathematica also has other commands Minimize, Maximize, NMinimize, and NMaximize
for the solution of linear and nonlinear optimization problems. Maximize and Minimize
commands give exact (or symbolic) solutions to the problems, whereas NMinimize, and
NMaximize commands show numeric solutions to the problems.
3.2.2.2. Examples
Here, we solve the same set of examples as given in Section 3.12.1.
1) We solve the LP problem:
Maximize P = 1.2 H + L
Subject to: 10 H + 12 L # 1920
5 H + 3 L # 780
H, L $ 0
LinearProgramming[{-1.2 ,-1},{{-10,-12},{-5,-3}},{-1920,-780}]
Answer: P = 204, H = 120, L = 60.
2) We solve the LP problem:
Minimize F = 7 x1+ 20 x2
Subject to:
x1 + 2 x2 $ 2
x1 + 5 x2 $ 3
x1, x2 $ 0
Answer: F = 16, x1 = 4/3, x2 = 1/3.
3)We solve the LP problem:
Maximize F = 2 x1 + x2 + 3 x3
Subject to:
x1 + 2x2 + x3# 12
2 x1 + x2 + x3# 20
x1 + x2 + 2 x3# 20
x1, x2 , x3# 0
Matlab was unable to solve this problem as we observed in the previous section. We solving
this LP problem with Mathematica by two different commands.
Mathematica Solution:
We use the first command LinearProgramming[ ]
1) LinearProgramming[{-2,-1,-3},{{-1,-2,-1},{-2,-1,-1},{-1,-1,-2}},{-12,-20,-20}]
Output: {4, 0, 8}
Maximum F comes out to be 32. at x1= 4, x2 = 0, and x3 = 8
And now we use another command Maximize[ ]
2) Maximize[{2a+b+3 c,a+2 b+ c# 12,2 a+ b+ c# 20,a+ b+2 c# 20,a$ 0,b$ 0,c$ 0},{a,b,c}]
Here, a = x1 b = x2 , c = x3 .
Output is:
F = 32, a6 4, b6 0, c6 8
.Above answer is same as given in the reference text. Next we solve a case study with
Matlab and Mahematica.
3.2.2.3. Case Study
Forest Service Allocation: The US forest services has used just such an allocation model
to address the sensitive task of managing 191 million acres of national fresh land (Rardin
R L.,1998). The forest service must tradeoff timber, grazing, recreational, environmental,
24
national preservation, and other demands on forestland. For this purpose it defines a linear
programming model in order to maximize total net present value (NPV) per acre of all
uses in area. Twenty one variables (all positive) are defined.
The above problem is solved with Matlab and Mathematica. Results are more or less
similar to the published report from the two CAS tools. However, Mathematica result sems
to be better, as it is closer to the reported NPV in the report.
a) MATLAB Solution: Box 1: Malab linprog command to solve the case study.
Box 1 gives the solution of the above cases tudy. Now, we perform calculation with
Mathematica.
As Maximize command in Mathematica can accommodate all types of constraints found
in the literature therefore, we solve this case study with Maximize command .
b) Mathematica Solution:
Box 2. Mathematica notebook showing Mathematica command for Maximization.
25
Box 2 gives the input and output of the case study pergormed with Mathematica.
5. References
Aguirregabiria JM, Hernandez A, and Rivas M, 1994,"Are we careful enough when using
computer algebra system", Computers in Physics, 8 (1)
Hoyles C. & Noss R., 2003, 'What can digital technologies take from and bring to research
in mathematics education? In: A.J. Bishop, M.A. Clements, C. Keitel, J. Kilpatrick &
F. Leung (Eds.)', Second International Handbook of Mathematics Education (Vol. 1,
pp. 323-349). Dordrecht: Kluwer Academic Publishers.
Kendal M. & Stacey K., 1999, Varieties of teacher privileging for teaching calculus
with computer algebra systems, International Journal of Computer Algebra in
Mathematics Education 6, 233-247.
Wicks, R. J., 1999, Mathematica materials from MAA mini-course on creating
interactive texts in Mathematica. MAA 1999 Joint meetings.
Wicks, R. J., 1996, Linear Algebra: an interactive laboratory approach with
Mathematica. Addison-Wesley Publishing Company, Reading, Massachusetts.
Shaw W T and Tigg J., 1994, "Applied Mathematica: Getting Started, Getting It
Done", Addison-Wesley, Massachusetts..
Davis B, Porta H and Uhl J., 1994, "Calculus andd Mathematica:, Addison-Wesley,
Massachusetts..
Maeder R, 1999, "Programming in Mathematica", Addison-Wesley, Massachusetts.
http://www.helium.com/items/895074-comparing-matlab-and-mathematica, 2012.
http://www.physicsforums.com/showthread.php?t=196740 , 2012.
Nash, S. and Sofer, A. 1996. Linear and Nonlinear Programming. McGraw-Hill. Smith
K. J. and Bradley G. L., 1999, "A Mathematica:Approach To Calculs 2/e",
Addison-Wesley, Massachusetts..
Venkataraman P., 2001, 'Applied Optimization with Matlab Programming', Wiley.
Hoffmann LD and Bradley GL, 1995, 'Finite Mathematics with Calculus', McGraw-Hill,
NY., pp. 153-204.
Wolfram Research, Inc. 2003 , Mathematica, Version 5.0, Champaign, IL.
Rardin R L, 1998, "Optimization in Operations Research", Prentice-Hall, NJ, pp.
132-134..
26