Solve "Index exceeds the number of array elements (1)", "Vectors must be the same length." errors

I'd like to solve those problems from under coding using A, B, and C relationships. If equation error can't be fixed, "B(i+1)=B(i)+((h.*q)/(k*T*E))*C(i)" can be used.
a = 0;
b = 1;
n = 110;
k = 8.61733*10^(-5);
T = 300 ;
q = 1.60218*10^(-19);
E = 8.8542*10^(-12)
x= linspace(a,b,n);
h= x(2)-x(1);
A = zeros(size(x));
B = zeros(size(x));
for i = 0:n-1
A(0)=3;
B(0)=1;
C(i)=2*x(i)+3;
A(i+1)=A(i)-h.*B(i)-(0.5.*h.^2*q.*C(i))./(k*T*E);
B(i+1)=B(i)+((h.*q)/(k*T*E))*0.5*(C(i)+C(i+1));%equation error
end
plot(transpose(x),A,'.')

1 Kommentar

Hi. Can you please try starting the index from 1 instead of 0 and go upto n in the for loop?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

a = 0;
b = 1;
n = 110;
k = 8.61733*10^(-5);
T = 300 ;
q = 1.60218*10^(-19);
E = 8.8542*10^(-12)
x= linspace(a,b,n);
h= x(2)-x(1);
A = zeros(size(x));
B = zeros(size(x));
A(0+1)=3;
B(0+1)=1;
for i = 0:n-1
C(i+1)=2*x(i+1)+3;
A(i+1+1)=A(i+1)-h.*B(i+1)-(0.5.*h.^2*q.*C(i+1))./(k*T*E);
B(i+1+1)=B(i+1)+((h.*q)/(k*T*E))*0.5*(C(i+1)+C(i+1+1));
end
plot(transpose(x),A,'.')

12 Kommentare

I still have the same error message "Index exceeds the number of array elements (1).."
C(i)=2*x(i)+3;
In that line you are writing into the "current" C -- you do not start out with a C, and you use and and you write to and and
B(i+1+1)=B(i+1)+((h.*q)/(k*T*E))*0.5*(C(i+1)+C(i+1+1));
In that line you use the "current" C that you just wrote to ( on the first iteration), but you also use the "next" C, on the first pass, which has not been created yet.
In order to be able to use two generations of C values in that line, you will need to initialize the first C before the loop and write to the new C.
a = 0;
b = 1;
n = 110;
k = 8.61733*10^(-5);
T = 300 ;
q = 1.60218*10^(-19);
E = 8.8542*10^(-12)
x= linspace(a,b,n);
h= x(2)-x(1);
A = zeros(size(x));
B = zeros(size(x));
C = zeros(size(x));
A(0+1)=3;
B(0+1)=1;
C(0+1) = something
for i = 0:n-1
C(i+1+1)=2*x(i+1)+3;
A(i+1+1)=A(i+1)-h.*B(i+1)-(0.5.*h.^2*q.*C(i+1))./(k*T*E);
B(i+1+1)=B(i+1)+((h.*q)/(k*T*E))*0.5*(C(i+1)+C(i+1+1));
end
plot(transpose(x),A,'.')
Thanks!
Now I got a message that "Vectors must be the same length." to fix the plot. Do you have any idea about this plot? I tried to change simple plot as
plot(x,A,'.')
but I got the same wrong message..
Your looping structure is creating up to A(n-1+1+1) = A(n+1) but your x is length n. That is, your loop is creating n new elements beyond but your x is only expecting n elements total including
i = 0:n-2
it could make a graph but they don't show any change by C value as I changed the equation form..
Could you give an example of a changed equation form that is not resulting in a change to C ?
Like
C(i+1)=2*x(i+1)+1; C(i+1)=x(i+1)+3;
etc..
What are you initializing C(1) to ? (0.5.*h.^2*q)./(k*T*E) is about 3E-11 so unless C is pretty big, the value being subtracted in forming A(i+1+1) is not going to make much difference to A.
Furthermore, in comparing the two possibilities 2*x(i+1)+1 compared to x(i+1)+3, your first x is 0, and 2*0 is the same as 1*0, so your second C is going to be 1 in the first case or 3 in the second case, no matter what you initialized C(1) to, and as noted above, you need a C on the order of 1E10 to make much difference to A.
Actually I've tried to find out C equation by putting new formula: cos(x), constant not zero, cosh(x), x^2 etc..
And I realized E is 11.9.
So E is 11.9 instead of 8.8542*10^(-12)?
Do I understand correctly that you do not know the formula for C and are trying to figure out what it is? If that is the case then what final result do you need for A in order to know that you got the right C formula?
Yes. And no. I need to figure out that C equation by comparing theoretical equation as I didn't put this code.
Which theoretical equation are you comparing to, and which result from your program are you comparing it to?
How do you know when you have reached and adequate solution?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Hi,
I have the understanding that you are facing trouble in indexing of arrays. Unlike C++ or Python, indexing starts from 1 in MATLAB. So if you write A(0), then it will throw error. The following code may be useful for your understanding:
a = 0;
b = 1;
n = 110;
k = 8.61733*10^(-5);
T = 300 ;
q = 1.60218*10^(-19);
E = 8.8542*10^(-12)
x= linspace(a,b,n);
h= x(2)-x(1);
A = zeros(size(x));
B = zeros(size(x));
A(1)=3;
B(1)=1;
C = 2*x + 3
for i = 1:n-1
A(i+1)=A(i)-h.*B(i)-(0.5.*h.^2*q.*C(i))./(k*T*E);
B(i+1)=B(i)+((h.*q)/(k*T*E))*0.5*(C(i)+C(i+1));%equation error
end
plot(transpose(x),A,'.')
Also please note that similar operations on each element of array can be done at once in MATLAB and do not require to run a loop like in case of C.
C = 2*x + 3
Hope that helps!

1 Kommentar

Thanks for your reply.
But no it is not correct. It doesn't reflect C value as I could see same graph.
I've followed Walter Roberson's coding. Now I'm in the situation to fix the ploting graph as loop structure and x length are different.

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by