Ask Your Question
1

How Do I Create/Plot Array of Calculated Values

asked 2015-02-25 20:03:05 +0100

j0nr gravatar image

updated 2015-02-26 15:47:55 +0100

slelievre gravatar image

Hi All,

First time user of SageMath here. Just trying to find out how to do things.

I wish to calculate the components of a rotating force of an object rotating around a fixed axis. The basic formula is $$mrw^2\cos\theta,$$ where $m$, $r$, $w$ are known values and $\theta$ is a simple angle from 0 to 360 degree, i.e. 1 revolution.

In a spreadsheet I would just have a column with values of $\theta$ (e.g. 0,5,10,15,...,350,355,360) and then in the next column I would have $mrw^2\cos \theta$, copy it down and then I could plot XY ($\theta$ vs $mrw^2$).

In a programmatic way, I would do something like

- create an array,
- for x = 0 to 360 in increments of 5:
      array(x) = m*r*w^2*cos(x)

You see how I'm going. How do I accomplish this in Sagemath? It's just about learning the syntax methinks, as I know what I want to achieve, it's just how.

Love the look of Sagemath, would really like to use it a lot.

Thanks in advance.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2015-02-25 21:47:35 +0100

dazedANDconfused gravatar image

I've chosen some values to have something to work with. Go to a Sage Cell Server and copy/paste the following code in:

m = 5
r = 3
w = 6
x_coords = [t for t in range(0,360,5)]
y_coords = [m*r*w^2*cos(t*pi/180).n(digits=5) for t in range(0,360,5)]
list_plot([(x_coords[i],y_coords[i]) for i in range(0,len(x_coords))])

Press "Evaluate" and you'll get the plot. Creating the arrays: The first array (actually a list in Python) has the x coordinates, the second list creates the y values. Note that range(0,360,5) will not include 360. Plotting the data: list_plot is used to graph the points. The len(x_coords) is the length of the list (number of x values).

edit flag offensive delete link more
1

answered 2015-02-25 23:30:40 +0100

tmonteil gravatar image

updated 2015-02-25 23:36:03 +0100

To complement @dazedANDconfused answer, which is correct, you can first notice that you do not need to write arrays (they are named lists in Python) if you only want to plot the function, you can simply do:

sage: m = 5
sage: r = 3
sage: w = 6
sage: f(t) = m*r*w^2*cos(t*pi/180)
sage: plot(f, t, 0, 360)

Second, if you want to deal with X and Y lists, you can use the Python zip function to avoid iterating among indexes 0...len(x_coords)-1 by hand:

sage: zip([1,2,3],[4,5,6])
[(1, 4), (2, 5), (3, 6)]

So in your case, you can write the less verbose:

sage: list_plot(zip(x_coords,y_coords))

The following also works:

sage: points(zip(x_coords,y_coords))
edit flag offensive delete link more

Comments

Ah, excellent, thank you very much. This is very helpful, both of you.

j0nr gravatar imagej0nr ( 2015-02-26 10:50:39 +0100 )edit
0

answered 2015-02-26 16:23:48 +0100

slelievre gravatar image

The answers by @dazedANDconfused and @tmonteil are correct.

To save time if you were doing computations on a larger scale, you could also

  • use a numerical value for pi instead of the symbolic version of pi,
  • calculate $mrw^2$ once and for all,
  • calculate the ratio pi/180 once and for all,
  • use a lambda function instead of a symbolic function.

Combining those, you could write:

sage: m, r, w = 5, 3, 6
sage: mrw2 = m * r * w^2
sage: deg_rad = RDF.pi() / 180    # degree to radian conversion factor
sage: ff = lambda t: mrw2 * cos(t * deg_rad)
sage: plot(ff, 0, 360)

Of course, this is a bit pointless here: who cares if it takes 4 ms or 40 ms for computing the plot, when displaying the plot takes much longer than that.

edit flag offensive delete link more

Comments

Always like tips for neater/minimal code, thanks.

j0nr gravatar imagej0nr ( 2015-02-27 12:39:47 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-02-25 20:03:05 +0100

Seen: 2,769 times

Last updated: Feb 26 '15