How to assemble coefficient matrix?

2 views (last 30 days)
I am trying to assemble a N x N matrix for IC as shown above. I have vectors for ao(theta), c(theta), and thetal. I am trying to figure out how to assemble the IC matrix from these vectors using the equation above. From there I need to solve for the A vector values.
  1 Comment
Christopher
Christopher on 12 Jun 2013
I believe the repmat command can be used to assemble this matrix, but I cannot figure out how I would write this out to generate the n x n IC matrix.
Thanks

Sign in to comment.

Accepted Answer

Roger Stafford
Roger Stafford on 12 Jun 2013
I'll assume you have a0, c, and theta as column vectors of corresponding elements and of length N.
IC = bsxfun(@plus,4*b./(a0.*c),(1./sin(theta))*(1:N)).*sin(theta*(1:N));
Of course it is much easier to write nested for loops:
IC = zeros(N);
for l = 1:N
for m = 1:N
IC(l,m) = (4*b/a(l)/c(l)+m/sin(theta(l)))*sin(m*theta(l));
end
end
Note: You ought to use something other than lowercase l for your index. It is too easily confused with the numeral 1.
  2 Comments
Christopher
Christopher on 12 Jun 2013
Thank you Roger
One question though, what purpose does the IC=zeros(N) line serve?
Thanks again
Roger Stafford
Roger Stafford on 12 Jun 2013
Doing the initial 'zeros' preallocates memory for the IC array all at once rather than increasing it piecemeal in the for-loops. For large arrays the latter can slow down the execution of code greatly. This does not apply to vectorized code such as in the first method above using 'bsxfun' since these matlab functions do their own preallocation.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!