0% found this document useful (0 votes)
42 views

Transfer Panas Di Matlab: All All

This document contains code to model heat transfer and wave propagation problems in MATLAB. The heat transfer code models 2D heat diffusion using finite differences. It initializes a temperature field, applies boundary conditions, and iteratively solves the heat equation to model diffusion over time. The wave propagation code models a 2D wave equation to simulate wave motion. It initializes the wave function, applies boundary conditions, and uses a finite difference time domain method to iteratively solve the wave equation and model the wave propagation. The Schrodinger equation code numerically solves the 1D time-dependent Schrodinger equation using finite differences to model quantum mechanical wave functions over time. It initializes the wave function, applies boundary conditions, and

Uploaded by

Wari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Transfer Panas Di Matlab: All All

This document contains code to model heat transfer and wave propagation problems in MATLAB. The heat transfer code models 2D heat diffusion using finite differences. It initializes a temperature field, applies boundary conditions, and iteratively solves the heat equation to model diffusion over time. The wave propagation code models a 2D wave equation to simulate wave motion. It initializes the wave function, applies boundary conditions, and uses a finite difference time domain method to iteratively solve the wave equation and model the wave propagation. The Schrodinger equation code numerically solves the 1D time-dependent Schrodinger equation using finite differences to model quantum mechanical wave functions over time. It initializes the wave function, applies boundary conditions, and

Uploaded by

Wari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Transfer panas di matlab

%------------------------------------------------------------------------
%--- Heat Equation di 2D-------------------------------------
%--- Solves Ut=alpha*(Uxx+Uyy)-------------------------------------------
%------------------------------------------------------------------------

clc;
close all;
clear all;

%--dimensi...........................................................

N = 51;
DX=0.1; % ukuran langkah
DY=0.1;
Nx=5;
Ny=5;

X=0:DX:Nx;
Y=0:DY:Ny;

alpha=5;

%--batas kondisi----------------------------------------------------

U(1:N,1:N) = 0 ;

U(1,1:N) = 100;
U(N,1:N) = 0;
U(1:N,1) = 0;
U(1:N,N) = 0;

%--kondisi awal------------------------------------------------------

U(23:29,23:29)=1000; % tambalan panas di tengah

Umax=max(max(U));

%-------------------------------------------------------------------------

DT = DX^2/(2*alpha); % time step

M=2000; % jumlah maksimum dari iterasi yang diperbolehkan

%---skema beda hingga----------------------------------------------

fram=0;
Ncount=0;
loop=1;
while loop==1;
ERR=0;
U_old = U;
for i = 2:N-1
for j = 2:N-1
Residue=(DT*((U_old(i+1,j)-2*U_old(i,j)+U_old(i-1,j))/DX^2 ...
+ (U_old(i,j+1)-2*U_old(i,j)+U_old(i,j-1))/DY^2) ...
+ U_old(i,j))-U(i,j);
ERR=ERR+abs(Residue);
U(i,j)=U(i,j)+Residue;

end
end

if(ERR>=0.01*Umax) % batas kesalahan yang diizinkan adalah 1% dari suhu


maksimum
Ncount=Ncount+1;
if (mod(Ncount,50)==0) % menampilkan bingkai tampilan setiap 50
langkah waktu
fram=fram+1;
surf(U);
axis([1 N 1 N ])
h=gca;
get(h,'FontSize')
set(h,'FontSize',12)
colorbar('location','eastoutside','fontsize',12);
xlabel('X','fontSize',12);
ylabel('Y','fontSize',12);
title('Heat Diffusion','fontsize',12);
fh = figure(1);
set(fh, 'color', 'white');
F=getframe;
end

%--jika solusi tidak bertemu dalam 2000 langkah------------------------

if(Ncount>M)
loop=0;
disp(['solusi tidak mencapai kondisi mapan ',num2str(M),...
'time steps'])
end

%--jika solusi menyatu dalam 2000 langkah..........................

else
loop=0;
disp(['solusi mencapai kondisi mapan di ',num2str(Ncount) ,'time steps'])
end
end
%------------------------------------------------------------------------
%--menampilkan film difusi panas------------------------------------

movie(F,fram,1)

%------END---------------------------------------------------------------
Schrodinger di Matlab

% Numerical solution (FDM)


clc, clear
dx=0.01; % Spatial discretization
x=0:dx:1; % Spatial domain
C=0.01; % User-defined value until stability is reached
dt=C*dx^2; % Time discretization
t=0:dt:0.5; % Temporal domain
dpsi=zeros(1,length(x)); % Derivative of psi w.r.t. time
% Initial conditions - where t(1)=0
nx=input('Quantum number n = ');
Enx=0.5*(nx*pi)^2;
[Re2,Im2]=Psi(nx,Enx,t(1),x);
psi=Re2+Im2;
% Main solver based on FDM & RK3 method
axis([0 1 -2 2]);
xlabel('Length of 1D box');
hold on; grid on;
axis manual;
for i=1:length(t)
% B.C.s at ends are zero cause infinite potential
psi(1)=0; psi(length(x))=0; % Dirichlet boundary conditions

