Note that there are some explanatory texts on larger screens.

plurals
  1. POf seems to be always positive in the MATLAB code, but in my Python code, f also gets negative values
    text
    copied!<p>How to convert the <a href="http://pastebin.com/fZe0AZQn" rel="nofollow">following MATLAB</a> code to Python? <a href="http://pastebin.com/gFcr8HGR" rel="nofollow">Here is my solution</a>, but it doesn't quite produce the same results. For example, <code>f</code> seems to be always positive in the MATLAB code, but in my Python code, <code>f</code> also gets negative values.</p> <p>Any ideas how to fix the program?</p> <p>Mostly, I am concerned about these:<br> MATLAB: </p> <pre><code> for k = 1 : nx j = k+2; </code></pre> <p>Python: </p> <pre><code>for k in range(1,nx+1): j = k+2 </code></pre> <p>MATLAB: </p> <pre><code> [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); </code></pre> <p>Python: </p> <pre><code>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] </code></pre> <p>MATLAB: </p> <pre><code> 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; </code></pre> <p>Python: </p> <pre><code>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 </code></pre> <p>MATLAB: </p> <pre><code> xx = -kappa:h:kappa; </code></pre> <p>Python: </p> <pre><code>xx = np.arange(-kappa, kappa+h, h) </code></pre> <p>Are those equivalent with each other? If they are, then why don't they produce exact the same results?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload