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 件のコメント

Guillaume
Guillaume 2017 年 1 月 30 日
DO NOT use sum as a variable name. Soon enough, you will try to use the sum function in the same code and end up wondering why it comes up with some weird error. That will be because matlab is trying to index into your sum matrix instead of calling the sum function.

サインインしてコメントする。

 採用された回答

Jan
Jan 2017 年 1 月 30 日
編集済み: Jan 2017 年 1 月 30 日

4 投票

The power operation is expensive and you can avoid the repeated application:
function Result = N_Approx(N, A, I)
Result = zeros([size(A), N]);
Ai = eye(size(A));
SumAi = I; % If I should be added to each matrix
facti = 1;
for i = 1:N
facti = facti * i; % cumulative product instead of factorial
Ai = Ai * A; % cumulative product instead of power
SumAi = SumAi + Ai / facti;
Result(:, :, i) = SumAi;
end
Even if runtime does not matter in your case, it is a good programming practice to reduce the number of power operations, when possible.
If the variable "I" should not be added to each matrix, use: "SumAi = 0" instead of "SumAi = I" and add I to the final Result only.
You can access the intermediate matrices and the last value by:
Result(:, :, k)
with k goes from 1 to N.
As Walter has said already: There are many of questions in the forum caused by using "sum" as name of a variable, when later in the code it is tried to use as function. Therefore it is recommended to avoid this.

3 件のコメント

CAAJ
CAAJ 2017 年 1 月 30 日
I'd like to have a range from N=1:10, and see how using 10 terms changes the matrix verses using 9 terms, and so on. I don't really understand how else to output that besides setting the range of N=10 before calling the function. And even when doing that, the function outputs only one matrix rather than the 10 different matrices.
Jan
Jan 2017 年 1 月 30 日
The shown code replies all 10 matrices, but stored in a 3D-array: The first matrix is: Result(:, :, 1) and so on. Now you can inspect the differences between using 9 or 10 terms by comparing Result(:, :, 9) and Result(:, :, 10).
CAAJ
CAAJ 2017 年 1 月 30 日
Thank you very much Jan!

サインインしてコメントする。

その他の回答 (2 件)

John BG
John BG 2017 年 1 月 30 日

1 投票

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
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.
Jan
Jan 2017 年 1 月 30 日
編集済み: Jan 2017 年 1 月 30 日
@John BG: You forgot the cumulative summation.
CAAJ
CAAJ 2017 年 1 月 30 日
For your example code above, is it saying that for of the range of N=1:4, that the code outputs the sum of all matrices up to the Nth term inputted, or does it output the matrix at the Nth term inputted.
Guillaume
Guillaume 2017 年 1 月 30 日
CAAJ
CAAJ 2017 年 1 月 30 日
Thank you Guillaume, I have changed that in my code and seem to still be having the same error.
John BG
John BG 2017 年 1 月 30 日
Guillon,
It's CAAJ that chose that particular name of variable
Walter Roberson
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
CAAJ 2017 年 1 月 30 日

0 投票

Just to clarify a bit, with inputting a range for my function beforehand, I would like to see how using 10 terms changes the matrix verses using 9 terms, and etc. I don't really understand why, but when I do set my range before hand, the function outputs only one matrix rather than the 10 different matrices created for the range of N values.

2 件のコメント

John BG
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
Walter Roberson 2017 年 2 月 3 日
Jan's answer does give a 3D matrix with all of the results.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2017 年 1 月 30 日

編集済み:

2017 年 2 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by