简体   繁体   中英

plotting with bessel functions in python

When I run this code:

from scipy.optimize import minimize
import numpy as np
import matplotlib.pyplot as plt
import scipy.special as spl 

def minf(x):
    return x[0]**2 + (x[1]-1.)**2

sol = minimize(minf, [1,2])
x = np.linspace(0,10,5000)
plt.plot(x, spl.jv(3,x), '-', sol.x, -sol.fun, 'o')

I get this error: ValueError: x and y must have same first dimension

How to correctly specify the plot statement?

My objective is to plot a landscape of inputs and function values. In this case, a two dimensional set of inputs. I want to know how I can use linspace, the bessel function and plot correctly for achieving this.

I expect a plot like this, with also the optimal point marked:

https://sites.google.com/site/haripkannan/Home/plot_pdqp.png

Something is not quite right with the output from minimize . It is unclear what you are trying to do with it. Look at the output of sol , how is this supposed to be plotted?

print sol.x, sol.fun
> [ -7.45132580e-09   9.99999993e-01] 1.1104451202e-16  

Nevertheless, plotting your Bessel function is simple:

x = np.linspace(0,10,500)
y = spl.jv(3,x)
plt.plot(x, y, '-')
plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM