Message Boards Message Boards

0
|
4536 Views
|
8 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Replace the "arrows" (rules) for equal signs "="?

Posted 4 years ago

For m=0,1,…,7, I am finding the intersection point of a straight line and a sine function (S):

ystar[m_] := 
 NSolve[zbar[m] + mpw*(y - ybar[m]) - S[y, m] == 0 && 
   y < ybar[m] - 0.001, y, Reals]

In case there is more than one solution (for each m), I define

Y[m_] := Max[ystar[m]].

When I compute for m=3, I get

In[71]:= Y[3]

Out[71]= y -> 15.8865

I would like to make my answers come out with an equality sign in place of the arrow (which, I assume, indicates an approximation to the root). My next step is

In[20]:= y /. Y[3]

Out[20]= 15.8865

which seems to accomplish what I want. I would like, however, to do that for all eight numbers simultaneously, something like

y[m] /.Y[m]

which does not work. How can I define a function that gives me the numbers without the arrows? This is important to me because, in actual cases, m can run into the hundreds.

POSTED BY: jasvesta
8 Replies
Posted 4 years ago

Thank you, Rohit.

POSTED BY: jasvesta
Posted 4 years ago

SW's Mathematica book is quite old. Take a look at his newer book which is available online. For a more in-depth understanding of WL look at Leonid Shifrin's book, also available online.

POSTED BY: Rohit Namjoshi
Posted 4 years ago

It works! Thank you very much for your help. As you realize, I am an amateur when it comes to Mathematica, mainly because I do not use it very often. I now have more time in my hands and I would appreciate any suggestions you may have about books that provide hands-on tutorials, besides Prof. Wolfram's MATHEMATICA book. Thanks again!!!

POSTED BY: jasvesta
Posted 4 years ago

Mr. Namjoshi: I have uploaded part of the code. If you can find the time, please take a look. (1) The problem starts with the solution of the equation in In[16] whose output is as in In[17]. (2) In line In[18], and for each m, I take the Max of possibly more than one solutions of In[16]. (3) Between In[19] and In[21] there is a lot of code that I have omitted. It defines the functions that start with the Greek letter sigma. (4) The critical line is In[21] where a pure number is not produced for Y[m]. All I want to say there is: if Y>0, sigmaH = sigma1H; if not it equals sigma0. (5) The final output of the code should be the table in In[23]. Any help in resolving this issue will be greatly appreciated.

Attachments:
POSTED BY: jasvesta
Posted 4 years ago

Hi Jasvesta,

There are a couple of issues

Y[m_] := Max[ystar[m]]

If ystar[m] returns multiple solutions then Max will not return the rule with the largest RHS. e.g.

solutions = {{y -> 1}, {y -> 2}}
Max @ solutions
(* Max[y -> 1, y -> 2] *)

It will only work if there is one solution

solutions = {{y -> 1}}
Max @ solutions
(* y -> 1 *)

Change it to

Y[m_] := ystar[m] // MaximalBy[Last] // Extract[{1, 1}]

Since Y[m] returns a Rule, the following will not work because > is not defined for a Rule

?H[f_, ?_, m_] := If[Y[m] > 0, ?1H[f, ?, m], ?0[f, ?, m]]

Change it to

?H[f_, ?_, m_] := If[Last@Y[m] > 0, ?1H[f, ?, m], ?0[f, ?, m]]

Then evaluating the TH table gives

{{0, ?0[9.5493, 89., 0]}, {1, ?0[9.5493, 89., 1]}, 
{2, ?1H[9.5493, 89., 2]}, {3, ?1H[9.5493, 89., 3]}, 
{4, ?1H[9.5493, 89., 4]}, {5, ?0[9.5493, 89., 5]}, 
{6, ?0[9.5493, 89., 6]}, {7, ?0[9.5493, 89., 7]}}

Is that the result you expect?

POSTED BY: Rohit Namjoshi
Posted 4 years ago

Thank you very much for your help. What I would like to do is to define a function that will give me the numerical values of Y without the arrow. My try f does not work. The arrow is still there. Is there another way to define such a function?

POSTED BY: jasvesta
Posted 4 years ago

Not sure why you need a function to extract the RHS.

Simulate function Y that returns a Rule

Y[x_] := y -> x^2
Y[3]
(* y -> 9 *)

Extract RHS

Y[3] // Last
(* 9 *)

Extract RHS for Y[1] to Y[8]

Last /@ Y /@ Range[8]
(* {1, 4, 9, 16, 25, 36, 49, 64} *)

If for some reason you do need a function then

f[r_] := Last@r

f[Y[6]]
(* 36 *)
POSTED BY: Rohit Namjoshi
Posted 4 years ago

Hi Jasvesta,

-> defines a Rule, not an approximation. You can extract the RHS from a list of rules e.g.

ClearAll[x, y, z];
rules = {x -> 1, y -> 2, z -> 3};
rules[[All, 2]]
(* {1, 2, 3} *)

If you want x, y, z to be set to the corresponding RHS then

Apply@Set /@ rules;
{x, y, z}
(* {1, 2, 3} *)

Or using your example

y[#] /. Y[#] & /@ Range@8

Is that what you are looking for?

POSTED BY: Rohit Namjoshi
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