How to plot integration equation using Python?

In summary, I was trying to code an equation into Python and it produced a different graph than the original. I am not sure why this is happening and I need help to fix it.
  • #1
Nur Ziadah
35
3
I have a few of integration equations and need to convert it into Python. The problem is when I tried to plot a graph according to the equation, some of the plot is not same with the original one.

The first equation is the error probability of authentication in normal operation:
upload_2018-12-29_12-2-16.png

cond equation is the error probability of authentication under MIM attack:
upload_2018-12-29_12-2-48.png

The error probability of authentication in normal operation can be calculated by:
upload_2018-12-29_12-3-56.png

Supposedly, the graph (original) will be shown like this:
upload_2018-12-29_12-4-42.png


y-axis: error probability
x-axis: N
Pe^normal = blue lines
Pe^MIM = red lines
Differences between two error probabilities (Pe^MIM - Pe^normal)= green lines

I tried to code it into Python and this is my full codes:

Python:
import matplotlib.pyplot as plt
import math
import numpy as np
from scipy.special import iv,modstruve

x=np.arange(-0.5,21,1)
x = np.array(x)
t = 0.9
pe_normal = (np.exp(t*x/2)*(iv(0, t*x/2) - modstruve(0,t*x/2))-1)/(np.exp(t*x)-1)
pe_diff = (np.exp((1-t**2)*x/2)*(iv(0, (1-t**2)*x/2) - modstruve(0,(1-t**2)*x/2))-1)/(np.exp((1-t**2)*x)-1)
pe_mim= np.add(pe_normal,pe_diff)plt.plot(x, pe_normal, '-', color='blue', label='Normal')
plt.plot(x, pe_mim,  '-', color='red', label='MIM')
plt.plot(x, pe_diff,  '-', color='green', label='DIFF')
plt.xlabel('Mean photon number N')
plt.ylabel('Error probabiity')
plt.text(10, 0.4, 't=0.9', size=12, ha='center', va='center')
plt.ylim([0, 0.5])
plt.xlim([0, 20])
plt.legend()
plt.show()

The graph produce from my code is:

upload_2018-12-29_14-29-55.png


It looks like that my plot is not same with the original one in terms N=0 of Pe^MIM (red line) and differences between two error probabilities (green line). The problem is the calculation for error probability under MIM attack was not given.

I hope that anyone may help me to solve this problem.

Thank you.
 

Attachments

  • upload_2018-12-29_12-2-16.png
    upload_2018-12-29_12-2-16.png
    2 KB · Views: 1,388
  • upload_2018-12-29_12-2-48.png
    upload_2018-12-29_12-2-48.png
    2.6 KB · Views: 1,186
  • upload_2018-12-29_12-3-56.png
    upload_2018-12-29_12-3-56.png
    5.7 KB · Views: 1,326
  • upload_2018-12-29_12-4-42.png
    upload_2018-12-29_12-4-42.png
    2.7 KB · Views: 1,293
  • upload_2018-12-29_12-7-29.png
    upload_2018-12-29_12-7-29.png
    6.3 KB · Views: 549
  • afNIG.png
    afNIG.png
    1.9 KB · Views: 533
  • upload_2018-12-29_14-29-55.png
    upload_2018-12-29_14-29-55.png
    6 KB · Views: 1,250
Last edited:
Technology news on Phys.org
  • #3
jedishrfu said:
Didn’t we discuss this program in this thread?

https://www.physicsforums.com/threads/convert-an-equation-to-python.962616/

Where you never responded to say whether it helped or not or have you fixed the expression I mentioned in it?
Thank you so much for your help. Apologize for not replying the threads. This post is different from the previous one.
 
  • #4
Ahh okay. I’ll look at it some more. Python and it’s plot capability is really awesome.
 
  • #5
Yes
jedishrfu said:
Ahh okay. I’ll look at it some more. Python and it’s plot capability is really awesome.
Yes. It is very interesting to play around with python. However, I felt some headache to understand the equation.
Thank you for your attention and help for the previous post.
Really appreciate it.
 
  • #6
A couple of questions:

- so the t**2 means to square the t value right? Okay I found this is correct.

- and the z2 = y + z adds the elements of y to the elements of z to get an array of z2 right?

I thought maybe you need to use numpy to do the element by element addition.

I didn’t see the equation that you used to code the z = line
 
  • #8
jedishrfu said:
A couple of questions:

- so the t**2 means to square the t value right? Okay I found this is correct.

- and the z2 = y + z adds the elements of y to the elements of z to get an array of z2 right?

I thought maybe you need to use numpy to do the element by element addition.

I didn’t see the equation that you used to code the z = line

1. Yes. t**2 is the square the t value
2. Yes. z2 = element of y + element of z
3. z line is (1-t**2)N, which is replacing tN in Pe^(aut,normal)
 
  • #9
jedishrfu said:
For numpy:

Python:
z2 = np.add(y,z)

I have tried it. However, it shows the same graph that I have been plotted before.
upload_2018-12-29_13-22-17.png
 

Attachments

  • upload_2018-12-29_13-22-17.png
    upload_2018-12-29_13-22-17.png
    6.3 KB · Views: 1,244
  • #10
While I don’t see exactly what’s wrong, I do see places where you could improve the readability of your code.

1) use the same colors for the curves to make easier to compare the expected graph to the one you generated

2) label your variables like pe_mim, pe_normal, pe_diff

3) I don’t see where you’re doing the pe_diff calculation and this may be the source of your problem,
 
  • #11
jedishrfu said:
For numpy:

Python:
z2 = np.add(y,z)

I have tried it. However, it shows the same graph that I have been plotted before.
View attachment 236522
jedishrfu said:
While I don’t see exactly what’s wrong, I do see places where you could improve the readability of your code.

