N-Term Approximation for Matrices
이전 댓글 표시
Hello,
Utilizing a function I created to calculate the N-term approximation for a matrix:
function[eA]= N_Approx( N,A,I )
sum=0;
for i=1:N
ai=(A^i)/(factorial(i));
sum=sum+ai;
end
eA=I+sum;
I would like to solve this function over a range of values of N however when I set my range for N (ie N=1:10), the approximation function above only outputs a matrix for the last number of the range. Is there a way to store the matrix created for every iteration?
Any help would be appreciated.
Thank you! CAAJ
댓글 수: 1
채택된 답변
추가 답변 (2개)
John BG
2017년 1월 30일
1.
init sum to all zeros same size as resulting coefficients by replacing
sum=0;
with
sum=zeros([size(A),N]);
2.
in the loop, instead of accumulating in variable sum, stack the iterations indifferent layers of sum. If A is size 3x3, now sum is size 3x3xN
for i=1:N
ai=(A^i)/(factorial(i));
sum(:,:,i)=ai;
end
example
A=magic(3)
N=4
for i=1:N
ai=(A^i)/(factorial(i));
sum(:,:,i)=ai;
end
to read each coefficient use the following
sum(:,:,1)
=
8.00 1.00 6.00
3.00 5.00 7.00
4.00 9.00 2.00
sum(:,:,2)
ans =
45.50 33.50 33.50
33.50 45.50 33.50
33.50 33.50 45.50
sum(:,:,3)
=
199.50 171.50 191.50
179.50 187.50 195.50
183.50 203.50 175.50
.
CAAJ
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
댓글 수: 7
Walter Roberson
2017년 1월 30일
Please do not use sum as the name of a variable, as that interferes with using the key MATLAB function sum(). Chances are high that at some point you will have used sum as a variable and then expect to have sum() be the function and have it mysteriously crash. And if you do manage to avoid that then you are still going to leave other people who read it confused.
CAAJ
2017년 1월 30일
Guillaume
2017년 1월 30일
@John,
As an Answerer, try to teach good practices to the people you answer to. One of these is to NEVER use sum as a variable name, as they usually don't have the experience to see where it's going to cause problem. For evidence, see this non-exhaustive list:
- sum-function-not-working
- how-to-use-sum
- problem-with-sum-command
- subscript-indices-must-either-be-real-positive-integers-or-logicals
- index-exceeds-matrix-dimensions
- i-keep-getting-index-exceeds-matrix-dimensions-but-i-don-t-understand-why-i-m-trying-to-sum-the-ele
- why-the-error-is-coming
- why-the-code-gives-an-error
- error-when-sum-a-vector
- subscript-indices-must-either-be-real-positive-integers-or-logicals
CAAJ
2017년 1월 30일
John BG
2017년 1월 30일
Guillon,
It's CAAJ that chose that particular name of variable
Walter Roberson
2017년 1월 30일
John,
Here, we do not believe that our role is only to literally answer Questions; we believe that part of our role is to teach people how to write clear and efficient and bug-free MATLAB programs. Part of that involves pointing out to people where their programs are weak, and suggesting improvements. Your efforts here would be more effective if you were to do that as well.
CAAJ
2017년 1월 30일
0 개 추천
댓글 수: 2
John BG
2017년 1월 30일
편집: John Kelly
2017년 2월 3일
this is why I give you a layered 3D matrix as answer, you can then pull each term and do whatever you want, sum it or anything else you find convenient.
Walter Roberson
2017년 2월 3일
Jan's answer does give a 3D matrix with all of the results.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!