Not able to understand matlab script
Show older comments
Can someone please help me in understanding this matlab sript in terms of mathematical equation.
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.
10 Comments
Jan
on 25 Jul 2021
No, this is impossible. It is not clear, if pa12, dtime, nmol, alpha, ... are variables of functions. This massive block of code is unreadable also. Without any useful comments and a clear structure, this pice of code is an almost black box. If there are no explanations in the code, the best way to handle it is to write a new vorsion by our own. Sorry.
dpb
on 25 Jul 2021
As Jan say, certainly not in isolation...and if the rest is so badly formatted/structured as this, good luck.
Approaching the author/source from whence it came is probably the best bet or either just hope it does what it's supposed to do and treat it as a black box or either find another source for the calculations it's doing or write a replacement.
Or, choose a different thesis topic! <VBG>
Anshuman S
on 25 Jul 2021
dpb
on 25 Jul 2021
Well, you can do the comparison of indicies term by term as well as we; it still would take comparing the code variables to the expression above to match them up precisely...
I don't see what there is that isn't far easier for you than us, you having all the context of the problem and the rest of the code...
Anshuman S
on 25 Jul 2021
Image Analyst
on 25 Jul 2021
I'd break it into smaller terms and then combine, like
term1 = ......
term2 = ..........
term3 = ............
dndt = term1 - term2 +.......................
If you still can't understand it, ask the person who wrote that bad piece of code.
Anshuman S
on 25 Jul 2021
dpb
on 25 Jul 2021
"This expression was kind of bottleneceking my solution, ..."
Rearranging, while undoubtedly would make the code much easier to read/debug/maintain won't solve a performance issue.
As this code is written, it looks like it is being called in a loop over j, and may well be from the looks of it, the functional for an ode function in which case it's being called a zillion times. What may be slowing it down might be if there are any constant terms inside the loop over j that could be calculated and moved out of the loop rather than recomputed every pass, etc., etc., etc., ...
Also a potential real performance killer could be whether or not the array N12() is preallocated before the loop or not.
There's just no way to know anything with just a single line of code in isolation...but the MATLAB editor will give you a lot of coding hints via its builtin mlint functionality -- all those orange marks on the RHS are indications of potential issues -- and I'll bet this code has a bunch of 'em.
Anshuman S
on 26 Jul 2021
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.
Answers (1)
Shadaab Siddiqie
on 28 Jul 2021
0 votes
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.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!