0

i need to create a higher resolution of my plot with the linspace function but i can't figure out how to implement it into my code. Maybe someone has a better understanding of this and can help me.

import numpy as np
import matplotlib.pyplot as plt
N = np.array([1, 2, 3, 4])
c = np.array([1359,2136.6,2617.74,2630.16]) 
ct = c/1000                                            
ct0 = 103.8348/1000
cmax = 2630.16/1000
n = N.size
A = np.zeros(n)                                         
k = np.zeros(n)                                        
for j in range(0,n):
    A[j] = (ct[j] - ct0)/(cmax - ct0)
for j in range(0, n):
    if j < 3:
        k[j] = -(np.log(1 - A[j])) / N[j]
    else:
        k[j] = 1
MWk = np.mean(k)
Amod = np.zeros(n)
for j in range(0,n):
    Amod[j] = 1 - np.exp((-N[j]) * MWk)    
    
     
print(ct)
print(A)
print(k)
plt.xlabel("N")
plt.ylabel("Aufschlussgrad ")
plt.plot(N, A, "g", label = "Aufschlussgrad")
plt.plot(N, Amod, "k", label = "Modelfunktion")
plt.title("Hochdruckhomogenisator")
plt.legend()
plt.show()
Cyanor
  • 3
  • 3
  • `A` and `Amod` are the same, so the two lines are exactly on top of each other. So you only see the line that was plotted last. – cazman Jan 07 '22 at 19:23
  • @mkrieger1 thanks! i updated my post and changed the Code. Maybe now you can help? – Cyanor Jan 07 '22 at 19:33
  • @Cyanor Is `c` meant to have the same length as `N`? If so, then it is easy to extend `N` with linspace, but it is unclear how we should extend `c` to have a matching size. If not, then you should have `n=c.size` instead of `n=N.size` – Ben Grossmann Jan 07 '22 at 19:39
  • @BenGrossmann yes. `c` and `N` are supposed to have the same length. i actually left 2 measured values out, because it would display an error otherwise. If i would do it correctly, the arrays would look something like `N = np.array([0, 1, 2, 3, 4,5])` and `c = np.array([103.8348,1359,2136.6,2617.74,2630.16,2617.74])` – Cyanor Jan 07 '22 at 19:46
  • @Cyanor "Something like" is vague. Unless you explain the rule for how you are extending the list `c`, we won't be able to increase the resolution of your plot. – Ben Grossmann Jan 07 '22 at 19:51
  • @BenGrossmann you're right. So the arrays are supposed to be like in my previous comment. those are my measurments. but because i can't divide by zero (my last `c` value appears twice in that array) it won't work. so i thought i could leave the last and the first value out. the first is similar to `ct0`. what do you suggest? – Cyanor Jan 07 '22 at 19:57
  • I don't understand, where do you believe that you might divide by zero. It the array is supposed to be as it is in your previous comment, then we can only extend the graph to 5 data points, but this does not seem like the kind of "increase in resolution" that you are looking for. – Ben Grossmann Jan 07 '22 at 20:00
  • Are you hoping to [interpolate](https://en.wikipedia.org/wiki/Interpolation) the data that you were given for c? – Ben Grossmann Jan 07 '22 at 20:01
  • i tried using the Comment-array again and it's true, but then my second graph won't show. i was told i can increase the resolution by creating multiple random data-points that are in between the values of the arrays `c` and `N` using the linspace function. i thought that would make the Plots a bit cleaner. is that even possible or the right approach? – Cyanor Jan 07 '22 at 20:06
  • yes!! that is what i am hoping to do. just didn't know the word. thanks for teaching – Cyanor Jan 07 '22 at 20:08

1 Answers1

0

There is no need to interpolate Amod, since it is a function you defined. On the other hand, it is necessary to perform an interpolation (either on A or on the original data c) in order to add more points to the graph. With only 4 or 5 points, the interpolation will not be very meaningful. In this case I choose to interpolate A.

The code was not taking profit of numpy's arrays, so I pythonized it a little (it looked like C)

import numpy as np
import matplotlib.pyplot as plt

def Amod(x, MWk):
    return 1 - np.exp((-x) * MWk) 

def k(x, A):
    rv = -np.log(1 - A) / x
    rv[np.nonzero(A==1)] = 1
    return rv

# No real changes here: only a little 'pythonization'
N = np.array([1, 2, 3, 4])
c = np.array([1359,2136.6,2617.74,2630.16]) 
ct = c/1000                                            
ct0 = 103.8348/1000
cmax = 2630.16/1000
n = N.size
A = (ct - ct0) / (cmax-ct0)
MWk = np.mean(k(N, A))

print(ct)
print(A)
print(k)

# we now interpolate A
# numpy's interpolation is linear... it is not useful for this case
from scipy.interpolate import interp1d
# interp1d returns a function that interpolates the data we provide
# in this case, i choose quadratic interpolation
A_interp_fun = interp1d(N, A, 'quadratic')

# Let's increase the number of points
new_x = np.linspace(N[0], N[-1])

A_xtra = A_interp_fun(new_x)

plt.xlabel("N")
plt.ylabel("Aufschlussgrad ")
plt.scatter(N, A, label = "Aufschlussgrad - data")
plt.plot(new_x, A_xtra, "g", label="Aufschlussgrad - interpolation")
plt.scatter(N, Amod(N, MWk), label="Modelfunktion - data")
plt.plot(new_x, Amod(new_x, MWk), "k", label="Modelfunktion - function")
plt.title("Hochdruckhomogenisator")
plt.legend()
plt.show()
azelcer
  • 1,383
  • 1
  • 3
  • 7