开发者

f seems to be always positive in the MATLAB code, but in my Python code, f also gets negative values

How to convert the following MATLAB code to Python? Here is my solution, but it doesn't quite produce the same results. For example, f seems to be always positive in the MATLAB code, but in my Python code, f also gets negative values.

Any ideas how to fix开发者_Go百科 the program?

Mostly, I am concerned about these:

MATLAB:

    for k = 1 : nx  
            j = k+2;   

Python:

for k in range(1,nx+1):  
    j = k+2  

MATLAB:

    [V,D] = eig(A, B);  
    DD = diag(D);  
    keep_idxs = find( ~isinf(DD) );  
    D = diag( DD(keep_idxs) );  
    V = V(:, keep_idxs);  
    [lambda, idx] = min(diag(D));  
    f = V(:,idx);  

Python:

w,vr = scipy.linalg.decomp.eig(A,B)  
w = w.real  
vr = vr.real  
w = w[2:-1-2]  
lambda_ = w.min()  
idx = w.argmin()  
f = vr[:,idx]  

MATLAB:

    f = f(3:end-2);  
    [nf, nf_idx] = max(abs(f)); % L_infty norm  
    n2 = f(nf_idx); % normalize sign away, too  
    f = f ./ n2;  

Python:

f = f[2:-1-1]  
nf = max(np.absolute(f))  
nf_idx = np.absolute(f).argmax()  
nf_idx = np.ma.argmax(f)  
n2 = f[nf_idx]  
f = f/n2  

MATLAB:

    xx = -kappa:h:kappa;  

Python:

xx = np.arange(-kappa, kappa+h, h)  

Are those equivalent with each other? If they are, then why don't they produce exact the same results?


I don't know about matlab, but for python the code

for k in range(1,nx+1):  
  j = k+2

is the same as

j = nx+2

This isn't your main problem, but it's worrying.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