Hari
Hari

Reputation: 1815

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

Upvotes: 0

Views: 7179

Answers (1)

Hooked
Hooked

Reputation: 88208

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()

enter image description here

Upvotes: 1

Related Questions