0

I have two agents, distributers and suppliers. Each of these turtles have a number of variables which some of them are defined global to be able to be assigned to agents of another type. The question is that I am trying to solve a system of equations for the distributors using some of its variables as follows :(Sales and Coefficients are some variables of distributers which change in every run) DCoeffiecients = matrix A, sales = matrix C, and I am looking for Unknowns = matrix B (All the variables that I am calling inside the procedure are defined global)

sales = DCoefficients * Unknowns

I used "Matrix Extension" to solve it, but I run into this error " Expected a literal value" since I have variables not some constant numbers!

to Solve-quantity
ask distributers[
let A matrix:from-row-list [[Prod1coeff1 Prod1coeff2 Prod1coeff3 prod1coeff4] [[Prod2coeff1 Prod2coeff2 Prod2coeff3 prod2coeff4][Prod3coeff1 Prod3coeff2 Prod3coeff3 prod3coeff4][Prod4coeff1 Prod4coeff2 Prod4coeff3 prod4coeff4]  ] 
let C matrix:from-row-list [[S1] [S2] [S3] [S4]]
print matrix:solve A C
]end

How can I fix this error and assign variables to the matrix? Thanks

Naha
  • 15
  • 4
  • Don't use the bracket shorthand for `list`. – Alan Feb 04 '17 at 21:01
  • Sorry, can you please explain more? I did not get it – Naha Feb 04 '17 at 21:19
  • If `x` and `y` are variables, you must write `(list x y)`. The bracket shorthand `[x y]` will not work; it only works with literals (numbers and strings). – Alan Feb 05 '17 at 05:10
  • let A matrix:from-row-list ((Prod1coeff1 Prod1coeff2 Prod1coeff3 prod1coeff4) ((Prod2coeff1 Prod2coeff2 Prod2coeff3 prod2coeff4)(Prod3coeff1 Prod3coeff2 Prod3coeff3 prod3coeff4)([Prod4coeff1 Prod4coeff2 Prod4coeff3 prod4coeff4)) Like this? does not work either! – Naha Feb 05 '17 at 16:37
  • You must use the `list` primitive, not just parentheses. – Alan Feb 05 '17 at 19:59
  • Thanks a lot. Both of your answers helped me fix the error. If I need to access the value in a row and coloum, is this correct? let Bxy matrix:get B 1 1 – Naha Feb 06 '17 at 00:32

1 Answers1

0

Here's an abbreviated example of what you need to do using the list primitive. Note that I've done it in one line, and then broken apart.

extensions [matrix]

to go
  let Prod1coeff1 1
  let Prod1coeff2 2
  let prod2coeff1 3
  let prod2coeff2 4

  let A matrix:from-row-list (list (list Prod1coeff1 Prod1coeff2) (list Prod2coeff1 Prod2coeff2))
  show A

  let row1 (list Prod1coeff1 Prod1coeff2)
  let row2 (list Prod2coeff1 Prod2coeff2)
  let list-of-rows (list row1 row2)
  let B matrix:from-row-list list-of-rows
  show B

end

Note too that I've put parentheses around the list primitive as you will need them if the list contains more than two elements.

HTH, Charles

Charles
  • 3,888
  • 11
  • 13
  • Thanks. The only problem is that when I want to assign the variables like what follows, it still does not work: to Proced1 let Prod1coeff1 variablex let Prod1coeff2 variabley let prod2coeff1 variabley Even though variablex,y,z are either global or have been defined prior to this line! – Naha Feb 05 '17 at 18:56
  • Sorry another question. Do you mean the parts that you broke apart are equivalent and one of them would suffice? For example just this : let A matrix:from-row-list (list (list Prod1coeff1 Prod1coeff2) (list Prod2coeff1 Prod2coeff2)) show A – Naha Feb 05 '17 at 19:20
  • Thanks a lot. Both of your responses helped me fix the error. – Naha Feb 06 '17 at 00:33
  • Sorry, another question about matrices. To have access to an element of a matrix, is this correct? : let Bxy matrix:get B 1 1 – Naha Feb 06 '17 at 02:08