IPython, Plotting a Polynomial - plot

I've been working with Sympy for an Interpolation, in which I obtain as a result a 7th degree Polynomial (ax^7 + bx^6 + cx^5 + ...+ h) which I want to Plot, but wen I try to plot it I get errors, for example, if I try:
plt.plot(r,U.subs(x,r))
where r = np.linspace(0,20,num=100) and U = Polynomial(x);
the result is an error message: ValueError: sequence too large; must be smaller than 32, I obtain MemoryError: if I try `r = np.arange(20)'. The only way I could plot it is with a for cycle, substituting one by one and saving it in another variable as a list. So my question is, what is wrong with the first's inputs? is there an easy way to plot a polynomial?

Welcome to SO!
The subs() method is not meant to be used with numpy arrays. lambdify() does what you want. Try:
import numpy as np
import matplotlib.pyplot as plt
import sympy as sy
sy.init_printing() # nice formula rendering in IPython
x = sy.symbols("x", real=True)
# the sample polynomial:
pp = x**3 + 3*x**2 - 6*x - 8
# Convert expression function usable with numpy array:
f_pp = sy.lambdify(x, pp, modules=np)
# Do the plotting:
x_n = np.linspace(-5, 2, 500)
y_n = f_pp(x_n) # evaluate all x_n
fg, ax = plt.subplots(1, 1)
ax.plot(x_n, y_n)
fg.canvas.draw()
plt.show()
The parameter modules=np ensures, that numpy is used for functions in the expression (e.g., sin() => np.sin()). In this example, it is not explicitly needed.
PS: If you include a runnable example in your question, it makes live much easier for potential answerers.

Related

InterpND for one dimension

I'm trying to use the function InterpND from openmdao.components.interp_util.interp.
No problem using it for multidimensional data - I can interpolate a 3-D tensor without problems. However I'd also like to use for 1-D data. Not sure what I am doing wrong, but when I try to do something really simple like
import numpy as np
from openmdao.components.interp_util.interp import InterpND
x = np.array([0.,1.,2.,3.,4.])
y = x**2
f = InterpND(points=x, values=y)
I get the following error message:
ValueError: There are 5 point arrays, but values has 1 dimensions
Looking at the InterpND source code it seems like I SHOULD be able to just have x and y as simple 1-D arrays.
Parameters
----------
points : ndarray or tuple of ndarray of float, with shapes (m1, ), ..., (mn, )
The points defining the regular grid in n dimensions. For 1D interpolation, this
can be an ndarray.
values : array_like, shape (m1, ..., mn, ...)
x is a 1D ndarray, y is also a 1D nd array.
This looks like a bug in OpenMDAO V3.7.0 related to the input error checking. Here is a workaround. If you put the 1-D table points into a list, it works without error:
f = InterpND(points=[x], values=y)

Plot of function, DomainError. Exponentiation yielding a complex result requires a complex argument

Background
I read here that newton method fails on function x^(1/3) when it's inital step is 1. I am tring to test it in julia jupyter notebook.
I want to print a plot of function x^(1/3)
then I want to run code
f = x->x^(1/3)
D(f) = x->ForwardDiff.derivative(f, float(x))
x = find_zero((f, D(f)),1, Roots.Newton(),verbose=true)
Problem:
How to print chart of function x^(1/3) in range eg.(-1,1)
I tried
f = x->x^(1/3)
plot(f,-1,1)
I got
I changed code to
f = x->(x+0im)^(1/3)
plot(f,-1,1)
I got
I want my plot to look like a plot of x^(1/3) in google
However I can not print more than a half of it
That's because x^(1/3) does not always return a real (as in numbers) result or the real cube root of x. For negative numbers, the exponentiation function with some powers like (1/3 or 1.254 and I suppose all non-integers) will return a Complex. For type-stability requirements in Julia, this operation applied to a negative Real gives a DomainError. This behavior is also noted in Frequently Asked Questions section of Julia manual.
julia> (-1)^(1/3)
ERROR: DomainError with -1.0:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
julia> Complex(-1)^(1/3)
0.5 + 0.8660254037844386im
Note that The behavior of returning a complex number for exponentiation of negative values is not really different than, say, MATLAB's behavior
>>> (-1)^(1/3)
ans =
0.5000 + 0.8660i
What you want, however, is to plot the real cube root.
You can go with
plot(x -> x < 0 ? -(-x)^(1//3) : x^(1//3), -1, 1)
to enforce real cube root or use the built-in cbrt function for that instead.
plot(cbrt, -1, 1)
It also has an alias ∛.
plot(∛, -1, 1)
F(x) is an odd function, you just use [0 1] as input variable.
The plot on [-1 0] is deducted as follow
The code is below
import numpy as np
import matplotlib.pyplot as plt
# Function f
f = lambda x: x**(1/3)
fig, ax = plt.subplots()
x1 = np.linspace(0, 1, num = 100)
x2 = np.linspace(-1, 0, num = 100)
ax.plot(x1, f(x1))
ax.plot(x2, -f(x1[::-1]))
ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')
plt.show()
Plot
That Google plot makes no sense to me. For x > 0 it's ok, but for negative values of x the correct result is complex, and the Google plot appears to be showing the negative of the absolute value, which is strange.
Below you can see the output from Matlab, which is less fussy about types than Julia. As you can see it does not agree with your plot.
From the plot you can see that positive x values give a real-valued answer, while negative x give a complex-valued answer. The reason Julia errors for negative inputs, is that they are very concerned with type stability. Having the output type of a function depend on the input value would cause a type instability, which harms performance. This is less of a concern for Matlab or Python, etc.
If you want a plot similar the above in Julia, you can define your function like this:
f = x -> sign(x) * abs(complex(x)^(1/3))
Edit: Actually, a better and faster version is
f = x -> sign(x) * abs(x)^(1/3)
Yeah, it looks awkward, but that's because you want a really strange plot, which imho makes no sense for the function x^(1/3).

Only getting bottom of blackbody curve rather than whole function

I'm trying to plot the blackbody distribution at T = 2.73, I'm getting a curve but not a complete one and can only see the very bottom of the curve. Any suggestions? Here is my code:
import matplotlib.pyplot as plt
from numpy import *
from scipy import *
h=6.62606957e-34
c=2.998e8
k=1.3806488e-23
T = 100
f = arange(0,10000)
E = (8*h/(c**3.0))*(f**3.0)*(1/(exp((h*f)/(k*T))-1))
plt.plot(f, E*10000)
plt.title('Black-Body spectrum for T = 2.725K')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Energy Density (f)')
plt.show()
Use something like
x = linspace(0,12,1501)
f = 10**x
eterm = exp(-(h*f)/(k*T))
E = (2*h)/(c**2) * f**3 * eterm/(1-eterm)
I introduced the eterm exponential term to convert the overflow in the exponential into a less harmful underflow.
Check your sources and compare to wikipedia on Planck's law to get the right formula.
You can now also use
plot(x,E)
to get the x axis in log-scale, since if you get the right end of the x interval wrong, the graph will be uninformatively squished to the y axis.

Plotting a Gaussian Convolution Graph

I'm writing a research paper on the SIFT algorithm, and I want to create a graphic to help explain the concept of a Gaussian blur in the context of continuous functions before I describe the discrete process. The graphic I want to create is a graph of a standard Gaussian convolved with a sine function. I can plot a Gaussian, and I can plot sine, but I don't know how to plot their convolution. I don't know how to calculate their convolution to plot it, and I don't know of any software that will allow me to use a convolution operator in the plot. I am familiar with tikz and gnuplot, but I would not know how to do this with either of them. Any suggestions as to how I could go about this would be greatly appreciated. Thanks.
You could use python's matplotlib and np.convolve
Please see the following code
__author__ = 'kgeorge'
import os
import numpy as np
import math
import matplotlib.pyplot as plt
from matplotlib import gridspec
#create gaussian for the x values in x-axis
def create_gaussian(x_axis):
sigma = 1.0
denom = math.sqrt(2 * math.pi) * sigma
twoSigmaSq = 2.0*sigma**2
e=np.zeros_like(x_axis)
for i,x in enumerate(x_axis):
e[i]=math.exp (-(x*x)/twoSigmaSq)
e = e / denom
return e
def main():
#x_axis
sz=100
halfW = int(sz/2)
x_axis=np.linspace(-halfW, halfW, 1000)
#cos fun
cos_f=np.cos(x_axis)
#gaussian
gaussian_f=create_gaussian(x_axis)
fig = plt.figure()
gs = gridspec.GridSpec(3, 1)
ax1 = fig.add_subplot(gs[0,0])
ax1.plot(x_axis, cos_f)
ax1.set_title('cos')
ax2 = fig.add_subplot(gs[1,0])
ax2.plot(x_axis, gaussian_f)
ax2.set_title('gaussian')
ax3 = fig.add_subplot(gs[2,0])
convolved_ret=np.convolve(cos_f, gaussian_f, mode='same')
ax3.plot(x_axis, convolved_ret)
ax3.set_title('cos convolved with gaussian')
gs.update(wspace=0.5, hspace=0.5)
plt.show()
Please see the output here.

clean up plot of tan(x)

I want to visualize the roots of tan(xi) = tanh(xi), xi>0 and my plot
plot(tan(pi*xi), tanh(pi*xi), (xi, 0, 4), ylim=(-1, 2))
comes out like this
where one sees the actual roots, xi_i \approx pi*(n+1/4), n=1, ... but also
fake roots at pi*(n+1/2), the reason why being sympy plotting algorithm that draws a vertical line between plus and minus infinity.
I tried to avoid the adaptive sampling and using a low sampling rate to no avail. Other programs, eg gnuplot, give me a more reasonable plot, at least in view of my concerns, that is...
Eventually my question is, is it possible to avoid those vertical lines in sympy's plot() function?
Sympy uses matplotlib as a backend for plotting; the root cause is that matplotlib connects the dots even around a singularity. If one plots with numpy, the direct access to y-values being plotted allows one to replace overly large numbers with nan or infinity. If staying within sympy, such tight control over numerics does not appear to be available. The best I could do is to split the range into a list of smaller ranges that do not include singularities, using the knowledge of the specific function tan(pi*x):
import math
from sympy import *
xi = symbols('xi')
xmin = 0
xmax = 4
ranges = [(xi, n-0.499, n+0.499) for n in range(math.ceil(xmin+0.5), math.floor(xmax+0.5))]
ranges.insert(0, (xi, 0, 0.499))
ranges.append((xi, math.floor(xmax+0.5) - 0.499, xmax))
plot((tanh(pi*xi), (xi, xmin, xmax)), *[(tan(pi*xi), ran) for ran in ranges], ylim=(-1, 2))
Output:
When a curve has singularities, it can plotted by segments, excluding the singularities. A tiny interval around the each singularity is built with math.nextafter(a,b) which returns a plus the smallest possible increment for a float, in direction of b.
from sympy import init_printing, symbols, plot
from sympy import singularities, Interval, tan, pi
from math import nextafter
init_printing()
x = symbols('x')
# The function to plot
y = tan(pi*x)
# Split x range at x singularities
min_x = next_x = 0
max_x = 4 # 4*pi
segments = []
undefs = singularities(y, x, domain=Interval(min_x, max_x))
for u in undefs:
# Add a subrange up to singularity for singularities within x range
if (u >= min_x) and (u <= max_x):
segments.append((x, next_x, nextafter(u, u-1)))
next_x = nextafter(u, u+1)
# Add last segment
if u <= max_x: segments.append((x, next_x, max_x))
# Plot all segments
plots = plot(*[(y, segment) for segment in segments], ylim=(-2,2), show=False)
plots.aspect_ratio = (1,1)
plots.show()
Of course you can use the same color for each curve segment.

Resources