Not able to understand matlab script
이전 댓글 표시
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
Jan
2021년 7월 25일
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
2021년 7월 25일
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
2021년 7월 25일
dpb
2021년 7월 25일
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
2021년 7월 25일
Image Analyst
2021년 7월 25일
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
2021년 7월 25일
dpb
2021년 7월 25일
"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
2021년 7월 26일
Steven Lord
2021년 7월 26일
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.
답변 (1개)
Shadaab Siddiqie
2021년 7월 28일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
