%   chaleur04.m
%   fd, 5 decembre 2010, 06 mars 2025  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   clear ; format long

fprintf('chaleur04 \n') ; 
fprintf('la chaleur avec les differences finies \n') 
fprintf('cas ou  le schema explicite en temps est stable \n')


ndf  = 32  , nitdf  = 120  %% 20*4*4  
dxdf = 1/ndf ; 

xxdf = zeros(1,ndf+1) ; yydf = xxdf ;  y1df = xxdf ;  yex  = xxdf ; 
xa    = 0 ; 
for j = 1:ndf+1 ; xxdf(j) = xa ;
y1df(j) =  sin(pi*xa)  ; 
xa = xa + dxdf ; end 

hold off
plot (xxdf,y1df,'k;second membre;' ) 
axis ([ -.02 , 1.02 , -.11 , 1.05 ]) ;
title ('second membre') ;
print('04a-second-membre-sinusoidal' , '-djpg','-S891,630'); 
pause (.5)  
hold off

fprintf('on suppose mu=1 \n')
sigma = .49 %% .70 %% .49    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     boucle en temps 
 
dtdf  = sigma * dxdf**2                %  pas de temps         


uu = zeros (ndf-1,1) ; vv = uu ; 

yyt = zeros (ndf+1,1) ; 

for iter = 1:nitdf

for j=1:ndf-1 ;  yydf(j+1) = uu(j) ;   end 

%%%   jmaj = nitdf  ;  its10 = floor(iter/jmaj); 
%%%   if (jmaj*its10==iter)
%%%   plot ( xxdf,y1df,'r;inf;' ,   xxdf,yydf,'b*;t;' ) 
%%%   pause  (.5)
%%%   end 

j = 1 ;  
vv(j) = uu(j) + sigma * (- 2*uu(j) + uu(j+1)) + sigma * y1df(j+1) * (pi*dxdf)**2 ; 
for j=2:ndf-2 ;     
vv(j) = uu(j) + sigma * (uu(j-1) - 2*uu(j) + uu(j+1)) + sigma * y1df(j+1) * (pi*dxdf)**2 ; 
j = ndf-1 ;    
vv(j) = uu(j) + sigma * (uu(j-1) - 2*uu(j))  + sigma * y1df(j+1) * (pi*dxdf)**2 ;
end  


uu=vv ; 
iter ; 

for j=2:ndf ; yyt(j) = vv(j-1) ; end 
plot (xxdf,y1df,'k;;' , xxdf,yyt,'-*r;;')
axis ([ -.02 , 1.02 , -.11 , 1.05 ]) ;
title ('schema explicite') ;
delete (findobj ("tag", "legend")) 


pause  (.01)

end %%%  fin de la boucle en temps 
print('04b-schema-explicite-correct' , '-djpg','-S891,630'); 

pause  (.5)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%        solution exacte avec Fourier  

yex  = xxdf ; tt = nitdf * dtdf 

for j=1:ndf+1
yex(j)  =  sin(pi*xxdf(j)) * (1 - exp(-tt*pi**2)) ;    
end 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%        dernier graphe   

for j=1:ndf-1 ;  yydf(j+1) = uu(j) ;   end 
plot ( xxdf,y1df,'k;second membre;' , xxdf,yydf,'*-r;differences finies;' , ...
       xxdf,yex,'b;solution exacte;')
axis ([ -.02 , 1.02 , -.11 , 1.05 ]) ;
title ('schema explicite stable car sigma < 1/2') ;
print('04c-schema-explicite-correct' , '-djpg','-S891,630'); 

hold off 
