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

조회 수: 2 (최근 30일)
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

채택된 답변

Walter Roberson
Walter Roberson 2012년 3월 8일
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;

추가 답변 (1개)

Charanraj
Charanraj 2013년 12월 23일
I too get the same error.I have written a simple Matlab s-function program to calculate nearest coordinates in a parallelogram, . the code is as follows:
function sys = mdlOutputs(t,x,u)
g=u(1);
h=u(2);
global m;
global n;
global m1;
global m2;
global n1;
global n2;
m=0;
if (g<m)
m1=m-1;
m2=m;
else
m=m+1;
end
n=0;
if (h<n)
n1=n-1;
n2=n;
else
n=n+1;
end
M1=m1;
M2=m2;
N1=n1;
N2=n2;
sal(1)=M1;
sal(2)=M2;
sal(3)=N1;
sal(4)=N2;
sys=sal;
% End of mdlOutputs.

카테고리

Help CenterFile Exchange에서 Eigenvalue Problems에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by