Search code examples
pythonexcelpolynomialspolynomial-mathpolynomial-approximations

How to create the polynom for function of two variables using python?


I have two variables - T1 and T2 - and their values. For example:

T1=[300,400,500,600,650,700]
T2=[300,400,500,600,700,800]

And i know the values of Z variable for each T1,T2 from example above (i obtained Z values from experiment). So i have a 6x6 matrix of Z values (i know Z for T1=300 T2= 300, Z for T1=300,T2=400 values and etc.). So we can say that Z is the function of two variables - T1 and T2. - Z=F(T1,T2)

I can get a polynom from EXCEL for Z=F(T1,T2) if one of the variables (T1 or T2) is fixed. For example:

Z(T1=300,T2)=a + b*T2+c*T2^2+d*T2^3+e*T2^4 Z(T1,T2=300)=f + g*T1+h*T1^2

Can you show me the correct code in python, which i can use to receive a polinom for that function of two variables. I think, that it should calculate the coefficients of polynom. For example:

If resulting polynom has the next view: P(T1,T2)= a + b*T2+c*T2^2+d*T2^3+e*T2^4 + g*T1+h*T1^2, the python script should calculate the value of coefficients a,b,c,d,e,g,h.


Solution

  • Basically you are looking for a way to use polyfit, but for 2-d array. You should use least-squares optimization (np.linalg.sltsq). The first answer here is exactly what you are looking for.