Marcos Stevenson
Marcos Stevenson

Reputation: 1

How can I find the maximum distance between two plots with different numbers of points in Python?

I have plotted two curves A and B (each with different number of data) extracted from a simulation with different numbers of elements and I want to extract the maximum relative error between the two (the maximum difference between the two at a same certain x-value that I don't know divided by the value at curve B).

How can I do on Python that given that they have different sizes, for example let's say curve A is plotted based on 20 data while B is based on 30 data?

I thought of choosing the one with the lowest number of data somehow using that, but I don't know if it's correct

Upvotes: 0

Views: 211

Answers (2)

Aldehyde
Aldehyde

Reputation: 113

From the documentation of numpy you can read about interp function. If you have two arrays A (short one) and B (long one) that are function of some common parameter t and you want to subtract them, first interpolate the first one to have same length as the long array.

Edit

Forgot to mention that the position for which distance is minimum is when the interpolated A-B is the lowest (in absolute value).

import numpy as np

xA = np.linspace(0, 2*np.pi, 20)
A = 0.5*xA

t  = np.linspace(0, 2*np.pi, 50)
B = np.cos(t)

A_interp = np.interp(t, xA, A)


plt.plot(xA, A, 'o',color='C1',label='A')
plt.plot(t, B, 'o',color='C0',label='B')
plt.plot(t, A_interp, '+',color='C5',label='A_interp')
plt.plot(t,A_interp - B, ':x',color='C2',label='A_interp - B')

plt.legend()

plt.show()

enter image description here

Upvotes: 1

MGP
MGP

Reputation: 133

Anything between your points is interpolated, or you have a function which describes your plot.

Evaluate both of those plots on all the points and then calculate error between them. If you have 20 and 30 different data points, you should just get two plots with 50 points.

At least that's my idea for this.

Upvotes: 1

Related Questions