Message Boards Message Boards

0
|
592 Views
|
10 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Iterating SolveValues for a function using For?

Hi, everyone! Honestly, I have learned right now how to use this dialog box. Thank you, Eric Rimbey! Well, my problem is about using the FOR loop in the following case:

f[x_,y_]:= x - 5*y - 10;
v1={};
For[y=1,y<=10,y++,s=SolveValues[f[x,y]==0,x,Reals];AppendTo[v1,s0]]

There is something wrong with this code, because s has not been evaluated at each iteration.
If there is a better way to solve with Wolfram functions, I would love to know about that.
Please, help me out in dealing with this issue. Thank you in advance for your help.

POSTED BY: Edson Orati
10 Replies

Eric Rimbey, you made me realize I can do more and work better with the aid of Mathematica. Thank you for your help. It is really increasing my willingness to learn Wolfram.

POSTED BY: Edson Orati
Posted 2 months ago

Within your For loop, you're assigning to s, but in your Append your appending s0. Is that the problem, or was s0 just a copy-paste error?

In the meantime, understand that For doesn't produce any output (well, it produces Null actually, but Null doesn't display in the UI by itself). So, if you fix the s versus s0 problem, then your code actually does work. To see the result, just evaluate v1. For me I get:

v1
(* {{15}, {20}, {25}, {30}, {35}, {40}, {45}, {50}, {55}, {60}} *)

Also, you can do this with Table rather than For, which is a bit more natural for Mathematica:

Table[SolveValues[f[x, y] == 0, x, Reals], {y, 1, 10}]
(* {{15}, {20}, {25}, {30}, {35}, {40}, {45}, {50}, {55}, {60}} *)

Notice that Table actually produces an expression as output.

POSTED BY: Eric Rimbey

Hi, Eric! In fact, I made a mistake with s and s0. Sorry for that! I have just corrected it. If you run the code, it is supposed to get a list of values for s, isn't it? However, we get a single value within a list.

POSTED BY: Edson Orati
Posted 2 months ago

In the first iteration, we'll evaluate an expression like this:

s = SolveValues[-15 + x == 0, x, Reals]

If you evaluate just that expression, it gives:

{15}

So, yes, s was set to be a list of values. There happens to be just one solution, so the length of the list is 1. Everything is working exactly as you describe.

POSTED BY: Eric Rimbey

Hey, Eric Rimbey! I am using the command SolveValues to obtain the element (reals) that satisfies an equation. In this case, the result of the computation is a 2-dimensional list, that is,

{{e1},{e2},{e3},etc}

I learned that the dimension of the list can be reduced by using the command Flatten. In addition to that, if the command Reduce is used to solve an specific equation, the result of the computation comes with a logic structure like:

x==e1 or x==e2

Also, if the command Solve or FindRoot is used, the result of the computation is always a list of rules, that means:

{x->e1,x->e2}

Questions: Is the command SolveValues only one that delivers a list of real numbers? Can we change a logic structure or rule into a real number with some Wolfram function?

Thank you for your help once again.

POSTED BY: Edson Orati
Posted 2 months ago

I honestly don't understand your question. Everything is working as expected, and everything is working as you describe. So, what's the problem exactly?

POSTED BY: Eric Rimbey

Hey, Eric Rimbey! I am using the command SolveValues to obtain the element (reals) that satisfies an equation. In this case, the result of the computation is a 2-dimensional list, that is,

{{e1},{e2},{e3},etc}

I learned that the dimension of the list can be reduced by using the command Flatten. In addition to that, if the command Reduce is used to solve an specific equation, the result of the computation comes with a logic structure like:

x==e1 or x==e2

Also, if the command Solve or FindRoot is used, the result of the computation is always a list of rules, that means:

{x->e1,x->e2}

Questions: Is the command SolveValues only one that delivers a list of real numbers? Can we change a logic structure or rule into a real number with some Wolfram function?

Thank you for your help once again.

POSTED BY: Edson Orati

I believe that there are three types of computation results (real numbers, identities and rules). Despite that, Wolfram commands operate a generic function over lists of real number. It sounds natural to you, but for me it is still something interesting (and new as a beginner).

Changing the subject, but it is still related with what you said above that For loops give Null as a result of computation. Well. I have the example of a function that when we apply the SolveValues command to it, we get a list like:

{{e1,e2},{e3,e4},{e5,e6}, ...,{},{},...{}} where e1<e2, e3<e4, etc

Do you know how to sort out lists {e1,e2} and empty list {}?

I did something like this:

hmax=Table[Max[SolveValues[F[x,y]==0,x,Reals]],{y,y0,yf,deltay}]
hmin=Table[Min[SolveValues[F[x,y]==0,x,Reals]],{y,y0,yf,deltay}]

But now hmax has negative infinite symbols and hmin has positive infinite symbol. These lists will mess up ListPlot. The function F[x,y] is something like that:

F[x_,y_]:=x/1.255-(1.127+1.127*y/x+0.1885/x)^(-1)+0.11

By the way, y0=0.0001; yf=0.5 and deltay=0.0005 Can you help me with this problem, please? Thank you very much.

POSTED BY: Edson Orati
Posted 2 months ago

Once you've generated your list (with a loop or with Table), just apply a function to the result that deletes or filters. Let's say it looks like this:

result = {{1, 2}, {3}, {4}, {5}, {}, {}, {6, 7}}

To get the non-empty results, just delete the empty ones:

DeleteCases[result, {}]
(* {{1, 2}, {3}, {4}, {5}, {6, 7}} *)

or filter on size:

Select[result, GreaterThan[0]@*Length]
(* {{1, 2}, {3}, {4}, {5}, {6, 7}} *)

or a filter that's more friendly to newcomers:

Select[result, Length[#] > 0 &]
(* {{1, 2}, {3}, {4}, {5}, {6, 7}} *)
POSTED BY: Eric Rimbey
Posted 2 months ago

Hi, everyone! Honestly, I have learned right now how to use this dialog box. Thank you, Eric Rimbey! Well, my problem is about using the FOR loop in the following case:

f[x_,y_]:= x - 5*y - 10;
v1={};
For[y=1,y<=10,y++,s=SolveValues[f[x,y]==0,x,Reals];AppendTo[v1,s0]]

There is something wrong with this code, because s has not been evaluated at each iteration.
If there is a better way to solve with Wolfram functions, I would love to know about that.
Please, help me out in dealing with this issue. Thank you in advance for your help.

POSTED BY: Updating Name
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