Re: Plot the results of Findroot

• To: mathgroup at smc.vnet.net
• Subject: [mg122793] Re: Plot the results of Findroot
• From: Alexei Boulbitch <Alexei.Boulbitch at iee.lu>
• Date: Thu, 10 Nov 2011 06:56:10 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com

```Hello, Dimitris,

One way to do what you want may be like the following. Since you do not give explicitly your equations I answer within the most simple example of two primitive equations:

Clear[y1, y2, x1, x2, n, d];

y1[n_] := x1 + x2 == n;

y2[d_] := x1 - x2 == d;

Let us define a function finding its roots for any n and d:

fR[{n_, d_}] := FindRoot[{y1[n], y2[d]}, {x1, -1}, {x2, -2}];

Let us check for the following list pairs {n, d} is {{1, 2}, {1, -1}, {3, -2}}. Map the function fR onto this list:

Map[fR, {{1, 2}, {1, -1}, {3, -2}}]

{{x1 -> 1.5, x2 -> -0.5}, {x1 -> 0., x2 -> 1.}, {x1 -> 0.5,

x2 -> 2.5}}

Now we can package this all into one function. Its argument is a list of pairs {n, d}:

solving[lst_List] :=

Module[{fR, y1, y2},

y1[n_] := x1 + x2 == n;

y2[d_] := x1 - x2 == d;

fR[{n_, d_}] := FindRoot[{y1[n], y2[d]}, {x1, -1}, {x2, -2}];

Map[fR, lst]

];

Now to address your second question I create an arbitrary list, lst, with the same n=1, and arbitrary d and solve it:

lst = Table[{1, RandomInteger[{-5, 5}]}, {10}]

sol = solving[lst]

{{1, 0}, {1, -5}, {1, -1}, {1, -1}, {1,

5}, {1, -2}, {1, -4}, {1, -4}, {1, 5}, {1, 3}}

{{x1 -> 0.5, x2 -> 0.5}, {x1 -> -2., x2 -> 3.}, {x1 -> 0.,

x2 -> 1.}, {x1 -> 0., x2 -> 1.}, {x1 -> 3., x2 -> -2.}, {x1 -> -0.5,

x2 -> 1.5}, {x1 -> -1.5, x2 -> 2.5}, {x1 -> -1.5,

x2 -> 2.5}, {x1 -> 3., x2 -> -2.}, {x1 -> 2., x2 -> -1.}}

In order to plot x1=x1({1, d}) let us now construct the list of pairs {d,x1}. Take the i,2-nd element of the list lst and the solution for x1 (that is the i,1,2-the element) of the list of the solutions and plot it:

lstForPlot = Table[{lst[[i, 2]], sol[[i, 1, 2]]}, {i, 1, Length[lst]}]

{{0, 0.5}, {-5, -2.}, {-1, 0.}, {-1, 0.}, {5,

3.}, {-2, -0.5}, {-4, -1.5}, {-4, -1.5}, {5, 3.}, {3, 2.}}

ListPlot[lstForPlot]

You get a list of pairs {d,x1} plotted as the result.

There is a possible weak point here: if your equations are non-linear, it is not necessary that your initial values for FindRoot are always good.

Hope this helps, have fun, Alexei

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

Alexei BOULBITCH, Dr., habil.

IEE S.A.

ZAE Weiergewan,

11, rue Edmond Reuter,

L-5326 Contern, LUXEMBOURG

Office phone :  +352-2454-2566

Office fax:       +352-2454-3566

mobile phone:  +49 151 52 40 66 44

e-mail: alexei.boulbitch at iee.lu<mailto:alexei.boulbitch at iee.lu>

```

• Prev by Date: Re: Import files on accessible URL and save in
• Next by Date: Re: Import files on accessible URL and save in
• Previous by thread: Re: Plot the results of Findroot
• Next by thread: Re: Plot the results of Findroot