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

1 Ansicht (letzte 30 Tage)
I'm trying to create a while loop that will take the average of each directly adjacent vector in order to calculate for a new temperature of each part of the block. I keep getting the error in the title and I'm not sure how to fix it.
Here is the code I have:
Dplate=zeros(30,50);
Dplate(2:29,1)=120;
Dplate(2:29,50)=40;
Dplate(1,:)=40;
Dplate(30,:)=40;
Dplate(10:20,10:40)=40;
TL=sum(sum(Dplate));
while TL>=.001
for r=1:30
for c=1:50
Dplate1=Dplate;
Dplate(1,1)=(Dplate(1,2)+Dplate(2,1))./2;
Dplate(30,1)=(Dplate(30,2)+Dplate(29,1))./2;
Dplate(1,50)=(Dplate(1,49)+Dplate(2,50))./2;
Dplate(30,50)=(Dplate(29,50)+Dplate(30,49))/2;
Dplate(1,2:49)=(Dplate(r-1,c)+Dplate(r+1,c)+Dplate(r,c-1))./3;
Dplate(2:29,1)=(Dplate(r+1,c)+Dplate(r-1,c)+Dplate(r,c+1))./3;
Dplate(30,2:49)=(Dplate(r+1,c)+Dplate(r,c-1)+Dplate(r,c+1))./3;
Dplate(2:29,50)=(Dplate(r+1,c)+Dplate(r-1,c)+Dplate(r,c-1))./3;
Dplate(2:29,2:49)=(Dplate(r+1,c)+Dplate(r-1,c)+Dplate(r,c+1)+Dplate(r,c-1))./4;
Dplate2=Dplate;
end
subplot(1,1,1)
contourf(Dplate)
end
Dfinal=abs(Dplate2-Dplate1);
TL=sum(sum(Dfinal));
end

Antworten (1)

David Hill
David Hill am 10 Apr. 2020
Not sure if this will help you or not.
Dplate=zeros(30,50);
Dplate(2:29,1)=120;
Dplate(2:29,50)=40;
Dplate(1,:)=40;
Dplate(30,:)=40;
Dplate(10:20,10:40)=40;
TL=sum(sum(Dplate));
while TL>=.001
Dplate1=Dplate;
Dplate(1,1)=(Dplate(1,2)+Dplate(2,1))/2;
Dplate(30,1)=(Dplate(30,2)+Dplate(29,1))/2;
Dplate(1,50)=(Dplate(1,49)+Dplate(2,50))/2;
Dplate(30,50)=(Dplate(29,50)+Dplate(30,49))/2;
for c=2:49
Dplate(1,c)=(Dplate(1,c-1)+Dplate(2,c)+Dplate(1,c+1))/3;
Dplate(30,c)=(Dplate(30,c-1)+Dplate(29,c)+Dplate(30,c+1))/3;
end
for r=2:29
Dplate(r,1)=(Dplate(r-1,1)+Dplate(r,2)+Dplate(r+1,1))/3;
Dplate(r,50)=(Dplate(r-1,50)+Dplate(r,49)+Dplate(r+1,50))/3;
end
for r=2:29
for c=2:49
Dplate(r,c)=(Dplate(r+1,c)+Dplate(r-1,c)+Dplate(r,c+1)+Dplate(r,c-1))/4;
end
end
TL=norm(Dplate-Dplate1);
end

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by