error message vectors must be the same length

2 visualizaciones (últimos 30 días)
salem alnaqbi
salem alnaqbi el 19 de Jul. de 2018
Comentada: salem alnaqbi el 19 de Jul. de 2018
Hello, im trying to plot the temperature distribution vs time, I get the right temp answer from running the code but when i try to plot it vs time it gives me an error message saying that vectors must be the same length. The question is asking to plot the surface temperature as a function of time from 0 to 1800 seconds. Thank you.
clear
clc
close
L=0.2;
Ti=600;
Tinf=22;
h=240;
k=80.2;
alpha=3.31e-5;
dx=0.01;
dt=1.5;
Fo=(alpha*dt)/dx^2;
Bii=(h*dx)/k;
Bio=(h*dx)/k;
x=[0:dx:L];
n=length(x);
T=Ti*ones(1,n);
t=[0:dt:1800];
for k=2:length(t)
Tnew(1)=T(1)*(1-2*Fo-2*Bio*Fo)+2*Fo*T(2)+2*Bio*Fo*Tinf;
for i=2:n-1
Tnew(i)=T(i)*(1-2*Fo)+Fo*(T(i+1)+T(i-1));
end
Tnew(n)=T(n)*(1-2*Fo-2*Bii*Fo)+2*Fo*T(n-1)+2*Bii*Fo*Tinf;
T=Tnew;
end
Tnew(n)
plot(t,Tnew)
  4 comentarios
salem alnaqbi
salem alnaqbi el 19 de Jul. de 2018
im trying to calculate the temperature distribution through a slab 1d transient heat conduction using explicit finite difference method
Guillaume
Guillaume el 19 de Jul. de 2018
@Rik, "k is not used anywhere in your loop, so your loop does the same thing every time"
Actually, the k loop calculates a new T based in its value in the previous iteration, so there's no issue on that front.
I suspect that the intent of the code is to plot the values of T (== Tnew) at a given index for each k.

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 19 de Jul. de 2018
Before you do anything else, the first thing you need to do is to comment your code. The code should start with:
%nameofscript: Calculate the temperature distribution through a ... finish the description
%inputs are: ...
%outptus are:
Then each variable should have a comment attached to it when it's declared
L = 0.2; %length of slab?
Ti = 600; %initial temperature somewhere?
...
I suspect that you're trying to plot the evolution of the temperature with time at a given x position, L possibly. The problem is that your current code overwrite all the temperatures at each timestep, so you're left with just the temperature at the last time step.
You can fix this two ways: Either you keep all the temperatures for all the time step and all the position. You'll need enough memory to store a numel(t) * numel(x) matrix. The advantage of this is that you can then easily plot the temperature for any position. The other option is to only keep the temperatures for the position of interest but if you need another position you'll have to recalculate everything. Of course, it uses less memory (only numel(t) elements to store|
Keeping all elements:
T = zeros(numel(t), numel(x)); %temperature of the slab at time t (rows) and position x (columns)
T(1, :) = Ti; %temperature at t = t0;
for k = 2:numel(t)
T(k, 1) = T(k-1, 1)*(1-2*Fo-2*Bio*Fo)+2*Fo*T(k-1, 2)+2*Bio*Fo*Tinf;
for i = 2:numel(x)-1
T(k, i) = T(k-1, i)*(1-2*Fo)+Fo*(T(k-1, i+1)+T(k-1, i-1));
end
T(k, n) = T(k-1, n)*(1-2*Fo-2*Bii*Fo)+2*Fo*T(k-1, n-1)+2*Bii*Fo*Tinf;
end
%plot temperature for all t at a different x
plot(t, T(:, end), 'DisplayName', 'x = L');
hold on
plot(t, T(:, 1), 'DisplayName', 'x = 0');
plot(t, T(:, 100), 'DisplayName', sprintf('x = %f', x(100)));

Más respuestas (0)

Categorías

Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by