Message Boards Message Boards

0
|
762 Views
|
24 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How can the range of the horizontal axis be extended?

Hi, everyone! This is my first time here.
As a beginner, I really need a hand with this task.
I am attempting to extend the range of the horizontal axis, but it does not work with the option DataRange.

a = Table[x, {x, 1, 10}];
b = Table[x^2, {x, 1, 10}];
tb3 = Table[{a[[i]], b[[i]]}, {i, 10}];
L1 = List[{15, 40}];
ListPlot[{tb3, L1}, DataRange -> {1, 20}]

Then, what should I do? Please, help me out here.
Thank you in advance.

POSTED BY: Edson Orati
24 Replies

Hi! I really need a hand to deal with the For loop, because s0 has not been evaluated within the loop.

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

Honestly, both s0 and v1 shouold be evaluated, but I don't know what is going on.
Why is this happening? Can someone help me, please?
Thank you in advance for your kind attention.

POSTED BY: Edson Orati
Posted 2 months ago

Hey, Edson, it's getting difficult to follow your questions, because pieces of your question are spread all over this conversation. Also, please use the codeblock formatting for your code instead of text. When you're posting your question look for the little icon that looks kinda like <>. Or just use 4 spaces after a blank line to initiate a codeblock. And make sure you paste in your actual code. If you do this, then we can copy-paste your code and be confident that we're replicating your actual situation. Also, it would really help if you'd provide all of the relevant code. I don't know how you defined F in this latest post. Maybe it's somewhere else in this conversation, but that just means it's effectively lost. And really, at this point, it's probably better to just start a whole new conversation. The initial question has been answered (I think), so you're now asking a completely different question, but nobody looking at this site knows that unless they've been following all the details. So, you're not likely to get any more "eyes" on this problem.

POSTED BY: Eric Rimbey
Posted 2 months ago

Hi, everyone! I know it seems easy, but it is driving me crazy. Let's say that we have two lists of the same size: {a,b} and {c,d}. How can we pair them up? But withou apply a function like we have in the command Outer. The resulting list should be {{a,c},{a,d},{b,c},{b,d}}. What if we have two lists of different sizes: {a,b} and {c,d,e}. How can we pair them up? The resulting list should be {{a,c},{a,d},{a,e},{b,c},{b,d},{b,e}}. Please, help me out here. I am searching for a Wolfram function that does that, but I haven't found sth yet. Thank you in advance.

POSTED BY: Updating Name

Hi, everyone! I found that Tuples[{{a,b},{c,d,e}}] does what I want. Is there another form of pairing up elements in Wolfram? Thank you.

POSTED BY: Edson Orati

Hi, everyone! I am really struggling to do something relatively simple. Please, help me out with this. I have a list of elements Qf.
I want to test if each element of Qf belongs to an interval and then a value is given as a result, but tests are made in a sequence. Here comes what I thought the solution could be, although it failed.

nQf=Length[Qf];

Table[Which[0.06<=Qf[i]<=0.6, 0.003,0.6<Qf[i]<=8,0.04,8<Qf[i]<=40,0.2],{i,1,nQf}]

Now you are right, Hans Milton, this is completely related to my original question, but I really need a hand on this, please.
What can I do in order to get a list of elements whose values are 0.003, 0.04 and 0.2? Thank you in advance for your response.

POSTED BY: Edson Orati

I did this and it worked!!!!! Wowwww!

TestQf[Qf_]:= Which[0.06<=Qf<=0.6,Return[0.003],
             0.6<Qf<=8,Return[0.04],
              8<Qf<=40,Return[0.2]]
Map[TestQf, Qf]

I am proud of myself!!!
Do you guys have another idea to solve the same problem, please share with me. Thank you.

POSTED BY: Edson Orati
Posted 2 months ago

Hi, Eric Rimbey and Hans Milton!
You two are contributing to keep me motivated in learning Wolfram.
Another intriguing question refers to the use of the FOR loop to find solutions (pairs {xi,yi}) for a two-variable function in the range [x0,xf], being deltax arbitrary in the FOR loop.

F[x,y]:= some complex (rational and ratinal-power) function involving x and y.
For fixed values of x, the values of y that satisfy the function F (or equation F==0) is determined.

w={ };For[x=x0,x<=xf,x+=deltax, s=SolveValues[F[x,y]==0,y,Reals];AppendTo[w,s]]

Actually, I found the calculation method too lengthy.
The aim is to determine the whole set of pairs {xi,yi} that represents a curve on the plot.

How could I simplify that using Wolfram functions to obtain the curve more easily?

Thank you in advance for the tips.

POSTED BY: Updating Name
Posted 2 months ago

In general, the use of Table is more concise than using For. Example:

(* Function and loop parameters *)
F[x_,y_]:=x+10*y
x0=2;
xf=12;
deltax=3;