dpsi=dpsidt(psi,dx);
k1=dt*dpsi;

dpsi2=dpsidt(psi+k1/2,dx);
k2=dt*dpsi2;

dpsi3=dpsidt(psi+k2/2,dx);
k3=dt*dpsi3;

psi=psi+(1/6)*k1+(2/3)*k2+(1/6)*k3;

if max(abs(psi)) > 1.5 % Check for divergence


sprintf('Error!!! At i=%d',i)
return
end

% Simulation
if rem(i,10/dx)==0 || i==1
box2=plot(x,real(psi),'r');

% Plot of energy density


rho=real(psi).^2+imag(psi).^2;
e=plot(x,rho,'b');

legend('Wave function (\Psi)','Energy density (\rho)');


txt=sprintf('At t = %.3f',t(i));
time=text(0.02,1.85,txt);
drawnow;
if length(t)/1000-(i/1000)>1/1000
delete(box2); delete(time); delete(e);
end
end
end
function [dpsi]=dpsidt(psi,dx)
len=length(psi);
dpsi=zeros(1,len);
for j=2:len-1
dpsi(j)=0.5i*((psi(j+1)-2*psi(j)+psi(j-1))/dx^2);
end
% Boundary conditions (differential)
dpsi(1)=0.5i*((psi(3)-2*psi(2)+psi(1))/dx^2); % For left boundary
dpsi(len)=0.5i*((psi(len)-2*psi(len-1)+psi(len-2))/dx^2); % For right boundary
end
function [Re,Im]=Psi(nx,Enx,time,x) % Wave-function exact solver
[cosine, sine]=euler2polar(-Enx*time);
Re=sqrt(2)*cosine.*sin(nx*pi*x);
Im=sqrt(2)*sine.*sin(nx*pi*x);
end
% Convert Euler equation to polar
function [cosine, sine]=euler2polar(theta)
cosine=cos(theta);
if theta<0
sine=-1i*sin(abs(theta));
else
sine=1i*sin(abs(theta));
end
end

pemodelan gelombang 2D

clc; clear all; close all;


%2D WAVE EQUATION utt = c^2(uxx+uyy)
%with initial condition u(x,y,0) = sin(p*pi*x)*sin(q*pi*y), 0<x<1 0<y<1
% and boundary conditions u(0,y,t) = u(1,y,t)= u(x,0,t)= u(x,1,t) = 0 t>0
c = 1;
dx = 0.01;
dy = dx;
sigma = 1/sqrt(2); gamma = 1/sqrt(2); %Courant-Friedrich Stability Condition
dt = sigma*(dx/c);
t = 0:dt:1; x = 0:dx:1; y = 0:dy:1;
u = zeros(length(x),length(y),length(t));
p = 2; q = 1;
u(:,:,1) = transpose(sin(p.*pi.*x))*sin(q.*pi.*y); %u(x,y,0) = sin(p*pi*x)*sin(q*pi*y)
%u(x,y,dt)
for i=2:length(x)-1
for j=2:length(y)-1
u(i,j,2)= (sigma^2)*(u(i+1,j,1)-2*u(i,j,1)+u(i-1,j,1))...
+(gamma^2)*(u(i,j+1,1)-2*u(i,j,1)+u(i,j-1,1))+2*u(i,j,1) - u(i,j,1);
end
end
for n=2:length(t)-1
for i=2:length(x)-1
for j=2:length(y)-1
u(i,j,n+1)= (sigma^2)*(u(i+1,j,n)-2*u(i,j,n)+u(i-1,j,n))...
+(gamma^2)*(u(i,j+1,n)-2*u(i,j,n)+u(i,j-1,n)) + 2*u(i,j,n) - u(i,j,n-1);
end
end
end
%Analytic solution
SOL = zeros(length(x),length(y),length(t));
S = transpose(sin(p.*pi.*x))*sin(q.*pi.*y);
for i=1:length(t)
SOL(:,:,i) = S*cos(c.*pi.*(sqrt(p^2+q^2).*t(i)));
end
%Absolute error
E = abs(SOL-u);
[X,Y] = meshgrid(x,y);
max_e = max(max(max(E)));
for j=1:length(t)
subplot(2,1,1)
colormap(cool);
p1 = surf(X,Y,u(:,:,j));
title(sprintf('2D wave equation at t = %1.2f, con sigma = %1.2f y gamma =
%1.2f',t(j),sigma, gamma),'Fontsize',11);
xlabel('x','Fontsize',11); ylabel('y','Fontsize',11);
zlabel(sprintf('u(x,y,t = %1.2f)',t(j)),'Fontsize',11);
axis ([0 1 0 1 -1 1]);

subplot(2,1,2)
p2 = surf(X,Y,E(:,:,j));
title(sprintf('Absolute error at t = %1.2f',t(j)),'Fontsize',11);
xlabel('x','Fontsize',11); ylabel('y','Fontsize',11);
zlabel(sprintf('E(x,y,t = %1.2f)',t(j)),'Fontsize',11);
axis ([0 1 0 1 0 max_e]);
pause(0.001);
hold on;

if(j~=length(t))
delete(p1);
delete(p2);
end
end

You might also like