# GAMS Support Wiki

### Site Tools

gams:linearize_a_non-linear_function

# Differences

This shows you the differences between two versions of the page.

 gams:linearize_a_non-linear_function [2007/10/30 16:58]Franz Nelissen created — (current) 2021/08/19 14:46 Aileen Boehme removed2010/02/11 09:53 gams 2009/11/13 06:18 support 2009/10/27 14:04 support 2007/10/30 17:05 Franz Nelissen 2007/10/30 17:04 Franz Nelissen 2007/10/30 16:58 Franz Nelissen created Next revision Previous revision 2021/08/19 14:46 Aileen Boehme removed2010/02/11 09:53 gams 2009/11/13 06:18 support 2009/10/27 14:04 support 2007/10/30 17:05 Franz Nelissen 2007/10/30 17:04 Franz Nelissen 2007/10/30 16:58 Franz Nelissen created Line 1: Line 1: - ====== How do I linearize a non-linear function? ====== - - Find below some approaches to linearize the cubic function ''​y =e= x*sqr(x) - 7*sqr(x) + 14*x - 8''​ - <​code>​ - \$offsymxref offsymlist - option limrow = 0, - ​limcol = 0, - ​solprint = off ; - - variable y,x ; - equations eq ; - eq.. y =e= x*sqr(x) - 7*sqr(x) + 14*x - 8 ; - - x.lo = 1 ; x.up = 10.5 ; - model cubic /all/ ; - solve cubic using nlp minimizing y ; - display y.l, x.l ; - ​ - The global solution to that problem is: - <​code>​ - ----     13 VARIABLE y.L                   ​= ​      ​-2.113  ​ - VARIABLE x.L                   ​= ​       3.215  ​ - ​ - - ===== Using Binary variables to model a piecewise linear function in GAMS. ===== - <​code> ​ - \$ontext - Using Binary variables to model a piecewise linear function in GAMS. - - This formulation uses points on the curve and is  particularly good for developing approximations of - nonlinear functions ​ - - Ref: "​Integer and Combinatorial Optimization"​ by - Nemhauser, G.L., and Wolsey, L.A., John Wiley - and Sons. Page 11. - \$ontext ​ - option limrow = 0, - ​limcol = 0 - ; ​ - - * this contains the set of points ​ - - set i / - ​1*20 ​ - /  ; - - set notlast(i) ; - notlast(i)\$(ord(i) lt card(i)) = yes ; - - * this contains the values of x-coordinates ​ - parameter xval(i) / - 1    1 - 2    1.5 - 3    2 - 4    2.5 - 5    3 - 6    3.5 - 7    4 - 8    4.5 - 9    5 - ​10 ​   5.5 - ​11 ​   6 - ​12 ​   6.5 - ​13 ​   7 - ​14 ​   7.5 - ​15 ​   8 - ​16 ​   8.5 - ​17 ​   9 - ​18 ​   9.5 - ​19 ​  ​10 ​ - ​20 ​  ​10.5 ​ - / ; - - * this contains the nonlinear function of x - parameter yval(i) ; - yval(i) = xval(i)*sqr(xval(i)) - 7*sqr(xval(i)) + 14*xval(i) - 8 ; - - variables x,​y,​lam(i),​bin(i) ; - positive variables lam(i) ; - binary variables ​  ​bin(i) ; - - equations xdef, ydef, norm, lamdef(i), soslam ; - - xdef.. x =e= sum(i,​lam(i)*xval(i)) ; - ydef.. y =e= sum(i,​lam(i)*yval(i)) ; - - norm.. sum(i,​lam(i)) =e= 1 ; - - lamdef(i).. lam(i) =l= bin(i-1) + bin(i)\$notlast(i) ; - soslam.. ​   sum(i\$notlast(i),​bin(i)) =e= 1 ; - - x.lo = smin(i,​xval(i)) ; - x.up = smax(i,​xval(i)) ; - - model piece /all/ ; - piece.optcr = 0.0 ; - solve piece minimizing y using mip ; - display x.l, y.l ; - display lam.l, bin.l ; - ​ - - - -