(* For *)
w={};
For[x=x0,x<=xf,x+=deltax,s=SolveValues[F[x,y]==0,y];AppendTo[w,s]]
w
(* {{-(1/5)}, {-(1/2)}, {-(4/5)}, {-(11/10)}} *)

(* Table *)
Table[SolveValues[F[x,y]==0,y],{x,x0,xf,deltax}]
(* {{-(1/5)}, {-(1/2)}, {-(4/5)}, {-(11/10)}} *)

Then, to get a list of x,y pairs:

Table[{x,First@SolveValues[F[x,y]==0,y]},{x,x0,xf,deltax}]
(* {{2, -(1/5)}, {5, -(1/2)}, {8, -(4/5)}, {11, -(11/10)}} *)
POSTED BY: Hans Milton

Hi, Eric! Can you help me once again with another question, please? How can we combine two lists with different number of elements to form a list of pair {xi,yi} and then apply a function F[xi,yi] to each pair?
Given that L1={x1,x2} and L2={y1,y2,y3}.
A possible solution can be done as follows but there must be a clever way.
The command Transpose[] cannot be used because it only combines lists of the same size.

Thread[F[L1,a]]/.a->y1

Here one list is generated L3={F[x1,y1],F[x2,y1]}.

Thread[F[L1,a]]/.a->y2

Here another list is generated L4={F[x1,y2],F[x2,y2]}

Join[L3,L4]

I searched for a clever function like MapThread, but the command requires a list of two lists with the same number of elements. So, it cannot be used for that purpose.
How would you do in this case in order to get it more easily?
Thank you in advance for your reply.

POSTED BY: Edson Orati
Posted 2 months ago

Using Outer:

L1 = {x1, x2};
L2 = {y1, y2, y3};
Outer[F, L1, L2] // Flatten

(* {F[x1,y1],F[x1,y2],F[x1,y3],F[x2,y1],F[x2,y2],F[x2,y3]} *)
POSTED BY: Hans Milton

Wow! That's fabulous! Outer + Flatten is like a miracle. Thank you.

POSTED BY: Edson Orati
Posted 2 months ago

It depends on how you want to fill in the missing data. But first we need to determine what you're actually wanting. When you said:

form a list of pair {xi,yi}

I assumed that you want something like Thread, meaning that elements with the same index get paired up. But I see Hans' answer using Outer, which will do all pairs. So, which do you want (before applying the function F)?

L1 = {x1, x2};
L2 = {y1, y2, y3};

possibleResult1 = {{x1, y1}, {x2, y2}, {y3}}
possibleResult2 = {{x1, y1}, {x2, y2}, {padding, y3}} (* where we need to figure out what padding to use *)
possibleResult3 = {{x1, y1}, {x1, y2}, {x1, y3}, {x2, y1}, {x2, y2}, {x2, y3}}

Or do you want something else entirely?

In the meantime...

