Matlab code for the Formula
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Following is the Analytical solution of a Heat Diffusion formula where x is length of Rod from 0 to 1 meter with an increment of 0.1. Also t is the time from 0 to 10 minute with an increment of 1 min. Value of r is 0.1. The result should almost match with the following table
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/171424/image.jpeg)
. The result in the table was obtained using the Numerical Method (Explicit Method) of the problem.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/171425/image.jpeg)
I've tried the following code:
close all clear clc
x = 0:0.1:1; t = 0:1:10; r = 0.1; [X,T] = meshgrid(x,t) S = 0;
for n=1:1e6 S = S + sin(pi*n/2)*sin(n*pi*X).*exp(-n*n*r*r*t)/(n*n); end U = (8/pi*pi)*S
What is wrong with this code?
2 Kommentare
John D'Errico
am 21 Mär. 2018
Bearbeitet: John D'Errico
am 21 Mär. 2018
What have you tried? If nothing, why not? It is by making an effort that you will learn, not by being given the solution to your homework on a platter. So show what you tried. If you do, you will have abetter chance of getting some help here.
Antworten (2)
Image Analyst
am 21 Mär. 2018
One way is a more direct, though not vectorized, approach of using for loops over t and x:
x = 0:0.1:1;
t = 0:1:10;
r = 0.1;
U = zeros(length(t), length(x));
for kt = 1 : length(t)
for kx = 1 : length(x)
s = 0;
for n=1:1e6
s = s + sin(pi*n/2).*sin(n*pi*x(kx)).*exp(-n*n*r*r*t(kt))/(n*n);
end
U(kt, kx) = 8 * s / pi^2;
end
end
U
0 Kommentare
Abraham Boayue
am 21 Mär. 2018
Bearbeitet: Abraham Boayue
am 21 Mär. 2018
Hey Rasel, your code was almost right. You made just a few errors, instead of initializing the sum as you did, you should have just initialized U as a matrix. This is your code with a few changes.
clear variables
close all
x = 0:0.1:1;
t = 0:10;
r = 0.1;
N = length(x);
M = length(t);
U = zeros(N,M); % This is what you should have done to start with.
[x,t] = meshgrid(x,t);
for n = 1: 100
U = U + ((1/n^2)*sin(0.5*pi*n)*sin(pi*n*x).*exp(-n^2*r^2*t))';
end
U = (8/pi.^2)*U;
disp(U')
figure
surf(x,t,U')
grid
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!