# Not able to understand matlab script

2 views (last 30 days)
Anshuman S on 25 Jul 2021
N12(2,j)=(N12(1,j)*pa12(1,j)+ dtime*(1/nmol(j)*(alpha1(j)*nL(j)+beta1(j)*na1t*(N12(1,j)-nR(j)))...
+1/nmol(j)*(beta1R(j-1)*na1t*nR(j-1)*(floor(nmolBC(j-1))*paR(j-1)+1)+beta2R(j-1)*nb1t*nR(j-1)*floor(nmolBC(j-1))*paR(j-1))...
+1/nmol(j)*(alpha1L(j+1)*nL(j+1)*(ceil(nmolBC(j))*paL(j+1)-1)+alpha2L(j+1)*nL(j+1)*ceil(nmolBC(j))*paL(j+1))...
+ceil(nmolBC(j-1))/nmol(j)*(alpha1L(j)+alpha2L(j))*(N12(1,j)*pa12(1,j)-nL(j)*paL(j))...
+floor(nmolBC(j))/nmol(j)*(beta1R(j)*na1t + beta2R(j)*nb1t)*(N12(1,j)*pa12(1,j)-nR(j)*paR(j))))...
/(1+dtime*(1/nmol(j)*alpha1(j)/pa12(1,j)+floor(nmolBC(j))/nmol(j)*(beta1R(j)*na1t+beta2R(j)*nb1t)...
+ceil(nmolBC(j-1))/nmol(j)*(alpha1L(j)+alpha2L(j))));
Thanks.
Steven Lord on 26 Jul 2021
I agree with Image Analyst's suggestion, though of course with more descriptive variable names than term1, term2, etc. Since people are going to need to try to read and understand this code in the future (potentially including you six months or maybe six days from now) if your code is at all complicated IMO you should err on the side of being descriptive. For example, that first term could be:
interactN1WithR = (m(1)./m(j))*b(j)*N(1)*(N(j)-nsupR(j));
That's still not a great name, but you know what your problem represents better than I do so you could choose an even better name. Though your LaTeX formatted equations seem a bit odd at first glance.
• Having In some places you refer to m as though it were a vector and in some places as though it were a matrix. That's fine in MATLAB, but if you're referring to just the first row or column of m when you use one index it might be clearer to make that explicit.
• Your use of both N and n could also be problematic; your finger slipping off the Shift key could change the results of your equations entirely.

Shadaab Siddiqie on 28 Jul 2021
From my understanding you want to improve the readability of the code. I agree with @Steven Lord to have more descriptive variable names. You can also leverage MATLAB editor tools to improve code readability. You could use the Apply smart indenting while typing option in the Preferences > MATLAB > Editor/Debugger > Language > Indenting section. You can refer Improve code readability for more information.
As for the existing code I would advise you to refactor the code with above mentioned improvements to make your code more readable.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by