Εφαρμογή της μεθόδου πεπερασμένων διαφορών στην εξίσωση θερμότητας Να γραφεί script το οποίο να επιλύει αριθμητικά της γενική εξίσωση θερμότητας με χρήση της προς τα εμπρός παραγώγου ως προς το χρόνο, 2 u u, 0 x 1 2 t x u(0, x) u ( x), 0 x 1 u( t,0) T0, u( t,1) T1, 0 t 4 για τις ακόλουθες περιπτώσεις: ( i) 0.1, u(0, x) 20, u( t,0) 5, u( t,1) 0, ( ii) 0.1, u(0, x) 5, u( t,0) 20, u( t,1) 30, ( iii) 0.1, u(0, x) 20sin(2 x), u( t,0) 5, u( t,1) 0 o Σε όλες τις περιπτώσεις να θεωρήσετε Ν=220 σημεία διαμέρισης για το χρόνο και Μ=11 σημεία διαμέρισης για το χώρο. Tο πρόγραμμα αυτό να εμφανίζει με τη χρήση subplot τέσσερα γραφήματα. Το πρώτο να εμφανίζει τη καμπύλη της αριθμητικής λύσης με χρήση της εντολής mesh. Στο δεύτερο γράφημα να εμφανίζεται το profile της θερμοκρασίας για τα σημεία x=0.2,0.4.0.6,0.8 στα σημεία της διαμέρισης, ενώ στο τρίτο το profile της θερμοκρασίας για τα χρονικά σημεία t=0.8,1.6,2.4,3.2 στα αντιστοιχα σημεία της διαμέρισης. Τέλος, στο τέταρτο γράφημα να εμφανίζεται η γραφική παραστάση της αρχικής κατανομής uo( x ). heat.m L=1; % length of domain in x direction tmax=4; % time period gamma=0.1; % diffusitivity N=220; % number of time steps M=11; % number of x-nodes dt=tmax/(n-1); % time step dx=l/(m-1); % spatial step mu=gamma*dt/(dx^2) % constant u=zeros(n,m); % define the size of solution t=[0:dt:tmax]; % time x=[0:dx:l]; % position % temperature at the boundary x=0 for i=1:n u(i,1)=5; 1
; % temperature at the boundary x=1 for i=1:n u(i,m)=0; ; % initial temperature distribution for j=1:m u(1,j)=20; ; % Implementation of the finite difference scheme-loop over time and x for i=1:n-1 u(i+1,j)=(1-2*mu)*u(i,j)+mu*u(i,j+1)+mu*u(i,j-1); %%%%% Plots %%%%%%%%%%%%%%%% x1=round(0.2/dx+1); x2=round(0.4/dx+1); x3=round(0.6/dx+1); x4=round(0.8/dx+1); t1=round(0.8/dt+1); t2=round(1.6/dt+1); t3=round(2.4/dt+1); t4=round(3.2/dt+1); figure subplot(2,2,1) mesh(x,t,u) xlabel('x') ylabel('t') zlabel('temp') title('temperature distribution') shading interp subplot(2,2,2) plot(t,u(:,x1),'k.-',t,u(:,x2),'b.-',t,u(:,x3),'r.-',t,u(:,x4),'c.-') leg('x=0.2','x=0.4','x=0.6','x=0.8') xlabel('t') ylabel('temp') title('profile at x=0.2,0.4,0.6,0.8') subplot(2,2,3) plot(x,u(t1,:),'.-',x,u(t2,:),'k.-',x,u(t3,:),'r.-',x,u(t4,:),'g.-') 2
xlabel('x') ylabel('temp') title('profile at t=0.8,1.6,2.4,3.2') leg('t=0.8','x=1.6','x=2.4','x=3.2') subplot(2,2,4) plot(x,u(1,:),'k.-') title('initial temp') xlabel('x') ylabel('temp') Εφαρμογή της μεθόδου πεπερασμένων διαφορών στην εξίσωση Laplace A. %%%% Solution of Laplace equation with Seider-Geisser method %%%% clear; format long; L1=3; L2=5; N=31; M=51; dx=l1/(n-1); dy=l2/(m-1); x=[0:dx:l1]; y=[0:dy:l2]; u=ones(n,m); % Iterative parameters epsilon=1e-4; % tolerance for convergence imax=1000; % maximum number of iterations allowed k=1; % initial index value for iteration errmax=zeros(imax,1); kp=[1:imax]; % Initialization at boundary points % bottom u(i,1)=10; ; % top u(i,m)=15; 3
; % left u(1,j)=30; ; % right u(n,j)=3; ; % Guess solution-> average of boundary conditions guess=mean([mean(u(2:n-1,1)),mean(u(2:n-1,m)),mean(u(1,2:m-1)), mean(u(n,2:m-1))]); % Initialize all internal points to the average value u(i,j)=u(i,j)*guess; uiter= u; % uiter = new iteration for solution error=uiter-u; %%% Gauss-Seidel iteration scheme while(k<imax) uiter(i,j) = 0.25*(u(i-1,j)+u(i+1,j)+u(i,j-1)+u(i,j+1)); error(i,j) = abs(uiter(i,j)-u(i,j)); u(i,j)=uiter(i,j); u=uiter; k=k+1; errmax(k)=max(max(error)); if (errmax(k)<epsilon) k fprintf('convergence achieved after k iterations= %3d \n',k) 4
break; % of error test % of while %%%%% Plots %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%% figure; surf(x,y,u') xlabel('x') ylabel('y') zlabel('u') title('temperature') caxis([0 30]) colorbar; figure; h=pcolor(x,y,u'); shading interp; set(h,'edgecolor', 'None') xlabel('x') ylabel('y') title('converged Temperature') caxis([0 30]) colorbar; figure; plot(kp,errmax, '.') title('stopping time') xlabel('iteration') ylabel('errormax') B. %%%% Solution of Laplace equation with Seider-Geisser method %%%% X=4; Y=4; N=41; M=41; dx=x/(n-1); dy=y/(m-1); x = [0:dx:X];y=[0:dy:Y]; 5
% Iterative parameters epsilon = 0.01;%1e-5; % tolerance for convergence imax = 100; % maximum number of iterations allowed k=1; % initial index value for iteration errmax=zeros(imax,1); kp=[1:imax]; %initialize at boundary points for anim=1:30 k=1; u=ones(n,m); % bottom for i=1:n-1 u(i,1)=10+0.5*anim; ; % top for i=1:n-1 u(i,m)=5; ; % left for j=1:m-1 u(1,j)=30-0.5*anim; ; % right for j=1:m-1 u(n,j)=3; ; %Guess an initial value guess=mean([mean(u(:,1)),mean(u(1,:)), mean(u(m-1,:)),mean(u(:,m-1))]); %average of boundary conditions %Initialize all internal points to the boundary value u(i,j)=u(i,j)*guess; 6
uiter = u; % uiter = new iteration for solution err = uiter-u; %%% Gauss-Seidel iteration scheme while(k<imax) for i=2:m-1 for j=2:n-1 uiter(i,j) = 0.25*(u(i-1,j)+u(i+1,j)+u(i,j-1)+u(i,j+1)); err(i,j) = abs((uiter(i,j)-u(i,j))/u(i,j)); u = uiter; k=k+1; errmax(k)=max(max(err)); if (errmax(k)<epsilon) %disp(fprintf('convergence achieved after k iterations=',k)); break; % of error test % of while %subplot(2,1,1) %surf(x,y,u) %xlabel('x') %ylabel('y') %title('temperature') %surf(x,y,u); clims=[0 25]; h=imagesc(x,y,u,clims); colorbar; pause(0.1) % of animation %Plots %subplot(2,1,2) %p%lot(kp,errmax, '.') %title('stopping time') %xlabel('iteration') %ylabel('errormax') %subplot(2,1,1) %surf(x,y,u); %colorbar %subplot(2,1,2) %imagesc(u) %figure; %clims=[0 25]; 7
%h=imagesc(x,y,u,clims) %colorbar; Γ. %%%% Solution of Laplace equation with Seider-Geisser method %%%% clear; L1=3; L2=5; N=31; M=51; dx=l1/(n-1); dy=l2/(m-1); x=[0:dx:l1]; y=[0:dy:l2]; u=ones(n,m); % Iterative parameters epsilon=1e-2; % tolerance for convergence imax=1000; % maximum number of iterations allowed k=1; % initial index value for iteration kp=[1:imax]; for anim=1:20; errmax=zeros(imax,1); u=ones(n,m); k=1; % Initialization at boundary points % bottom u(i,1)=10; ; % top u(i,m)=15+0.5*anim; ; % left 8
u(1,j)=30-0.5*anim; ; % right u(n,j)=3; ; % Guess solution-> average of boundary conditions guess=mean([mean(u(2:n-1,1)),mean(u(1,2:m-1)), mean(u(n,:)),mean(u(2:n-1,m))]); % Initialize all internal points to the average value u(i,j)=u(i,j)*guess; uiter= u; % uiter = new iteration for solution error=uiter-u; %%% Gauss-Seidel iteration scheme while(k<imax) uiter(i,j) = 0.25*(u(i-1,j)+u(i+1,j)+u(i,j-1)+u(i,j+1)); error(i,j) = abs(uiter(i,j)-u(i,j)); u=uiter; k=k+1; errmax(k)=max(max(error)); if (errmax(k)<epsilon) %disp(fprintf('convergence achieved after k iterations=',k)); break; % of error test % of while h=pcolor(x,y,u'); 9
shading interp; set(h,'edgecolor', 'None') xlabel('x') ylabel('y') title(['temperature for animation: ', num2str(anim)]) caxis([0 30]); colorbar; pause(0.05) ; % of animation 10