Info

This question is closed. Reopen it to edit or answer.

Subscript indicies must either be real positive integers or logicals

1 view (last 30 days)
Hi,
I am trying to multiply two matrices together in a for loop however when I run my code it says that my matrix B, "Subscript indicies must either be real positive integers or logicals".
What does this mean and how can I fix it?
Thank you!
clear all;
clc;
K=0.6285;
alpha=1.516*10^-7;
dT=50;
r=0.02;
h=1000;
Fo=alpha*dT/(r/2)^2;
Tinf=100;
Bi=h*(r/2)/K;
T1(1)=2; % Temp @ Node-1
T2(1)=2; % Temp @ Node-2
T3(1)=2; %Temp @ Node-3
time(1)=0;
rho=997;
c=1.99;
%% Implicit Method
for k=1:20
A=[-((T1(k)*K*4*pi*(r/4)^2)/(r/2))-((rho*(4/3)*(r/4)^3*c*T1(k))/dT) ((T2(k)*K*4*pi*(r/4)^2)/(r/2)) 0; ((T1(k)*K*4*pi*(r/4)^2)/(r/2)) -((T2(k)*K*4*pi*(r/4)^2)/(r/2))-(((K*4*pi*((r/2)+(r/4))^2))/(r/2))-((rho*((4/3)*pi*(((r/2)+(r/4)^3)-(r/3)^3))*c*T2(k))/dT) ((K*4*pi*((r/2)+(r/4)^2)*T3(k))/(r/2)); 0 ((K*4*pi*r^2*T2(k))/(r/2)) -((K*4*pi*r^2*T3(k))/(r/2))-(h*4*pi*r^2*T3(k))-((rho*(4/3)*pi*r^3*c*T3(k))/dT)];
B=[-((rho*(4/3)*(r/4)^3*c*T1(k-1))/dT); -((rho((4/3)*pi*(((r/2)+(r/4)^3)-(r/3)^3))*c*T2(k-1))/dT); -((rho*(4/3)*pi*r^3*c*T3(k-1))/dT)-h*4*pi*r^2*Tinf];
X=inv(A)*B;
T1(k+1)=X(1);
T2(k+1)=X(2);
time(k+1)=k*dT;
whos ind
end
close all
plot(time, T1,'--*k',time, T2,'--pm',time, T3,'--og')
legend('T1','T2','T3')
xlabel('Time(sec)')
Subscript indices must either be real positive integers or logicals.
Error in Implicit_Method (line 22)
ind_double=[-((rho*(4/3)*(r/4)^3*c*T1(k-1))/dT);
-((rho((4/3)*pi*(((r/2)+(r/4)^3)-(r/3)^3))*c*T2(k-1))/dT);
-((rho*(4/3)*pi*r^3*c*T3(k-1))/dT)-h*4*pi*r^2*Tinf];

Answers (1)

James Tursa
James Tursa on 30 Mar 2020
... rho((4/3) ...
should be
... rho*((4/3) ...
  6 Comments
Monica DelaCruz
Monica DelaCruz on 30 Mar 2020
If I change k=2, I get a new error that says (I apologize about the format of my matrices):
Index exceeds matrix dimensions.
Error in Explicit_Method (line 21)
A=[-((T1(k)*K*4*pi*(r/4)^2)/(r/2))-((rho*(4/3)*(r/4)^3*c*T1(k))/dT)
((T2(k)*K*4*pi*(r/4)^2)/(r/2)) 0; ((T1(k)*K*4*pi*(r/4)^2)/(r/2))
-((T2(k)*K*4*pi*(r/4)^2)/(r/2))-(((K*4*pi*((r/2)+(r/4))^2))/(r/2))-((rho*((4/3)*pi*(((r/2)+(r/4)^3)-(r/3)^3))*c*T2(k))/dT)
((K*4*pi*((r/2)+(r/4)^2)*T3(k))/(r/2)); 0 ((K*4*pi*r^2*T2(k))/(r/2))
-((K*4*pi*r^2*T3(k))/(r/2))-(h*4*pi*r^2*T3(k))-((rho*(4/3)*pi*r^3*c*T3(k))/dT)];
Monica DelaCruz
Monica DelaCruz on 30 Mar 2020
I think I've fixed my last problem, however this is my new error with my corrected code
Everything before this point stays the same
%% Implicit Method
A=[-((T1(1)*K*4*pi*(r/4)^2)/(r/2))-((rho*(4/3)*(r/4)^3*c*T1(1))/dT) ((T2(1)*K*4*pi*(r/4)^2)/(r/2)) 0; ((T1(1)*K*4*pi*(r/4)^2)/(r/2)) -((T2(1)*K*4*pi*(r/4)^2)/(r/2))-(((K*4*pi*((r/2)+(r/4))^2))/(r/2))-((rho*((4/3)*pi*(((r/2)+(r/4)^3)-(r/3)^3))*c*T2(1))/dT) ((K*4*pi*((r/2)+(r/4)^2)*T3(1))/(r/2)); 0 ((K*4*pi*r^2*T2(1))/(r/2)) -((K*4*pi*r^2*T3(1))/(r/2))-(h*4*pi*r^2*T3(1))-((rho*(4/3)*pi*r^3*c*T3(1))/dT)];
for k=2:20
B=[-((rho*(4/3)*(r/4)^3*c*T1(k-1))/dT); -((rho*((4/3)*pi*(((r/2)+(r/4)^3)-(r/3)^3))*c*T2(k-1))/dT); -((rho*(4/3)*pi*r^3*c*T3(k-1))/dT)-(h*4*pi*r^2*Tinf)];
X=inv(A)*B;
T1(k+1)=X(1);
T2(k+1)=X(2);
time(k+1)=k*dT;
end
close all
plot(time, T1,'--*k',time, T2,'--pm',time, T3,'--og')
legend('T1','T2','T3')
xlabel('Time(sec)')
Index exceeds matrix dimensions.
Error in Explicit_Method (line 21)
B=[-((rho*(4/3)*(r/4)^3*c*T1(k-1))/dT);
-((rho*((4/3)*pi*(((r/2)+(r/4)^3)-(r/3)^3))*c*T2(k-1))/dT);
-((rho*(4/3)*pi*r^3*c*T3(k-1))/dT)-(h*4*pi*r^2*Tinf)];

Tags

Community Treasure Hunt

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

Start Hunting!