Clear Filters
Clear Filters

For Loop through Matrix

4 views (last 30 days)
Brittany Burns
Brittany Burns on 2 May 2022
Edited: Torsten on 3 May 2022
Ts is changing over time. I need to get Ts to change in matrix T. How do I get it to change with each iteration within the matrix T?
clear all;clc
%Given
x=0:0.25:1; %thickness [m]
rho=2300; %density [kg/m^3]
k=1.4; %Thermal conductivity [W/mK]
c=880; %thermal coeff [J/kgK]
t=[2 11 45]*3600; %time [s]
%let
deltax=1/4;
deltat=1;
a=k/deltax;
b=(rho*deltax*c)/deltat;
%Initial conditions
Tx=300+400.*(x./2)-400*(x./2).^2;
%Solve for T'
for k=1:length(x)
for i=1:length(t)
if i<=10*3600
Ts=300+10/3600*i;
elseif i>10*3600 && i<=25*3600
Ts=400-5/3600*(i-10*3600);
else
Ts=325;
end
%Create matrices
A=1/b*[(-2*a+b) a 0 0;
a (-2*a+b) a 0;
0 a (-2*a+b) a;
0 0 a (-2*a+b)];
T=[Tx(1); Tx(2); Tx(3); Ts];
% Solve for B
B=A*T;
end
end
  4 Comments
Torsten
Torsten on 2 May 2022
Your T vector has only 4 elements, but your x vector has 5.
Further, T is the vector of unknown - thus B = A*T does not make sense.
What is the differential equation together with the boundary conditions you are trying to solve ?
Brittany Burns
Brittany Burns on 2 May 2022
I'm trying to solve for T' on the RHS

Sign in to comment.

Accepted Answer

Torsten
Torsten on 2 May 2022
Edited: Torsten on 3 May 2022
rho=2300; %density [kg/m^3]
k=1.4; %Thermal conductivity [W/mK]
c=880; %thermal coeff [J/kgK]
xstart = 0.0;
xend = 1.0;
nx = 100;
dx = (xend-xstart)/(nx-1);
x = xstart:dx:xend;
tstart = 0.0;
tend = 45*3600;
dt = 0.5*dx^2*rho*c/k;
nt = ceil((tend-tstart)/dt);
t = linspace(tstart,tend,nt);
T0 = Tinit(x);
T = zeros(nt,nx);
T(1,:) = T0;
for j = 1:nt-1
Ts = Texternal(t(j+1)/3600);
T(j+1,1) = Ts;
T(j+1,2:nx-1) = T(j,2:nx-1) + dt*k/(rho*c)*(T(j,3:nx)-2*T(j,2:nx-1)+T(j,1:nx-2))/dx^2;
Tnxp1 = T(j,nx-1);
T(j+1,nx) = T(j,nx) + dt*k/(rho*c)*(T(j,nx-1)-2*T(j,nx)+Tnxp1)/dx^2;
end
to = [2 11 45]*3600;
it = arrayfun(@(tout)find(t-tout>=0,1,'first'),to);
plot(x,[T(it(1),:);T(it(2),:);T(it(3),:)])
function Ts = Texternal(t)
if t <= 10
Ts = 300 + 10*t;
elseif t > 10 && t <= 25
Ts = 400 - 5*(t-10);
else
Ts = 325;
end
end
function T0 = Tinit(x)
T0 = 300 + 400.*(x./2) - 400*(x./2).^2;
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!