Message Boards Message Boards

0
|
9173 Views
|
9 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Generate a table from Solve or NSolve and plot it?

Posted 5 years ago

I have a simple question. I want to generate a list. To make things simple, I use a simple model.

xt = Table[{NSolve[x^2 - .001 i == 0 && x > 0]}, {i, 8}];

This generates a table, but when I inquire the value of xt, I got an answer like

xt[[4]]

Answer is

{{{x -> 0.0632456}}}

How can I ListPlot xt? If I want to ListPlot xt versus another table, how can I do it? Much thanks to anyone who can tell me.

POSTED BY: Hong-Yee Chiu
9 Replies

Helpful information shared thank you for helping out others.

POSTED BY: Ruth Davidson
Posted 5 years ago

I think I have found the solution. The following three operations In xt = Table[{NSolve[x^2 - .001 i == 0 && x > 0]}, {i, 8}];

In[25]:= { {xt1 = xt /. {{Rule[a, b]}} :> b;}, {[Placeholder]} }

Out[25]= {{Null}, {[Placeholder]}}

In[26]:= xt1

Out[26]= {{0.0316228}, {0.0447214}, {0.0547723}, {0.0632456}, \ {0.0707107}, {0.0774597}, {0.083666}, {0.0894427}}

generates a 8 d vector, and by using Transpose, In xxt = Transpose[xt1]

Out[33]= {{0.0316228, 0.0447214, 0.0547723, 0.0632456, 0.0707107, 0.0774597, 0.083666, 0.0894427}} Taking the first element, In[34]:= xxt[[1]]

Out[34]= {0.0316228, 0.0447214, 0.0547723, 0.0632456, 0.0707107, \ 0.0774597, 0.083666, 0.0894427} this is the result I wanted. Thanks to you, because you, ivar johannesen,, oslomet, inspired me to look for transpose. This is the greatness of forum discussions, generate inspirations. Hong-Yee Chiu

POSTED BY: Hong-Yee Chiu
Posted 5 years ago

I followed your suggestion,here is what I got: In xt = Table[{NSolve[x^2 - .001 i == 0 && x > 0]}, {i, 8}];

In[25]:= { {xt1 = xt /. {{Rule[a, b]}} :> b;}, {[Placeholder]} }

Out[25]= {{Null}, {[Placeholder]}} In[26]:= xt1

Out[26]= {{0.0316228}, {0.0447214}, {0.0547723}, {0.0632456}, \ {0.0707107}, {0.0774597}, {0.083666}, {0.0894427}}

In[27]:= Table[xt1[[n, 2]], {n, 8}]

During evaluation of In[27]:= Part::partw: Part 2 of {0.0316228} does not exist.

During evaluation of In[27]:= Part::partw: Part 2 of {0.0447214} does not exist.

During evaluation of In[27]:= Part::partw: Part 2 of {0.0547723} does not exist.

During evaluation of In[27]:= General::stop: Further output of Part::partw will be suppressed during this calculation.

Out[27]= {{{0.0316228}, {0.0447214}, {0.0547723}, {0.0632456}, \ {0.0707107}, {0.0774597}, {0.083666}, {0.0894427}}[[1, 2]], {{0.0316228}, {0.0447214}, {0.0547723}, {0.0632456}, \ {0.0707107}, {0.0774597}, {0.083666}, {0.0894427}}[[2, 2]], {{0.0316228}, {0.0447214}, {0.0547723}, {0.0632456}, \ {0.0707107}, {0.0774597}, {0.083666}, {0.0894427}}[[3, 2]], {{0.0316228}, {0.0447214}, {0.0547723}, {0.0632456}, \ {0.0707107}, {0.0774597}, {0.083666}, {0.0894427}}[[4, 2]], {{0.0316228}, {0.0447214}, {0.0547723}, {0.0632456}, \ {0.0707107}, {0.0774597}, {0.083666}, {0.0894427}}[[5, 2]], {{0.0316228}, {0.0447214}, {0.0547723}, {0.0632456}, \ {0.0707107}, {0.0774597}, {0.083666}, {0.0894427}}[[6, 2]], {{0.0316228}, {0.0447214}, {0.0547723}, {0.0632456}, \ {0.0707107}, {0.0774597}, {0.083666}, {0.0894427}}[[7, 2]], {{0.0316228}, {0.0447214}, {0.0547723}, {0.0632456}, \ {0.0707107}, {0.0774597}, {0.083666}, {0.0894427}}[[8, 2]]} I cannot get the anticipated table form like following: {0.0316228, 0.0447214, 0.0547723, 0.0632456, 0.0707107, \ 0.0774597, 0.083666, 0.0894427} Anything wrong? Thanks

POSTED BY: Hong-Yee Chiu

Sorry, I ment Table[xt1[[n,2]],{n,8}]

POSTED BY: ivar johannesen

Table[xt[[n,2]],{n,8}]

POSTED BY: ivar johannesen

xt is a list of elements containing two components: the x-value of the intended plot and - in some nested form - the y-value (the result of NSolve). I extracted this value simply using a replacement rule, which could as well be written like so:

xt1 = xt /. {{a_ -> b_}} :> b

which is probably more similar to what you are seeing in xt.

General hint: Use FullForm in combination with the excellent documentation, e.g.:

{{a_ -> b_}} :> b // FullForm
POSTED BY: Henrik Schachner
Posted 5 years ago

Thank you. I got the following answers.

In[16]:= xt = Table[{NSolve[x^2 - .001 i == 0 && x > 0]}, {i, 8}];

In[21]:= {
 {xt1 = xt /. {{Rule[a_, b_]}} :> b;},
 {\[Placeholder]}
}

Out[21]= {{Null}, {\[Placeholder]}}

In[23]:= xt1

Out[23]= {{0.0316228}, {0.0447214}, {0.0547723}, {0.0632456}, \
{0.0707107}, {0.0774597}, {0.083666}, {0.0894427}}

Now, how can I get rid of the parenthesis so that the answer will look like

 {0.0316228, 0.0447214, 0.0547723, 0.0632456, \
0.0707107, 0.0774597, 0.083666, 0.0894427}
so that I can list plot xt1 with another table, like
yt1=(1, 2, 3, 4, 5, 6, 7, 8}

?

Much thanks for your time.

POSTED BY: Updating Name
Posted 5 years ago

Thanks, it works. Can you spare a few moments to explain what you are doing? It will be a great education to me.

POSTED BY: Hong-Yee Chiu

A very simple way would be:

xt = Table[{i, NSolve[x^2 - .001 i == 0 && x > 0]}, {i, 8}];
xt1 = xt /. {{Rule[a_, b_]}} :> b;
ListPlot[xt1]
POSTED BY: Henrik Schachner
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