Filter löschen
Filter löschen

For Loop through Matrix

4 Ansichten (letzte 30 Tage)
Brittany Burns
Brittany Burns am 2 Mai 2022
Bearbeitet: Torsten am 3 Mai 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 Kommentare
Torsten
Torsten am 2 Mai 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 am 2 Mai 2022
I'm trying to solve for T' on the RHS

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Torsten
Torsten am 2 Mai 2022
Bearbeitet: Torsten am 3 Mai 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

Weitere Antworten (0)

Kategorien

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

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by