recurrence relation for any given 'n'.
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
How to compute A_j^(2n) for any 'n' using A_j^(2).
Here, n=2,3,4,...; and j=1:n-1.

Any kind of help is highly appreciated.
Thank you.
채택된 답변
Walter Roberson
2017년 6월 21일
2 개 추천
If you have the Symbolic toolbox, you could use evalin(symengine) or feval(symengine) to call into MuPAD in order to take advantage of solve() of a rec() recurrence structure
댓글 수: 11
Walter Roberson
2017년 6월 27일
편집: Walter Roberson
2017년 6월 27일
Guessing that there is a vector of initial constants, A(J)^1, J = 1..n-1, and calling that vector A_1:
function result = Aj_2(j, A_1)
result = A_1(1:j) * A_1(j:-1:1).';
end
function result = Aj_2n(j, two_n, A_1)
if two_n == 2
result = Aj_2(j, A_1);
else
result = 0;
for mu = 1 : j
result = result + Aj_2n(mu, two_n - 2, A_1) .* Aj_2(j - mu + 1, A_1);
end
end
end
You can invoke this by creating a specific vector of values:
function result = A_driver
N = 7; %for example
A_1 = randi(5, 1, N); %for example
result = zeros(1, N-1);
for j = 1 : N-1
result(j) = Aj_2n(j, 2*N, A_1);
end
end
With large enough values of N, the cost of recursion might add up significantly (though you would probably risk exceeding 2^53, the point at which you stop being able to distinguish adjacent integers.) I was going to show a way in R2017a and later to improve that speed, but the code above is fast enough for the values I tried that it is not worth going to the trouble.
Dear Roberson,
Thank you for your prompt response. However, I got large values from your computation.
The multiplication in the first function, 'result' should be a vector multiplication not the matrix multiplication. I corrected that, then I received the following values, for N=5 and with A_1 having the actual values.
641584391529617.250000
12978244794078948.000000
147984150516881280.000000
1267037968968080400.000000
However, what I am supposed to get are the following values. These I got from my computation. I will cross check these too.
915.062500
3702.055500
12257.898481
41869.900040
Here are the A_j^(2) [it is the variable 'result' in your code] values I have used:
30.250000
61.191000
140.720000
407.410000
1419.200000
Thank you so much.
"The multiplication in the first function, 'result' should be a vector multiplication not the matrix multiplication."
No, that is not correct. Notice that I used
A_1(1:j) * A_1(j:-1:1).'
with the second item transposed to create a column vector. That makes it a row vector matrix-multiplied by a column vector, which will have the effect of multiplying the corresponding locations and then summing the result. The code is equivalent to
sum( A_1(1:j) .* A_1(j:-1:1) )
which could also be written
dot( A_1(1:j), A_1(j:-1:1) )
Of these, the form that I used is the fastest, with the explicit sum of .* the second fastest, and dot() about 1/3 slower than the form I used.
Walter Roberson
2017년 6월 27일
I found that with larger N the recursion was a big problem. Here are versions that have considerably improved performance for larger N:
Venkata
2017년 6월 28일
Dear Roberson,
Yes, you are right. That was not a matrix multiplication. I made it a column vector by using A_1(:) and that's why got a matrix. My bad. I am sorry.
Nevertheless, I get, 'memoize' is an undefined function or variable error. I am using Matlab-2016a version, but I saw this feature was explained by Loren in 2006 in Mathworks page. So, should I activate some feature to use this facility in my version?
Thank you.
Walter Roberson
2017년 6월 28일
memoize() was new in R2017a. You can acheive the same effect using tools such as the one mentioned at http://blogs.mathworks.com/pick/2015/03/27/cache-your-nuts-to-eat-later/, but that does say something about writing to disk.
With your N being around 5, the memoization / caching is not important and the original would be fine. I just happened to randomly toss in 17 for N and the result was so slow that I was inspired to try out the R2017a memoization feature. If you are only ever going to be using small values for N then it is not worth the time to create a version with caching for use with earlier MATLAB releases.
Venkata
2017년 7월 1일
Dear Roberson,
Thanks a ton. That was perfect.
Thank you so much.
For the testing purpose I chose N=5 else there is no limit to it. I want up to N=50 at least.
Now I know how to do it.
I can't thank you less.
Thank you.
memoization / caching would make a huge difference for N = 50.
If you do not use caching, or do not use a large enough cache, you would probably be better off building upwards, starting from the smaller values and calculating up to the larger values using the already-calculated values.
Venkata
2017년 7월 6일
Dear Roberson,
Is there any work around for memoization in 2016a?
Seems that I need to run for N=1500. I ran the code for N=500 yesterday evening and even after 12 hrs it's still running.
Please let me know how to achieve memoization in 2016a.
Thanks a lot.
I tossed together the attached. I did not test it with functions that return multiple outputs.
Aj_2n_better and Aj_2 are the same as last time. memoizefun is new (I just wrote it) and A_driver_cache is almost the same as the previous A_driver_better except for calling memoizefun instead of R2017a's memoize()
Note: you should test that the results are the same as the previous... they should be, but I did not test that.
Venkata
2017년 7월 7일
Dear Roberson,
Sure, I'll check them.
I owe you big time, Mr. Roberson.
Thanks a ton.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Code Performance에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