(* possiblity #1 *)
F /@ Flatten[{L1, L2}, {2}]
(* {F[{x1, y1}], F[{x2, y2}], F[{y3}]} *)

F @@@ Flatten[{L1, L2}, {2}]
(* {F[x1, y1], F[x2, y2], F[y3]} *)

(* Need to decide what to do with the dangling F[y3] or F[{y3}] *)


(* possibility #2 *)
F /@ Transpose[PadRight[{L1, L2}]]
(* {F[{x1, y1}], F[{x2, y2}], F[{0, y3}]} *)

F @@@ Transpose[PadRight[{L1, L2}]]
(* {F[x1, y1], F[x2, y2], F[0, y3]} *)

(* This assumes that 0 is an appropriate default. Different default can be specified. *)


(* possibility #3 *)
F /@ Tuples[{L1, L2}]
(* {F[{x1, y1}], F[{x1, y2}], F[{x1, y3}], F[{x2, y1}], F[{x2, y2}], F[{x2, y3}]} *)

F @@@ Tuples[{L1, L2}]
(* {F[x1, y1], F[x1, y2], F[x1, y3], F[x2, y1], F[x2, y2], F[x2, y3]} *)

Or, of course, Hans` answer provides another option.

POSTED BY: Eric Rimbey

I am having to draw a curve from the solutions of a two-variable function f[jg,jf].
The FOR loop is being used to do that.

h1 = {}; For[jf = 0.001, jf <= 0.30, jf += 0.00005, s = Max[SolveValues[f2[jg, jf] == 0, jg, Reals]]; 
 AppendTo[h1, s]]; h1 // MatrixForm;
h2 = {}; For[jf = 0.001, jf <= 0.30, jf += 0.00005, s = jf;  AppendTo[h2, s]]; h2 // MatrixForm;
n1 = Length[h1]; n2 = Length[h2]; (*n1=n2*)
tb1 = Table[{h1[[i]], h2[[i]]}, {i, n1}];

Is there a better way to determine this big list of pairs {jg,jf} that describes the curve?
Please, help me out with this task.
Thank you in advance.

POSTED BY: Edson Orati
Posted 2 months ago

How does this relate to your original question?

POSTED BY: Hans Milton

Apparently, there is no relation, but you may have seen that I am trying to make pairs with this syntax:

tb1 = Table[{h1[[i]], h2[[i]]}, {i, n1}];

It seems too lengthy for processing, doesn't it?
Thank you for your message.

POSTED BY: Edson Orati
Posted 2 months ago

Let's examine your Table expression:

tb1 = Table[{h1[[i]], h2[[i]]}, {i, n1}]

So, you have two lists, h1 and h2, and you want to pair them up. There are other ways to do this that are more idiomatic to Mathematica. I'll manufacture some lists to illustrate:

h1 = {1, 3, 5, 7};
h2 = {"a", "b", "c", "d"};
Transpose[{h1, h2}]
(* {{1, "a"}, {3, "b"}, {5, "c"}, {7, "d"}} *)

In general, you can probably find a Mathematica function that deals with the lists as lists rather by indexing into each list. There are various ways to thread through them or re-structure them or map over them or operate on them element-wise and so on and so forth.

POSTED BY: Eric Rimbey

I cannot thank you enough for that, Eric Rimbey! You are making me eager to study more Wolfram.

POSTED BY: Edson Orati
Posted 2 months ago

You used For when it would be more natural to use Table (or some other table-like function, and see my other comment about replacing Table with Transpose). Let's say can specify your sample points with an iteration expression and you want to apply a function to those points:

Table[myFunction[i], {i, 0.1, 0.3, 0.05}]
(* {myFunction[0.1], myFunction[0.15], myFunction[0.2], myFunction[0.25], myFunction[0.3]} *)

Array[myFunction, 5, {0.1, 0.3}]
(* {myFunction[0.1], myFunction[0.15], myFunction[0.2], myFunction[0.25], myFunction[0.3]} *)

Or let's say you have the sample points in a list, then you can just map over the list:

samplePointsFromSomewhere = Range[.1, .3, .05];
myFunction /@ samplePointsFromSomewhere
(* {myFunction[0.1], myFunction[0.15], myFunction[0.2], myFunction[0.25], myFunction[0.3]} *)

Now, for the specific case of h2 where you just need to generate those sample points, you did this:

h2 = {};
For[jf = 0.001, jf <= 0.30, jf += 0.00005, s = jf; AppendTo[h2, s]];

But it would be much clearer and cleaner (and probably faster) to do this:

h2 = Range[.001, .3, .00005];
POSTED BY: Eric Rimbey
Posted 2 months ago

See if this does what you want:

ListPlot[{tb3, Flatten@L1}, DataRange -> {1, 20}]
POSTED BY: Hans Milton
Posted 2 months ago

DataRange is used to re-scale the horizontal axis. If you want to extend it, use PlotRange.

tb3 = Table[{i, i^2}, {i, 10}];  (* You don't need all of the indirection you had before. *)
L1 = {{15, 40}};    (* Using List explicitly is fine, but not necessary, just use curly braces. *)
ListPlot[{tb3, L1}, PlotRange -> {{0, 20}, Automatic}]

enter image description here

POSTED BY: Eric Rimbey

Thank you. I really learned from your post.

POSTED BY: Edson Orati

Eric, I am having trouble with the command Show.

curveA=LogLogPlot[functionA,domainA,PlotRange->{{xA1,xA2},{yA1,yA2}}]
curveB=LogLogPlot[functionB,domainB,PlotRange->{{xB1,xB2},{yB1,yB2}}]
L1 is a list defined as Table[{i,0.05},{i,1.5,2,0.1}]
P1=ListLogLogPlot[L1,PlotRange->All]

Then, here comes the command Show:

Show[{curveA,curveB,P1,PlotRange->All]

Unfortunately, when I change the PlotRange with Show for what I want that to be, the plot disappears.
How can I specify the range in the option PlotRange within the command Show?
Thank you once again for your kind attention.

POSTED BY: Edson Orati
Posted 2 months ago

Since I don't have your actual functions (e.g. functionA), I can't reproduce this.

I don't know if this will help, but using completely different and arbitrary functions, this all seems to work for me:

curveA = Plot[Sin[x], {x, 1, 10}];
curveB = Plot[Cos[x], {x, 5, 20}];

Show[{curveA, curveB}]

enter image description here

Show[{curveA, curveB}, PlotRange -> All]

enter image description here

Show[{curveA, curveB}, PlotRange -> {{5, 15}, {-10, 10}}]

enter image description here

POSTED BY: Eric Rimbey
Posted 2 months ago

Eric, Mathematica fascinates me as much as your good explanation! Thank you very much.

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