0

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

Heath Huffman
  • 589
  • 3
  • 10
  • 18

1 Answers1

2

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

Jeremy Upsal
  • 1,443