Error when trying to graph an arbitrary 3d function - python

I'm working on a project for my multivariable calculus class, My objective is to graph an arbitrary function f(x,y), use contour plots to graph the partial derivatives (df/dx, df/dy) and a quiver plot for the gradient of the function, but I have an issue while graphing more complicated functions.
For function inputs like f(x,y) = (x+y)**2 the program works fine an outputs the graph, but when I use an input that requires a more complicated mathematical concept (i.e: f(x,y) = sin(x*y). I get an error:
TypeError: only length-1 arrays can be converted to Python scalars.
There are a lot of cases of this on stackoverflow, but they all seem to be isolated incidents involving numpy/sympy conflicts. In my program, I am reliant on sympy for creating arbitrary functions, and numpy for array computation, so I'm not sure how to get around this problem.
'''
Imports
'''
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np
from sympy import *
x = Symbol('x')
y = Symbol('y')
lims = [-10, 10]
function = sin(x+y)
lam_function = lambdify((x,y),function)
fig = plt.figure()
ax = fig.gca(projection='3d')
gX, gY = np.meshgrid(np.arange(lims[0], lims[1], 0.05),
np.arange(lims[0], lims[1], 0.05))
z = lam_function(gX, gY)
plot = ax.plot_surface(gX, gY, z, cmap=plt.cm.jet, linewidth=0)
plt.colorbar(plot, cmap=plt.cm.jet)
plt.show()

The solution is easy: You need to specify the which package to use with lambdify
lam_function = sym.lambdify((x,y), function, "numpy")
This will ensure that the resulting functions will be numpy compatible. This works for basic functions like sin, cos, atan, log but may fail for more complicated ones such as sympy.lowergamma.
Now as to why polinamials work without specifiying "numpy" is easy to understand if we have a close look at sympy's documentation. If no package is specified sympy will try python-math, numpy and mpmath in exactly this order. Now a python-math x is not really different from a numpy x, but python-math sin is a lot different as it can not handle numpy arrays.
One last thing: Newer versions of sympy (1.1.1+) behave differently. In these new versions sympy.lambdify will try to use numpy as default if installed if not resort to math, mpmath, sympy.

Related

sympy "Invalid limits given" when plotting hyperbolic integral

I'm trying to do some calculations in sympy but keep getting an "Invalid limits given" error when I try and plot my function. I'm new to python and sympy so I'm sure it's an obvious mistake but I just cant understand how the limits are invalid.
import sympy
x = sympy.symbols('x')
min_x, max_x = -6.0, 6.0
func = x * sympy.integrate(x * sympy.tanh(x), (x, min_x, max_x))
p = sympy.plot(func)
In a few words, the plotting module converts the symbolic expression to a numerical function, evaluates it and create the plot. However, the current plotting module is using out-of-date code to convert the symbolic expression to a numerical function. That error simply means that the plotting module can't plot the expression.
You can plot your expression in two different ways. The first: convert the symbolic expression to a numerical function, use numpy and matplotlib:
import matplotlib.pyplot as plt
import numpy as np
f = sympy.lambdify(x, func)
plt.figure()
xx = np.linspace(-10, 10)
yy = f(xx)
plt.plot(xx, yy)
The second one: you install the new and more advanced plotting module, Sympy Plot Backend. Then:
from spb import *
plot(func)

How to get piecewise linear function in Python

I would like to get piecewise linear function from set of points. Here is visual example:
import matplotlib.pyplot as plt
x = [1,2,7,9,11]
y = [2,5,9,1,11]
plt.plot(x, y)
plt.show()
So I need a function that would take two lists and would return piecewise linear function back. I do not need regression or any kind of least square fit.
I can try to write it myself, but wonder if there is something already written. So far, I only found code returning regression
try np.interp. It interpolates the values.
Here is a small example.
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> x = [1,2,7,9,11]
>>> y = [2,5,9,1,11]
>>> np.interp([1.5, 3], x, y)
array([ 3.5, 5.8])
A caution note is to make sure for the sample points, make sure the x increases.

Scipy: efficiently generate a series of integration (integral function)

I have a function, I want to get its integral function, something like this:
That is, instead of getting a single integration value at point x, I need to get values at multiple points.
For example:
Let's say I want the range at (-20,20)
def f(x):
return x**2
x_vals = np.arange(-20, 21, 1)
y_vals =[integrate.nquad(f, [[0, x_val]]) for x_val in x_vals ]
plt.plot(x_vals, y_vals,'-', color = 'r')
The problem
In the example code I give above, for each point, the integration is done from scratch. In my real code, the f(x) is pretty complex, and it's a multiple integration, so the running time is simply too slow(Scipy: speed up integration when doing it for the whole surface?).
I'm wondering if there is any way of efficient generating the Phi(x), at a giving range.
My thoughs:
The integration value at point Phi(20) is calucation from Phi(19), and Phi(19) is from Phi(18) and so on. So when we get Phi(20), in reality we also get the series of (-20,-19,-18,-17 ... 18,19,20). Except that we didn't save the value.
So I'm thinking, is it possible to create save points for a integrate function, so when it passes a save point, the value would get saved and continues to the next point. Therefore, by a single process toward 20, we could also get the value at (-20,-19,-18,-17 ... 18,19,20)
One could implement the strategy you outlined by integrating only over the short intervals (between consecutive x-values) and then taking the cumulative sum of the results. Like this:
import numpy as np
import scipy.integrate as si
def f(x):
return x**2
x_vals = np.arange(-20, 21, 1)
pieces = [si.quad(f, x_vals[i], x_vals[i+1])[0] for i in range(len(x_vals)-1)]
y_vals = np.cumsum([0] + pieces)
Here pieces are the integrals over short intervals, which get summed to produce y-values. As written, this code outputs a function that is 0 at the beginning of the range of integration which is -20. One can, of course, subtract the y-value that corresponds to x=0 in order to have the same normalization as on your plot.
That said, the split-and-sum process is unnecessary. When you find an indefinite integral of f, you are really solving the differential equation F' = f. And SciPy has a built-in method for that, odeint. Just use it:
import numpy as np
import scipy.integrate as si
def f(x):
return x**2
x_vals = np.arange(-20, 21, 1)
y_vals = si.odeint(lambda y,x: f(x), 0, x_vals)
The output is essential identical to the first version (within tiny computational errors), with less code. The reason for using lambda y,x: f(x) is that the first argument of odeint must be a function taking two arguments, the right-hand side of the equation y' = f(y, x).
For the equivalent version of user3717023's answer using scipy's solve_ivp you need to keep in mind the different ordering of x and y in the function f (different from the odeint version).
Further, keep in mind that you can only compute the solution up to a constant. So you might want to shift the result according to some given condition. In the example here (with the function f(x)=x^2 as given by the OP), I shifted the numeric solution such that it goes through the origin, matching the simplest analytic solution F(x)=x^3/3.
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
def f(x):
return x**2
xs = np.linspace(-20, 20, 1001)
# This is the integration step:
sol = solve_ivp(lambda x, y: f(x), t_span=(xs[0], xs[-1]), y0=[0], t_eval=xs)
plt.plot(sol.t, sol.t**3/3, ls='-', c='C0', label="analytic: $F(x)=x^3/3$")
plt.plot(sol.t, sol.y[0], ls='--', c='C1', label="numeric solution")
plt.plot(sol.t, sol.y[0] - sol.y[0][sol.t.size//2], ls='-.', c='C3', label="shifted solution going through origin")
plt.legend()
In case you don't have an analytical version of the function f, but only xs and ys as data points, then you can use scipy's interp1d function to interpolate between the data points and pass on that interpolating function the same way as before:
from scipy.interpolate import interp1d
f = interp1d(xs, ys)

using Odeint to solve two first-order differential equations with a vector as input [duplicate]

i am a newbie to python. I have a simple differential systems, which consists of two variables and two differential equations and initial conditions x0=1, y0=2:
dx/dt=6*y
dy/dt=(2t-3x)/4y
now i am trying to solve these two differential equations and i choose odeint. Here is my code:
import matplotlib.pyplot as pl
import numpy as np
from scipy.integrate import odeint
def func(z,b):
x, y=z
return [6*y, (b-3*x)/(4*y)]
z0=[1,2]
t = np.linspace(0,10,11)
b=2*t
xx=odeint(func, z0, b)
pl.figure(1)
pl.plot(t, xx[:,0])
pl.legend()
pl.show()
but the result is incorrect and there is a error message:
Excess work done on this call (perhaps wrong Dfun type).
Run with full_output = 1 to get quantitative information.
I don't know what is wrong with my code and how can i solve it.
Any help will be a useful to me.
Apply trick to desingularize the division by y, print all ODE function evaluations, plot both components, and use the right differential equation with the modified code
import matplotlib.pyplot as pl
import numpy as np
from scipy.integrate import odeint
def func(z,t):
x, y=z
print t,z
return [6*y, (2*t-3*x)*y/(4*y**2+1e-12)]
z0=[1,2]
t = np.linspace(0,1,501)
xx=odeint(func, z0, t)
pl.figure(1)
pl.plot(t, xx[:,0],t,xx[:,1])
pl.legend()
pl.show()
and you see that at t=0.64230232515 the singularity of y=0 is assumed, where y behaves like a square root function at its apex. There is no way to cross that singularity, as the slope of y goes to infinity. At this point, the solution is no longer continuously differentiable, and thus this is the extremal point of the solution. The constant continuation is an artifact of the desingularization, not a valid solution.

Python: Integration of Interpolation

I've got some question I cant solve:
#! /usr/bin/env python
import numpy as np
from scipy.interpolate import UnivariateSpline
from scipy.integrate import quad
import pylab as pl
x = ([0,10,20,30,40,50,60,70,...,4550,4560])
y = ([0,0,0,0,0,0,0,3,2,3,2,1,2,1,2,...,8,6,5,7,11,6,7,10,6,5,8,13,6,8,8,3])
s = UnivariateSpline(x, y, k=5, s=5)
xs = np.linspace(0, 4560, 4560)
ys = s(xs)
This is my code for making some Interpolation over some data.
In addition, I plotted this function.
But now I want to integrate it (from zero to infinity).
I tried
results = integrate.quad(ys, 0, 99999)
but it didnt work.
Can you give me some hints (or solutions) please? thanks
As Pierre GM said, you have to give a function for quad (I think also you can use np.inf for the upper bound, though here it doesn't matter as the splines go to 0 quickly anyways). However, what you want is:
s.integral(0, np.inf)
Since this is a spline, the UnivariateSpline object already implements an integral that should be better and faster.
According to the documentation of quad, you need to give a function as first argument, followed by the lower and upper bounds of the integration range, and some extra arguments for your function (type help(quad) in your shell for more info.
You passed an array as first argument (ys), which is why it doesn't work. You may want to try something like:
results = quad(s, xs[0], xs[-1])
or
results = quad(s, 0, 9999)

Categories

Resources