0

I want to plot the following y and y' functions together on the same axes in python. The x-axis values should go from -10 to 10 in 0.1 intervals: enter image description here

what I tried: I tried plotting just y' (labelled y_p) and I am getting errors.

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-10,10,0.1)

A=1
B=0.1
C=0.1
y_p = (A*np.exp((-x**2)/2))(1+(B*((2*(np.sqrt(2))*(x**3))-(3*(np.sqrt(2))*x))/((np.sqrt(6)))))
plt.plot(x,y_p)

but this generates error:

TypeError                                 Traceback (most recent call last)
<ipython-input-71-c184f15d17c7> in <module>
      7 B=0.1
      8 C=0.1
----> 9 y_p = (A*np.exp((-x**2)/2))(1+(B*((2*(np.sqrt(2))*(x**3))-(3*(np.sqrt(2))*x))/((np.sqrt(6)))))
     10 plt.plot(x,y_p)

TypeError: 'numpy.ndarray' object is not callable

​

I am sure there is a better way to do this. I'm quite new to python, so any help is truly appreciated!

3
  • ^ applies a binary operation. To use the power function, the synthax is **. Example x²: x**2 Commented Sep 21, 2021 at 8:45
  • thanks @Julien..i have corrected that bit now but still getting error in calling the y_p function..please check my edit in my post Commented Sep 21, 2021 at 8:53
  • you have a missing * sign in y_p, right before (1+B*...) Commented Sep 21, 2021 at 8:58

2 Answers 2

2

The appropriate approach would be to take it a step at a time:

You could start by defining y1 as a lambda:

y1 = lambda x: (2*np.sqrt(2)*x**3 - 3*np.sqrt(2)*x)/np.sqrt(6)

Then implement your formula:

y_p = A * np.exp(-x**2/2)*(1 + B*y1(x))

This way you have less chances of getting a typo.

Sign up to request clarification or add additional context in comments.

1 Comment

Prettier answer than mine
1

You cannot multiply implicitly (without using *) in python.

Here is your code after the correction:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-10, 10, 0.1)

A = 1
B = 0.1
C = 0.1
y_p = (A * np.exp((-x ** 2) / 2)) * (1 + (B * ((2 * (np.sqrt(2)) * (x ** 3)) - (3 * (np.sqrt(2)) * x)) / (np.sqrt(6))))
plt.plot(x, y_p)
plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.