user3771983
user3771983

Reputation: 47

Parabolic equation producing odd results

I have looked on the forums for help with this but to tell you the truth I have no idea what to search for and I have no error to go by so here goes. I am trying to plot a parabola. I have plot it in excel already so I know what it should look like. However when I plot it in Python using Matplotlib, instead of the smooth curve I expect to get, I get a jagged edge. I have zoomed in and solved for specific values of x and found the python solution to be incorrect. I have copied my code below and will include an example of a calculation.

  thacker_curved_final__author__="ahe"
__date__ ="$20-Aug-2014$"

import numpy as np
import matplotlib.pyplot as plt 
import math 
import sys
from math import sqrt  
import decimal

n=5
t=0
l=100000.0
h0=100
g=9.81
l2=l**2.0

nx, ny = (1001,1001)
x5 = np.linspace(-100000,100000,nx)
y5 = np.linspace(-100000,100000,ny)
xv,yv = np.meshgrid(x5,y5)
x = np.arange(-100000,100200,200)
y = np.arange(-100000,100200,200)
t59=np.arange (1,1002002,1)

# Building the parabolic basin (Bottom)
zf=np.arange(len(x))
for i in range (len(x)):
   zf[i]=((1.0*(10.0**-8.0))*(x[i]**2.0))-100

plt.figure(1)
plt.plot(x,zf)
plt.show()

Example: Take x to be 200. Substituting that into the equation:

Zf = (1*10^-8(x^2))-100

I get that Zf = -99.9996, however in the plot Zf is equal to -99.0.

As I said I have no idea what is the cause of this (fairly new to Python) so any help would be appreciated.

Upvotes: 2

Views: 149

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122032

np.arange builds an array of dtype('int32'), so any numbers put into that array will be truncated. Instead, specify that the array should hold floating point numbers:

zf = np.arange(len(x), dtype=np.float_)

For example:

>>> a = np.arange(2)
>>> a[1] = 0.8
>>> a
array([0, 0])
>>> a = np.arange(2, dtype=np.float_)
>>> a[1] = 0.8
>>> a
array([ 0. ,  0.8])

Upvotes: 1

Related Questions