PDA

View Full Version : matrix function help


brad sue
03-06-2006, 07:36 AM
Hi ,
I want to compute a geomtric series with matrices. I have the following functions:

add (matrix1,matix2)=function to add 2 matrices.
mult(matirx1,matrix2) =function to multiply two matrices.

I want to create this function in C that computes the following.



I+A+A^2+A^3+....A^k where I=indentity matrix

The sum should stop when all the entries in A^k are less than .00001.

I tried something like:

sum=I;
for(i=0;i<k;i++)
{
intermediary=mult(I,A);
sum+=intermediary;
intermediary=mult(intermediary,A)
}

In fact I need to use a recursive form but I just dont know how to do it.
I have been working on it since friday but I coud not figure out the solution.
I need some suggestions

Thank you for your help
B

jkd
03-06-2006, 07:51 AM
To incorporate your error term, you would need a max function, which takes a matrix, and gives the value of the maximum entry. Then when max(matrix) < 0.00001 (or whatever), return the sum.

As for the recursive case, the very basic structure of any simple recursive code is:

function MyFunction(bla) {
if (basecase) return someValue;
else return calculateSomething(bla) * MyFunction(bla2)
}


In your case, you just have to figure out what your base case is, and how you use the subsequent value to calculate the value of the first.

Melon00
03-06-2006, 03:48 PM
I dont think I fully understand your question. Are you computing the geometric series, given k? Or are you computing k given the series is less than .00001 ?