MATLAB: In an assignment A(I) = B, the number of elements in B and I must be the same.

crank-nicolsonfor loop

How do you state the first iteration to be used in a for loop? I am having trouble with the following error message:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Hello,
I am using the Crank-Nicholson method to solve for a linear pde, and I have a simple initial condition. I am at whits end to try to fix this problem that seems so simple. Please help me.
My problem is trying to apply the initial conditions as the first iteration for a for loop.
What I am trying to do in "real-speak":
Let Bb be the initial condition.
Let AA be the matrix used for the crank-nicolson scheme.This never changes.
w0=AA\Bb; This is the concentration profile at t=0.
Take this value w0, and let this be the new "b" vector called BB. at t=1, we have a vector BB and matrix AA
w=AA/BB
take this w, and it should be the new BB for t=2.
Continue for n iterations, what you should have is the function w changing over time.
A portion of my code *note C values are constants:
Bb=zeros(m,1); %this forms the initial b matix
Bb(1,1)=(G0); %this defines the initial conditions
w0=AA\Bb; %this solves the initial matrix, and we would get u at %t=0
BB=zeros(m,1)
%this is problem
BB(1)=Bb; %for the first iteration of BB, let it be initial condition vector Bb

w(1)=w0; %for the first iteration of w, let it be initial condition vector w0
for j=1:n,
for k=2:m,
BB(k)=(-1*C1*w(k))+(C4*w(k))-(C3*w(k))
RHS=BB;
end
w=AA\(RHS); %the B vector should lead to the new u vector
%the solution of the u vector from the previous step must used as the b solution for the future step
%assuming that the A matrix is correct
%repeat
end
**
This is the error msg:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in crank5_linda (line 49)
BB(1)=Bb; %for the first iteration of BB, let it be initial condition vector Bb
Thank you in advance.
Cathleen

Best Answer

You define Bb as a vector, and then you try to store that entire vector in to BB(1) which is a single element of an array.
I do not know what the correct code should be for your situation, but probably one of
BB(1) = Bb(1);
or
BB = Bb;