Re: Plot the results of Findroot

*To*: mathgroup at smc.vnet.net*Subject*: [mg122776] Re: Plot the results of Findroot*From*: Ray Koopman <koopman at sfu.ca>*Date*: Thu, 10 Nov 2011 06:53:05 -0500 (EST)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*References*: <j9dojg$b4m$1@smc.vnet.net>

On Nov 9, 3:36 am, Dimitris Kontopoulos <dimitris... at gmail.com> wrote: > Hello everyone, > > I have a system of 21 simultaneous equations where i want to calculate > the values of x1, x2...x21 and i am trying to evaluate it by using > FindRoot. > > I have 2 problems, > 1) I want to tell FindRoot to calculate this set for a range of values > for 2 parameters (n and d) that are found in the equations > 2) I want to plot the results of x1, x2,...x20 for the range of the > parameters. > > i have named each of the 21 equations y1, y2, y3.... y21 and this is > how i wrote the command for FindRoot: > > FindRoot[{y1, y2, y2, y4, y5, y6, y7, y8, y9, y10, y11, y12, y13, y14, > y15, y16, y17, y18, y19, y20, > x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 + x13 + > x14 + x15 + x16 + x17 + x18 + x19 + x20 + x21 == 1}, {{x1, > 0.0272275467214981, 0, 1}, {x2, 0.0271731, 0, 1}, {x3, 0.04279, 0, > 1}, {x4, 0.042534, 0, 1}, {x5, 0.0548527, 0, 1}, {x6, 0.054198, 0, > 1}, {x7, 0.0627144, 0, 1}, {x8, 0.0614572, 0, 1}, {x9, 0.066180, 0, > 1}, {x10, 0.064208, 0, 1}, {x11, 0.075882, 0, 1}, {x12, 0.062862, > 0, 1}, {x13, 0.06160, 0, 1}, {x14, 0.058231, 0, 1}, {x15, > 0.0230354, 0, 1}, {x16, 0.0250354, 0, 1}, {x17, 0.055199, 0, > 1}, {x18, 0.026367, 0, 1}, {x19, 0.028367, 0, 1}, {x20, 0.051339, > 0, 1}, {x21, 0.028735, 0, 1}}] > > Now every time i want to find the solution for each n and d i manually > type their values > e.x. > n=10^4 > d=10^6 > > and then i get the answer from FindRoot for all the x1,x2,....x21 > > What i want to do is calculate x1,x2,...x21 for n ranging from 10^4 to > 10^9 and d ranging from 10^4 to 10^11. > > And in the end I want to plot the results (for a given d) in a graph > where the y axis is one of the x1,x2,..x21 and the x axis is n > > I tried creating a table first so i can plot its contents but it > doesn't seem to be working.... > Table[n, d, > Evaluate[MyFunction[n, d]], {n, 10^4, 10^9, 10^4}, {d, 10^4, 10^9, > 10^11}] > > I would appreciate all the tips you can give me > > Dimitris Three things. First the notation. I would use x[1], x[2], ... instead of x1, x2, ... . ClearAll[x]; u = Array[x,21] gives u = {x[1], ..., x[21]}, and the constraint simplifies to Tr[u] == 1. Initialization becomes more flexible. Your initial values were init = {.0272275467214981, .0271731, .04279, .042534, .0548527, .054198, .0627144, .0614572, .066180, .064208, .075882, .062862, .06160, .058231, .0230354, .0250354, .055199, .026367, .028367, .051339, .028735}; and FindRoot[equations, Thread@{u, init, 0, 1}] is equivalent to what you wrote. However, the advantage of doing it this way is that you would no longer be forced to enter the initial values manually -- you could let Mathematica compute and normalize them. FindRoot[equations, Thread@{u, init/Tr@init, 0, 1}] will work for any list of positive values, which only need to be proportional to, not equal to, the desired actual initial values. It would probably also be a good idea to index y as y[1], ..., y[21] or y[[1]], ..., y[[21]] instead of y1, ..., y21. Second, your unknowns are proportions, that must add to 1. In such situations it is often better to solve for the logs of the relative proportions and to impose the constraint implicltly rather than explicitly. That is, instead of solving for x[1], ..., x[21], with the side condition that x[1] + ... + x[21] == 1, solve for t[1], ..., t[20], with t[21] = 0 and x[[i]] = E^t[i] / (E^t[1] + ... + E^t[21]). ClearAll[t]; u = Array[t,20]; x = #/Tr@# & @ Append[E^u, 1.]; In terms of initial x-values, the initial t-values would be tinit = Log[Most@xinit/Last@xinit]. Finally, there is the matix you attempted to create. Do you really want 10^5 rows? What d-increment do you want? I would start with a small table, with logarithmic increments: Table[{n = 10^logn, d = 10^logd, MyFunction[n,d]}, {logn, 4, 9}, {logd, 4, 11}]