# plot graph for multiple values for one of the variables on the same axis (and in different colours)

11 views (last 30 days)
Mohammed Haque on 18 Apr 2022
Edited: KSSV on 18 Apr 2022
I want to plot multiple lines for different values of r2 % outer radius of sphere, m
r1=18e-2; % inner radius of sphere, m
>> r2=24e-2; % outer radius of sphere, m
>> k=0.034; % thermal conductivity of sphere wall, W /(m C)
>> bc1=40; % temperature at inner sphere wall, deg C
>> bc2=-81; % temperature at outer sphere wall, deg C
>> n=100*2*2*2*2; % number of unknown temperatures between the two radii
>> m=(r2-r1)/(n+1); % step size: difference between two consecutive difference pointa, m
>>
>> %%%%%%%%%% GENERATION OF MATRIX A5
>> for i=1:n
r(i)=r1+i*m;
a(i,i)=-2*r(i)^2/m^2;
if(i==1)
a(i,i+1)=(r(i)^2/m^2 +r(i)/(m));
end
if((i>1)&&(i<n))
a(i,i-1)=(r(i)^2/m^2 -r(i)/(m));
a(i,i+1)=(r(i)^2/m^2 +r(i)/(m));
end
if(i==n)
a(i,i-1)=(r(i)^2/m^2 -r(i)/(m));
end
end
>> %%%% BOUNDARY CONDITION MATRIX GENERATION OF MATRIX B5
>> b(1,1)=-(r(1)^2/m^2 -r(i)/(m))*bc1;
>> b(n,1)=-(r(n)^2/m^2 +r(i)/(m))*bc2;
>>
>> t=inv(a)*b;
>>
>> temp(1,1)=bc1;
>> temp(2:n+1,1)=t(1:n);
>> temp(n+2,1)=bc2;
>>
>>
>> figure(1)
>> ylabel('Temperature, (^{o}C)','fontsize',20,'fontweight','b')
r2 values:[24e-2,30e-2,36e-2]

KSSV on 18 Apr 2022
Edited: KSSV on 18 Apr 2022
You have to initilaize the variables inside the loop. Read about initliaing an array in aloop.
r1=18e-2; % inner radius of sphere, m
r2=24e-2; % outer radius of sphere, m
k=0.034; % thermal conductivity of sphere wall, W /(m C)
bc1=40; % temperature at inner sphere wall, deg C
bc2=-81; % temperature at outer sphere wall, deg C
n=100*2*2*2*2; % number of unknown temperatures between the two radii
R2 = [24e-2,30e-2,36e-2] ;
for j = 1:length(R2)
r2 = R2(j) ;
m=(r2-r1)/(n+1); % step size: difference between two consecutive difference pointa, m
%%%%%%%%%% GENERATION OF MATRIX A5
for i=1:n
r(i)=r1+i*m;
a(i,i)=-2*r(i)^2/m^2;
if(i==1)
a(i,i+1)=(r(i)^2/m^2 +r(i)/(m));
end
if((i>1)&&(i<n))
a(i,i-1)=(r(i)^2/m^2 -r(i)/(m));
a(i,i+1)=(r(i)^2/m^2 +r(i)/(m));
end
if(i==n)
a(i,i-1)=(r(i)^2/m^2 -r(i)/(m));
end
end
%%%% BOUNDARY CONDITION MATRIX GENERATION OF MATRIX B5
b(1,1)=-(r(1)^2/m^2 -r(i)/(m))*bc1;
b(n,1)=-(r(n)^2/m^2 +r(i)/(m))*bc2;
t=inv(a)*b;
temp(1,1)=bc1;
temp(2:n+1,1)=t(1:n);
temp(n+2,1)=bc2;
T(:,j) = temp' ;
end
figure(1)
plot(R,T)
ylabel('Temperature, (^{o}C)','fontsize',20,'fontweight','b')

### Categories

Find more on Networks in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by