- #1
range.rover
- 16
- 0
HI guys,this is my first programming experience , i have developed an MATLAB code for steady state heat conduction equation , on governing equation
dt2 /dx2 + dt2/dy2 = -Q(x,y)
i have solved this equation with finite difference method, As far as i know if we increase the mesh size it leads to decrease in the distance between the nodes and leads us to the true solution.i guess the final solution will be constant.
but my code is not converging or moving towerds true solution can anybody help me out on this. Here the solution is just varying with the mesh size.
what can be the reason behind this.
this is my code.
%%%%%% STRICTLY ONLY FOR ODD NUMBERS %%%%%%%%%
clear all
close all
clc
n = 101;% n grids and has n interior points per dimension
m = n+2;
x = linspace(0,1,m); % grid points x including boundaries
y = linspace(0,1,m); % grid points y including boundaries
dx = x(2)-x(1);
h = dx;
dy = dx;
k=1; %thermal conductivity
Q=8000; % heat source
Iint = 2:n+1;
Jint = 2:n+1;
%Right hand side matrix
B=zeros(n,n);
if (mod(n,2)==0)
B(n/2,n/2)=Q;
else
B((n+1)/2,(n+1)/2)=Q;
end
%Main Matrix
Told = zeros(m,m);
% boundary values
Told(1:m,1) = 0;
Told(1:m,m) = 0;
Told(1,1:m) = 0;
Told(m,1:m) = 0;
Tsoln = Told;
B(:,1) = B(:,1) + Told(Iint,1)*k/h^2; % boundary valuues are added from the left hand side matrix including k and h
B(:,n) = B(:,n) + Told(Iint,m)*k/h^2;
B(1,:) = B(1,:) + Told(1,Jint)*k/h^2;
B(n,:) = B(n,:) + Told(m,Jint)*k/h^2;
% Matrix formations
F = reshape(B,n*n,1);
I = speye(n);
e = ones(n,1);
T = spdiags([e -4*e e],[-1 0 1],n,n);
% v = full(T);
S = spdiags([e e],[-1 1],n,n);
% w = full(S);
% v = kron(I,T);
% w = kron(S,I);
% z = (kron(I,T) + kron(S,I));
% nny = full(z);
A = (kron(I,T) + kron(S,I))* k / h^2; % A is a tridiagonal spurse matix multiplied and dvided by h^2
% z = full(A);
% Tvec = abs(A\F); Solving AX = B ;
Tvec = abs(mldivide(A,F));
Tsoln(Iint,Jint) = reshape(Tvec,n,n);
sol = max(max(Tvec)) ;
% Ploting
figure,surf(x,y,Tsoln),
figure, contour(x,y,Tsoln),
title('Temperature (Steady State)'),xlabel('x'),ylabel('y'),colorbar
figure,pcolor(x,y,Tsoln), shading interp
title('Temperature (Steady State)'),xlabel('x'),ylabel('y'),colorbar
dt2 /dx2 + dt2/dy2 = -Q(x,y)
i have solved this equation with finite difference method, As far as i know if we increase the mesh size it leads to decrease in the distance between the nodes and leads us to the true solution.i guess the final solution will be constant.
but my code is not converging or moving towerds true solution can anybody help me out on this. Here the solution is just varying with the mesh size.
what can be the reason behind this.
this is my code.
%%%%%% STRICTLY ONLY FOR ODD NUMBERS %%%%%%%%%
clear all
close all
clc
n = 101;% n grids and has n interior points per dimension
m = n+2;
x = linspace(0,1,m); % grid points x including boundaries
y = linspace(0,1,m); % grid points y including boundaries
dx = x(2)-x(1);
h = dx;
dy = dx;
k=1; %thermal conductivity
Q=8000; % heat source
Iint = 2:n+1;
Jint = 2:n+1;
%Right hand side matrix
B=zeros(n,n);
if (mod(n,2)==0)
B(n/2,n/2)=Q;
else
B((n+1)/2,(n+1)/2)=Q;
end
%Main Matrix
Told = zeros(m,m);
% boundary values
Told(1:m,1) = 0;
Told(1:m,m) = 0;
Told(1,1:m) = 0;
Told(m,1:m) = 0;
Tsoln = Told;
B(:,1) = B(:,1) + Told(Iint,1)*k/h^2; % boundary valuues are added from the left hand side matrix including k and h
B(:,n) = B(:,n) + Told(Iint,m)*k/h^2;
B(1,:) = B(1,:) + Told(1,Jint)*k/h^2;
B(n,:) = B(n,:) + Told(m,Jint)*k/h^2;
% Matrix formations
F = reshape(B,n*n,1);
I = speye(n);
e = ones(n,1);
T = spdiags([e -4*e e],[-1 0 1],n,n);
% v = full(T);
S = spdiags([e e],[-1 1],n,n);
% w = full(S);
% v = kron(I,T);
% w = kron(S,I);
% z = (kron(I,T) + kron(S,I));
% nny = full(z);
A = (kron(I,T) + kron(S,I))* k / h^2; % A is a tridiagonal spurse matix multiplied and dvided by h^2
% z = full(A);
% Tvec = abs(A\F); Solving AX = B ;
Tvec = abs(mldivide(A,F));
Tsoln(Iint,Jint) = reshape(Tvec,n,n);
sol = max(max(Tvec)) ;
% Ploting
figure,surf(x,y,Tsoln),
figure, contour(x,y,Tsoln),
title('Temperature (Steady State)'),xlabel('x'),ylabel('y'),colorbar
figure,pcolor(x,y,Tsoln), shading interp
title('Temperature (Steady State)'),xlabel('x'),ylabel('y'),colorbar