Clear Filters
Clear Filters

Got to do this program about heat transer and finite equations and although the program is done I have litle errors dont know how to fix. The issuesare remarked down. THANKS

1 view (last 30 days)
>> %ANA BATRES MATLAB PROGRAM FINITE EQUATIONS MODELING.
clear;
B=24; %(mm) distance between heat wires
D=4; %(mm)distance from upper plate to the heat wire
a=6; %(mm) distance from upper plate to inferior plate
T_inf=30; %(centigrades), we dont change to kelvin the rest of the parameters are in degrees.
k=2; %(w/m2C), conductive parameter
h=100; %(W/m2C), convective parameter
q_perunit=50; %(W/m)
mesh=14; %this is choosed and can be changed as we want
delta_y=(B+4)/mesh; %sidelength of each element, same in both directions
delta_y=(delta_y)*(10^3); %I pass it to m.
>> Bi=h*delta_y/k; %biot number
>> n=mesh+1;
>> m=mesh+1;
>> k=n*m;
>> X=0:1/mesh:1;
>> Y=0:1/mesh:1;
>>
>> Con=ones(n,m);
>> A=zeros(k,k);
>> C=zeros(k,1);
>>
>> for i=1:n;
Con(i,1)=2;
Con(i,m)=4;
Con(n,i)=2;
end
>>
>> for i=2:m-1;
Con(1,i)=3;
end
>>
>> counter=1;
>> for i=1:m;
for j=1:n;
switch Con(i,j)
case 1
%internal node
A(counter,counter+1)=1;
A(counter,counter-1)=1;
A(counter,counter+n)=1;
A(counter,counter-n)=1;
A(counter,counter)=-4;
case 2
A(counter,counter+1)=1;
A(counter,counter-1)=1;
A(counter,counter+n)=1;
A(counter,counter-n)=1;
A(counter,counter)=-4;
C(counter,1)=-q_perunit*delta_y;
%this is an interior node with generation
case 3
%corner node with convection
A(counter,counter+1)=2;
A(counter,counter+n)=1;
A(counter,counter)=-4-Bi;
C(counter,1)=-Bi*T_inf;
case 4
%corner node with no convection (insulated surface)
A(counter,counter+1)=2;
A(counter,counter+n)=1;
A(counter,counter)=-4;
case 5
%convection and conduction
A(counter,counter-1)=1;
A(counter,counter+1)=1;
A(counter,counter+n)=2;
A(counter,counter)=-4-Bi;
C(counter,1)=-2*Bi*T_inf;
end;
counter=counter+1;
end;
end;
Index in position 2 is invalid. Array indices must
be positive integers or logical values.
>> T2=inv(A)*C;
Warning: Matrix is singular to working precision.
>> counter2=1;
>> for i=1:m;
for j=1:n;
T(i,j)=T2(counter2);
counter2=counter2+1;
end
end
>>
>> figure(1)
>> [C1,h1]=contour(X,Y,T,15);
Warning: Contour not rendered for non-finite ZData
> In contour (line 51)
>> clabel(C1,h1)
>> grid on
>>
>> figure (2)
>> surf(X,Y,T)
>>
>>
>> figure(1)
>> surf(T)

Answers (1)

Neelanshu
Neelanshu on 8 Feb 2024
Hi Ana,
It looks like you're trying to fix the "Array indices must be positive integers or logical values" error that's popping up in your MATLAB code.
As Walter pointed out, using A(counter, counter-1) when counter = 1 results in counter-1 being equal to 0. Since MATLAB indexing starts at 1, attempting to access A(1, 0) is invalid. Similarly, when i=1 and j=1, the left boundary case statement activates with counter = 1, but A(counter, counter-n) would again result in negative index values as illustreated in the image, which are not permitted.
To fix this error, please ensure that you are using positive integers for array indexing. When counter is 1, you must avoid using expressions that result in zero or negative indices. You can implement conditional checks to handle boundary cases or adjust your loop ranges to start from a higher index when necessary.
For more information on array indexing in MATLAB and to avoid such errors, you can refer to the following documentation:
Hope this helps.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!