# How Do I Create/Plot Array of Calculated Values

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.

edit retag close merge delete

Sort by » oldest newest most voted

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).

more

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))

more

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

( 2015-02-26 10:50:39 +0200 )edit

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.

more

Always like tips for neater/minimal code, thanks.

( 2015-02-27 12:39:47 +0200 )edit