MatMod logo

Exercise in Indexing (exer-sum)

Run LPL Code  ,  PDF Document

Problem

Given are

Pj = (    )  4  6  8  7 , Ai = (  )   5 || 2|| | 2| |( 4|)   1 , Xi,j = (   )   3  6   5  4 || 6  7   7  1|| | 7  8   4  2| |( 3  3   2  6|)   5  8   3  8 and Y i,j = (     )   4  1  6  6 || 3  3  4  4|| | 4  8  5  3| |( 6  7  4  5|)   3  5  5  3

Evaluate a) – d) by hand, write the first 4 terms of the sum expansion for e) – g).

a) i=15A i b) j=14P j c) i=15X i,j for j = 1 d) j=14X i,j for i = 12

e) i=15 j=14X i,j Y i,j f) i=15 j=14P j Xi,j g) i=15A i Y i,j for j = 14

Implement it in LPL

a) Write an LPL-code which defines two sets i and j for the indices and the vector and matricies from Problem as parameters. Display all data with Writep() statements.

Install LPL on your computer. Alternatively you can enter the models directly online at empty. Make sure you save your models on your computer by copy/pasting your code to a local file.

b) Write LPL-code to compute and display all sums from the problem. We give two examples:

  Write(' %5.2f' n', sum' {i' } A); 
  Write(' %s' n', {i|i<=2} Format('i=%d: %5.2f ',i,sum{j} X));

Solution

Listing 1: The Complete Model implemented in LPL [2]

model exerSum "Exercise in Indexing"; 
  set i:=1..5; 
  set j:=1..4; 
  parameter P{j}:=[4 6 8 7]; 
  parameter A{i}:=[5,2,2,4,1]; 
  parameter X{i,j}:=[3 6 5 4, 6 7 7 1, 7 8 4 2, 3 3 2 6, 5 8 3 8]; 
  parameter Y{i,j}:=[4 1 6 6, 3 3 4 4,  4 8 5 3, 6 7 4 5, 3 5 5 3]; 
  Writep(P,A,X,Y); 
  Write('a) %5.2f' n', sum{i} A); 
  Write('b) %5.2f' n', sum{j} P); 
  Write('c) %5.2f' n', sum{j} X[1,j]); 
  Write('d) %s' n', {i|i<=2} Format('i=%d: %5.2f ',i,sum{j} X)); 
  Write('e) %5.2f' n', sum{i} sum{j} X[i,j]*Y[i,j]); 
  Write('f) %5.2f' n', sum{i} sum{j} P[j]*X[i,j]); 
  Write('g) %s' n', {j} Format('j=%d: %5.2f ',j, sum{i} A[i]*Y[i,j])); 
end

References

[1]   MatMod. Homepage for Learning Mathematical Modeling :  https://matmod.ch.

[2]   Hürlimann T. Reference Manual for the LPL Modeling Language, most recent version. https://matmod.ch/lpl/doc/manual.pdf.