Building a function to display of the sum of matrices at different powers in Matlab

103 Views Asked by At

I'm trying to write a for loop for the sum A+A^2+A^3+..+A^n.

Here is my code:


function [ x ] = Untitled2( A , n )

for k=1:n,

x =sum(A^k)

end


The problem I'm having with this is this function is listing the matrix A to each power without adding them. What can I do to add these together? Thank you in advance

1

There are 1 best solutions below

10
On BEST ANSWER

Matlab's sum returns the sum along the first dimension not equal to one. If you want to add to x you would be best to use:


function[x]=Untitled(A,n)

x=zeros(size(A));

for k=1:n

x = x + A^k

end