JoshuaF
JoshuaF

Reputation: 1215

Same equation gives different values in Matlab and Numpy?

I'm trying to convert a function from Matlab to Python. The Matlab function is:

function [f,df_dr1,df_dr2,g,dg_dr1,dg_dr2] = f_eval_2eq(r1,r2,r3,z1,z2,z3,n1,n2,n3)

f      = (r1)./sqrt(r1.^2 + z1.^2)...
- (n2/n1)*(r2-r1)./sqrt((r2-r1).^2 + z2.^2);

df_dr1 = 1./sqrt(r1.^2 + z1.^2)...
- r1.^2./(r1.^2 + z1.^2).^(3/2)...
+ (n2/n1)./sqrt(z2.^2 + (r1-r2).^2)...
- (n2/n1).*(r1-r2).*(2*r1-2*r2)./(2*((r1-r2).^2 + z2.^2).^(3/2));

df_dr2 = (n2/n1).*(r1-r2).*(2*r1-2*r2)./(2*((r1-r2).^2 + z2.^2).^(3/2))...
- (n2/n1)./sqrt(z2.^2 + (r1-r2).^2);


g      = (r2-r1)./sqrt((r2-r1).^2 + z2.^2)...
- (n3/n2)*(r3-r2)./sqrt((r3-r2).^2 + z3.^2);

dg_dr1 = (r1-r2).*(2*r1-2*r2)./(2*((r1-r2).^2 + z2.^2).^(3/2))...
- 1./sqrt(z2.^2 + (r1-r2).^2);

dg_dr2 = 1./sqrt((r1-r2).^2 + z2.^2)...
+ (n3/n2)./sqrt(z3.^2 + (r2-r3).^2)...
- (r1-r2).*(2*r1-2*r2)./(2*((r1-r2).^2 + z2.^2).^(3/2))...
- (n3/n2).*(r2-r3).*(2*r2-2*r3)./(2*((r2-r3).^2 + z3.^2).^(3/2));

end

%test code
K>> a=[1,2,3];b=a+1;c=b+1;d=a;e=b;f=c;g=1;h=2;i=3;
K>> [f,df_dr1,df_dr2,g,dg_dr1,dg_dr2] = f_eval_2eq(a,b,c,d,e,f,g,h,i)

The Python function I wrote is:

def f_eval_2eq(r1,r2,r3,z1,z2,z3,n1,n2,n3):
    #evaluate gradients
    #n_ are scalars
    f = (r1)/np.sqrt(r1**2 + z1**2) \
        - (n2/n1)*(r2-r1)/np.sqrt((r2-r1)**2 + z2**2);

    df_dr1 = 1/np.sqrt(r1**2 + z1**2) \
             - r1**2/((r1**2 + z1**2)**(3/2)) \
             + (n2/n1)/np.sqrt(z2**2 + (r1-r2)**2) \
             - (n2/n1)*(r1-r2)*(2*r1-2*r2)/(2*((r1-r2)**2 + z2**2)**(3/2));

    df_dr2 = (n2/n1)*(r1-r2)*(2*r1-2*r2)/(2*((r1-r2)**2 + z2**2)**(3/2)) \
             - (n2/n1)/np.sqrt(z2**2 + (r1-r2)**2);


    g      = (r2-r1)/np.sqrt((r2-r1)**2 + z2**2) \
             - (n3/n2)*(r3-r2)/np.sqrt((r3-r2)**2 + z3**2);

    dg_dr1 = (r1-r2)*(2*r1-2*r2)/(2*((r1-r2)**2 + z2**2)**(3/2)) \
             - 1/np.sqrt(z2**2 + (r1-r2)**2);

    dg_dr2 = 1/np.sqrt((r1-r2)**2 + z2**2) \
             + (n3/n2)/np.sqrt(z3**2 + (r2-r3)**2) \
             - (r1-r2)*(2*r1-2*r2)/(2*((r1-r2)**2 + z2**2)**(3/2)) \
             - (n3/n2)*(r2-r3)*(2*r2-2*r3)/(2*((r2-r3)**2 + z3**2)**(3/2));

    return (f,df_dr1,df_dr2,g,dg_dr1,dg_dr2)

#test code
A=np.array([1,2,3])
B=A+1
C=B+1
D=A
E=B
F=C
G=1
H=2
I=3
[f,df_dr1,df_dr2,g,dg_dr1,dg_dr2] =f_eval_2eq(A,B,C,D,E,F,G,H,I)
print ('f= '+str(f) +'\n'+'df_dr1= '+str(df_dr1) +'\n' +'df_dr2='+str(df_dr2) +'\n'+'g= '+str(g) +'\n'+'dg_dr1= '+str(dg_dr1) +'\n'+'dg_dr2= '+str(dg_dr2) +'\n')

The output for f is the same in both, but all the other values are different and I cant figure out why???

Any help is appreciated.

Upvotes: 0

Views: 182

Answers (2)

Suever
Suever

Reputation: 65470

In Python 2.x, if you divide two integers (such as 2 and 3) the result is cast as an integer as well:

x = 3/2
#   1

type(x)
#   <type 'int'>

You need to explicitly specify either the numerator or denominator to be a float rather than an integer using a decimal point and this will allow the output to be a float as well.

y = 3./2
#   1.5

type(y)
#   <type 'float'>

Alternately, as suggested by @rayryeng, you can place the following at the top of your code to get the behavior you expect.

from __future__ import division

Upvotes: 2

tst
tst

Reputation: 46

You can also add

from __future__ import division

to the top of your file, if you're using Python 2, in order to get the Python 3 behavior, i.e. always using float division.

Upvotes: 2

Related Questions