Message Boards Message Boards

0
|
5972 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Create a table containing the numerical results of FindRoot?

I am trying to create an array, or rather just a table, from the results of the root finding procedure, calculated with a set of input parameters. This is what I wrote:

pvec = {0.03, 0.06, 0.09, 0.12, 0.15}
 Xpvec = Table[0, 5]
While[n < 6, 
 FindRoot[1 - 3/(8 Xp^3) (2 Xp^2 - 1 + (1 + 2 Xp) Exp[-2 Xp]) == 
   pvec[[n]], {Xp, 0.01, 0.5}];
 xtemp = Xp /. %; Xpvec[[n]] = xtemp; i++]

But when I run this small piece, I got a number of error messages. On the other hand, if I specify any given value of n betweeen 1 and 5, and run the kernel of the While statement, it gives the correct result, so I can build my Xpved table manually. What is wrong with the While loop?

POSTED BY: Imre Pazsit
4 Replies

With Table code is shorter;

pvec = {0.03, 0.06, 0.09, 0.12, 0.15};
Table[Xp /. FindRoot[1 - 3/(8 Xp^3) (2 Xp^2 - 1 + (1 + 2 Xp) Exp[-2 Xp]) == 
pvec[[n]], {Xp, 0.01, 0.5}], {n, 1, 5}]

(* {0.0408762, 0.0836014, 0.128334, 0.175253, 0.22456} *)
POSTED BY: Mariusz Iwaniuk

Efficient and elegant, thanks a lot!

POSTED BY: Imre Pazsit
Posted 5 years ago
pvec = {0.03, 0.06, 0.09, 0.12, 0.15};
Xpvec = Table[0, 5]; n = 1;
While[n < 6, 
  fr = FindRoot[
    1 - 3/(8 Xp^3) (2 Xp^2 - 1 + (1 + 2 Xp) Exp[-2 Xp]) == 
     pvec[[n]], {Xp, 0.01, 0.5}];
  xtemp = Xp /. fr; Xpvec[[n]] = xtemp; n++];
Xpvec

gives

{0.0408762, 0.0836014, 0.128334, 0.175253, 0.22456}
POSTED BY: Oliver Seipel

Thank you for this solution, it indeed works.

POSTED BY: Imre Pazsit
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract