Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a matematical equation on Python

I'm try to plot a graph on python using matplotlib, but i can't to get a expected result.

Equation 1: 1/2 * (w1 + w2) + 1/2 * np.sqrt(4*(g)**2 + (w2-w1)**2)

Equation 2: 1/2 * (w1 + w2) - 1/2 * np.sqrt(4*(g)**2 + (w2-w1)**2)

My python code:

import matplotlib.pyplot as plt
import numpy as np

def W_p(w1,w2,g):
   return 1/2 * (w1 + w2) + 1/2 * np.sqrt(4*(g)**2 + (w2-w1)**2)

def W_m(w1,w2,g):
    return 1/2 * (w1 + w2) - 1/2 * np.sqrt(4*(g)**2 + (w2-w1)**2)

w1 = np.linspace(-10,10,1)
w2 = np.linspace(0.9,1.10,0.1)
g = 1

plt.plot(W_p(w1,w2,g), 'r', W_m(w1,w2,g), 'b')

plt.show()

The generatad graph should look like the graph on the image below:

like image 381
Hagzel Avatar asked Dec 08 '22 20:12

Hagzel


1 Answers

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

Return evenly spaced numbers over a specified interval.

Returns num evenly spaced samples, calculated over the interval [start, stop].

The endpoint of the interval can optionally be excluded.

Parameters:

start : scalar The starting value of the sequence.

stop : scalar The end value of the sequence, unless endpoint is set to False. In that case, the sequence consists of all but the last of num + 1 evenly spaced samples, so that stop is excluded. Note that the step size changes when endpoint is False.

num : int, optional Number of samples to generate. Default is 50. Must be non-negative.

....

Must be change

w1 = np.linspace(-10,10,1)
w2 = np.linspace(0.9,1.1,0.1)

to

w1 = np.linspace(-10,10,100)
w2 = np.linspace(0.9,1.1,100)

enter image description here

like image 171
eyllanesc Avatar answered Dec 22 '22 00:12

eyllanesc