heat equation not working
Show older comments
This is a solution for my homework, but the code gives us an error on the next line
I appreciate your help in that that
T(i,j+1)=T(i.j)+((dt)/(dx.^2))*(T(i+1,j)-2*T(i,j)+T(i-1,j));
clc;
clear all;
for i=1:15
x(i)=i;
x1(i)=(i-1)/15;
for j=1:10
T(i,j)=0;
end
end
T(x,1)=sin(pi*x1);
dx=0.1;
dt=0.04;
for j=1:10
for i=2:15
T(i,j+1)=T(i.j)+((dt)/(dx.^2))*(T(i+1,j)-2*T(i,j)+T(i-1,j));
end
end
for i=1:15
T0(i)=T(i,1);
T1(i)=T(i,2);
T2(i)=T(i,3);
T3(i)=T(i,4);
T4(i)=T(i,5);
T5(i)=T(i,6);
end
title("Heat");
ylabel("y");
xlabel("x");
legend("HEAT EQ");
Answers (1)
Voss
on 12 Dec 2021
The error is because you have a typo in that line:
T(i.j)
should be:
T(i,j)
Once you correct that, you will get a new error on the same line. This error is due to trying to refer to T(i+1,j) when i is 15, i.e., T(16,j) but T has size 15 in dimension 1. To fix that, I guess the i loop should go from 2 to 14 rather than 2 to 15:
for i=2:14
Categories
Find more on PDE Solvers in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!