1) use the same colors for the curves to make easier to compare the expected graph to the one you generated

2) label your variables like pe_mim, pe_normal, pe_diff

3) I don’t see where you’re doing the pe_diff calculation and this may be the source of your problem,

I already updated the code and the graph in the question.
 
  • #12
Your curve for ##P_e^{Auth,Normal}(t,N)## looks right. Therefore the problem is in your MIM case. You don't seem to have written out an explicit expression for ##P^{Auth,MIM}_e(t,N)##. Reverse engineering your code, you have implemented $$P_e^{Auth,MIM}(t,N)=
{{\left(I_0\left({{\left(1-t^2\right)N}\over{2}}\right)-
L_0\left({{\left(1-t^2\right)N}\over{2}}\right)
\right)e^{{{\left(1-t^2\right)N}/{2}}}-1}\over{e^{\left(1-t^
2\right)N}-1}}+{{\left(I_0\left({{tN}\over{2}}\right)-
L_0\left({{tN}\over{2}}\right)\right)e^{{{tN
}/{2}}}-1}\over{e^{tN}-1}}$$(Edit: replaced x with N above.) I presume this is incorrect.
 
Last edited:
  • Like
Likes jedishrfu
  • #13
Ibix said:
Your curve for ##P_e^{Auth,Normal}(t,N)## looks right. Therefore the problem is in your MIM case. You don't seem to have written out an explicit expression for ##P^{Auth,MIM}_e(t,N)##. Reverse engineering your code, it is $$P_e^{Auth,MIM}(t,N)=
{{\left(I_0\left({{\left(1-t^2\right)x}\over{2}}\right)-
L_0\left({{\left(1-t^2\right)x}\over{2}}\right)
\right)e^{{{\left(1-t^2\right)x}/{2}}}-1}\over{e^{\left(1-t^
2\right)x}-1}}-{{\left(I_0\left({{tx}\over{2}}\right)-
L_0\left({{tx}\over{2}}\right)\right)e^{{{tx
}/{2}}}-1}\over{e^{tx}-1}}$$I presume this is incorrect.

How to write the expression for ##P^{Auth,MIM}_e(t,N)##? Do you have any idea, what is the equation to plot ##P^{Auth,MIM}_e(t,N)##?
 
Last edited:
  • #14
I've no idea. I simply reverse engineered your code in the hope that you would see a typo or something.

You seem to have a source with at least some of the maths in it - what does it say? And you have the graph you want to replicate - how was it generated?
 
  • #15
Ibix said:
I simply reverse engineered your code in the hope that you would see a typo or something.
...speaking of which, it should be a plus between the fractions. Now corrected above.
 
  • #16
Ibix said:
...speaking of which, it should be a plus between the fractions. Now corrected above.
Yes. I realized it. It is stated that
upload_2018-12-29_16-57-23.png
. However, I don't think that they used another equation to calculate pe_mim.
 

Attachments

  • upload_2018-12-29_16-57-23.png
    upload_2018-12-29_16-57-23.png
    1.1 KB · Views: 778
  • #17
Then what is the expression given for the difference? Is it consistent with the expression I gave in #12?
 
  • #18
Actually, I didn't have any idea.
 
  • #19
Then where did you get what you implemented in the code?
 
  • #20
Ibix said:
Then where did you get what you implemented in the code?
I will try to ask about the equation from the author of the paper. Hopefully he will response my questions.
 
  • #21
A good idea. It seems to me that the problem is you don't know what to plot, not a problem with the actual plotting. You might get more help with that if you provide a link to the paper and start a thread asking for help understanding that, rather than focussing on the python. Your code seems to be right in the sense that it's doing what you ask - your problem is that you don't know what exactly you want it to do.
 
  • #22
Ibix said:
A good idea. It seems to me that the problem is you don't know what to plot, not a problem with the actual plotting. You might get more help with that if you provide a link to the paper and start a thread asking for help understanding that, rather than focussing on the python. Your code seems to be right in the sense that it's doing what you ask - your problem is that you don't know what exactly you want it to do.
That is the good idea. I will start a new thread and asking about the equations.
 

FAQ: How to plot integration equation using Python?

How do I import the necessary libraries for plotting integration equations in Python?

To plot integration equations in Python, you will need to import the "numpy" and "matplotlib.pyplot" libraries. These can be imported using the following code:

import numpy as np
import matplotlib.pyplot as plt

How do I define the equation to be integrated in Python?

To define the equation to be integrated, you can use the "lambda" function. For example, if you want to integrate the function f(x) = x^2, you can define it as follows:

f = lambda x: x**2

How do I use the "scipy.integrate" module to integrate equations in Python?

The "scipy.integrate" module provides various methods for numerical integration in Python. To use it, you can import it as follows:

from scipy import integrate

Then, you can use the "quad" function to integrate a single-variable function, or the "dblquad" function to integrate a double-variable function.

How do I plot the integration of an equation in Python?

To plot the integration of an equation, you can use the "plt.plot" function from the "matplotlib.pyplot" library. First, you will need to define the range of the x-values, and then use the "integrate.quad" function to calculate the y-values. Finally, you can use the "plt.plot" function to plot the x and y values.

Can I customize the plot of the integrated equation in Python?

Yes, you can customize the plot of the integrated equation in Python. You can use various functions from the "matplotlib.pyplot" library to add labels, titles, legends, and change the style and color of the plot. You can also adjust the range of the x and y axes using the "plt.xlim" and "plt.ylim" functions.

Similar threads

Replies
6
Views
2K
Replies
1
Views
1K
Replies
5
Views
3K
Replies
15
Views
2K
Replies
5
Views
2K
Replies
1
Views
731
Replies
4
Views
5K
Back
Top