Index in position 1 is invalid. Array indices must be positive integers or logical values.

1 view (last 30 days)
Getting the error
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in Untitled (line 35) plot(x, Tdata(0,:));
when running. Cannot figure out how to make the plot work in the following script.
b = 0.0003;
a = 1.5;
d = 5.3;
c = 1.8;
R = 2/100; %m
Tr = -45; %degC
Ti = -30; %degC
N = 10;
x = linspace(-R,R,(2*N+1));
dx = x(2)-x(1);
T = zeros(size(x));
T = T + Ti;
T(1) = Tr;
T(end) = Tr;
Tdata = T;
dt = 1.8;
T1 = T;
T2 = zeros(size(T1));
ndt = 1800/dt;
for j = 1:ndt
T2(1) = Tr;
T2(end) = Tr;
for i=2; length(T2)-1;
T2(i) = T1(i) + dt/2/dx/dx/(c+d*T1(i))*((2*a+b*(T1(i+1)+T1(i)))*(T1(i+1)-T1(i))-(2*a+b*(T1(i-1)+T1(i)))*(T1(i)-T1(i-1)));
end
Tdata = [Tdata;T2];
T1=T2;
end
plot(x, Tdata(0,:));

Answers (1)

Image Analyst
Image Analyst on 7 Dec 2019
Edited: Image Analyst on 7 Dec 2019
The first row is not 0 -- it's 1. Try
plot(x, Tdata(1,:), 'b-', 'LineWidth', 2);
This is one of the most frequently asked questions, so for a full discussion with solutions, see the FAQ.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!