Python Forum
Simple coding help: Volumes of three cubes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple coding help: Volumes of three cubes
#1
Hello Python-forums!

Im new to python and programming. Literally no experiences before so please bear with me.
For a textbook exercise the task goes as follow:

Exercise 1.4: Volumes of three cubes
We are interested in the volume V of a cube with length L: V D L3, computed for
three different values of L.
a) Use the linspace function to compute three values of L, equally spaced on the
interval Œ1; 3.
b) Carry out by hand the computation V D L3 if L is an array with three elements.
That is, compute V for each value of L.
c) In a program, write out the result V of V = L**3 when L is an array with three
elements as computed by linspace in a). Compare the resulting volumes with
your hand calculations.
d) Make a plot of V versus L.


Now...given the past examples, i think I am to use matplotlib as a module for plotting the function of V with respect of the variable V [1,3]. My attempt goes as follows:

from numpy import *
import matplotlib.pyplot as plt
L = 3                       
t = linspace(0, 30, 100)    
V = L**3 + L                
plt.plot(V, L)              
plt.xlabel('L')             
plt.ylabel('V(L)')          
plt.show()
BTW im using spyder for writing and plotting this. For some reason it plots (V,L), xlabel and ylabel, but the graph doesnt show up. I tried changing the graphics backend to automatic in preferences. Any clue?
Not sure I fully comprehend the actual exercise either...

In advance; thanks

Update:

Fixing my own mathematical mistakes and replacing variable L = 'number' with 'linspace(0,3,10) gives a chart, but I dont understand how, why or IF i fixed my own code.

New code:

from numpy import *
import matplotlib.pyplot as plt
L =linspace(0, 3, 4)    
V = L**3       
plt.plot(V, L)              
plt.xlabel('L')             
plt.ylabel('V(L)')          
plt.show()
Reply
#2
Still having problems ?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020