Sie sind auf Seite 1von 8

function soal_page149_1

%Inputing data needed for calculation

clc
clear
M=4;
kG=0.08;
R=2;
H=0.2;
De=0.04;
yud=0.002;
N=10;
time=100;
Kons=zeros(N,time);
Kons(:,1)=0.2; %konsentrasi cairan disemua posisi dimulai dari i sampai 1
delr=R./N;
Np=N-1;

for j=1:time
%Fullfiling the coefficient of tridiagonal matric
A(1)=0;
B(1)=(M+4);
C(1)=-4;
D(1)=M.*Kons(1,j);

for i=2:Np
A(i)=1-1./(2.*i); %alfa eq.
B(i)=-2-M; %beta eq.
C(i)=1+1./(2.*i);
D(i)=-M.*Kons(i,j);
end
A(N)=2.*(N-1./2);
B(N)=-M.*(N-1./4)-(2*(N-1/2)-2.*kG.*R.*H./De);
C(N)=0;
D(N)=-M.*(N-1./4).*Kons(N,j)-2.*kG.*R.*H.*yud./De;

%Eliminating A coefficient
for i=2:N
B(i)=B(i)-A(i)/B(i-1)*C(i-1);
D(i)=D(i)-A(i)/B(i-1)*D(i-1);
end
%backward substitution
Kons(N,j+1)=D(N)/B(N);
for i=Np:-1:1
Kons(i,j+1)=(D(i)-C(i).*Kons(i+1,j+1))./B(i);
end

end
Kons
%Program for solving page 131-134
%Book of "Permodelan Matematis....WBS AP and RS"
%Correlation between Temperature (T) and Heat Capacity (Cp)
function soal131
global koefCp n
T=[400 475 520 580 660 750 850];
Cp=[41.29 45.5 48.0 51.31 55.61 60.3 65.26];
TCp=[T' Cp']
T1=525; %the longer limit of temperature, K
T2=810; %the higher limit of temperature, K
koefCp=polyfit(T,Cp,n)
Cpcal=polyval(koefCp,T)
TCpCpcal=[T' Cp' Cpcal']
end

function y=fun_TCp(T)
global koefCp n
y=koefCp(n+1)
for i=1:n %i selalu dimulai dari 1
y=y+koefCp(n-i+1).*T.^(i); %jika n=3 keatas (banyak)agar penulisan lebih
mudah
end
%y=koefCp(4)+koefCp(3)*T+koefCp(2)*T.^2+koefCp(1)*T.^3
%y=a+b.*T+c.*T.^2+d.*T.^3
%Cpcal=y
end

%soal_page 135 chemical reaction problem

%equilibrium chemical reaction
%A <==> B + C
%Reaction take place plug flow reaction(pipe) with diameter and length of
%pipe D and L
%rA=k1.CA-k2.CB.CC
%rA=k(CA-CB.*CC./K)
%K=k1/k2
%with k=A.exp(-E/RT)
%K=exp(alfa+beta/T)
function soal_page135
clear
clc
global D P F0 R U Ts CpA CpB CpC CpI
global A ER alfa beta delHr0 Tref
U=0.0085;%cal/(cm.^2.det.K)
D=35; %gmol/det
P=7; %atm
CpA=20; %cal/gmol.K
CpB=10; %cal/gmol.K
CpC=15; %cal/gmol.K
CpI=10; %cal/gmol.K
delHr0=-35000; %cal/gmol
R=82; %cm^3.atm/gmol.K
A=10000; %det-1
F0=10;
Ts=421;
ER=6500; %K
alfa=-12.3;
beta=4400; %K
Tref=273;
z0=0; %tspan=[z0 zL]
zL=1000;
xA0=0; %initial[x0 T0]
T0=470;
[z xAT]=ode23(@fun_1,[z0 zL],[xA0 T0])
end

function dxATdz=fun_1(z,xAT)
global D P F0 R U Ts CpA CpB CpC CpI
global A ER alfa beta delHr0 Tref

x=xAT(1);
T=xAT(2);
k=A.*exp(-ER./T);
K=exp(alfa+(beta./T));
delHr=delHr0+(CpB+CpC-CpA).*(T-Tref);

dTdzbot=F0.*(0.9.*(1-x).*CpA+0.9.*x.*(CpB+CpC)+0.1.*CpI);
dTdz=dTdzup./dTdzbot; %5.67

end

PAK MUSLIHIN

MEETING 2
%this is main function
clc
x0=10; %initial value
%find the solution
x=fzero(@func_1,x0)

%this is main function

clc
x0=10; %initial value
%find the solution
x=fzero(@func_2,x0)

%this is main function

clc
x0=[0 1 0]; %initial value
%find the solution
%x=fzero(@func_1,x0)
%x=fzero(@func_2,x0)
option=optimset('display','iter');
x=fsolve(@func_3,x0,option)
[x,f,flag,out]=fsolve(@func_3,x0,option)

MEETING 3
x=0;
y=1;
[x y]=ode45(@func_4,[0 10],1)
%main function

clc
x=0
y=1
xspan=[0 10]
[x y]=ode45(@PD_func1,xspan,y)

clear
clc
xsp=[0 1];
p0=[1 .5 .75];
[x p]=ode45(@PD_func2,xsp,p0)
plot(x,p)

clear
clc
xsp=[0 1];
p0=[1 0.25];
[x p]=ode45(@PD_func3,xsp,p0)
plot(x,p)
legend('y','z')
xlabel('x')
ylabel('y,z')
title('y,z vs x')
grid
%kenapa digunakan ode45 ? karena tingkat ketelitiannya yang tinggi.
%semakin tinggi odenya, maka semakin tinggi tingkat ketelitiannya

MEETING 4
clear
clc
xspan=[0 4];
p0=[0.5 14.256]; %trial nilai z sampai diperoleh y=-4 --> [y z]
y=PDBVP_func(1,[1 1])
[x p]=ode45(@PDBVP_func,xspan,p0)
plot(x,p)

clc
global xspan yf y0
xspan=[0 4];
y0=0.5; yf=2; %initial value (pancingan)
z0=1498u ;
option=optimset('display','iter');
z0=fzero(@zero_func,z0,option);%jika PDnya banyak maka menggunakan fsolve
p0=[y0 z0];
[x y]=ode45(@PDBVP_func,xspan,p0);
plot(x,y)
legend('y','z')
xlabel('x')
ylabel('y,z')

f=fminsearch_fun1([1 1 1]) %untuk menguji apakah fungsi sudah benar atau

salam dengan
%memanggil fungsi di command window
x=fminsearch(@fminsearch_fun1,[0 0 0]) %minimal value
[x,ymin]=fminsearch(@fminsearch_fun,[100 100 100]) %initial value bebas
nilainya (x1,x2)
%global solution
option=optimset('display','iter')
[x,ymin]=fminsearch(@fminsearch_fun1,[100 100 100],option)

[x,ymin]=fminunc(@fminsearch_fun1,[100 100 100],option)

MEETING 5
data=[1 1.5;3 -9.5;5 -36.5;7 -79.5;9 -138.5]
x=1:2:9
y=[1.5 -9.5 -36.5 79.5 -138.5]
plot(x,y)
plot(x,y,'*')
x=x'
y=y'
%tools, basic fitting, choose linear or quadratic or polynomial
%Basic fitting
cftool
T=300:50:500
k=[.15 .25 .5 1.20 2.5]
%cftool untuk curve fitting, dilihat persamaan yang sesuai dengan eq.pada
%soal
%'help lsqcurvefit' diinput pada command window

T=[300 350 400 450 500]

k=[.15 .25 .5 1.2 2.5]
a=lsqcurvefit(@Arrhenius,[1000 1000],T,k)

option=optimset('display','iter')
a=lsqcurvefit(@Arrhenius,[1000 1000],T,k,[],[],option)
kcal=Arrhenius(a,T)
error=k-kcal
sse=sum(error.^2)

plot(T,k,'*',T,kcal)
disp('=========================================')
disp(' T kdata kcal error')
disp('=========================================')
data=[T' k' kcal' error']
disp('=========================================')

sse=sum(error.^2)

clc
x=0:.1:10;
y=fminbnd_fun(x);
plot(x,y)
x=-5:.1:10; %matrik yang dimulai dari -5 sampai 10 dengan interval 0.1
y=fminbnd_fun(x);
plot(x,y)
x=fminbnd(@fminbnd_fun,-5,10)
[x,ymin]=fminbnd(@fminbnd_fun,-5,10)
[x,ymin,io,out]=fminbnd(@fminbnd_fun,-5,10)
help optim
%1 fminbnd converged with a solution X based on OPTIONS.TolX.
%0 Maximum number of function evaluations or iterations reached.
%-1 Algorithm terminated by the output function.
%-2 Bounds are inconsistent (that is, ax > bx).

f=fminsearch_fun([1 1]) %untuk menguji apakah fungsi sudah benar atau salam
dengan
%memanggil fungsi di command window
x=fminsearch(@fminsearch_fun,[0 0]) %minimal value
[x,ymin]=fminsearch(@fminsearch_fun,[100 100]) %initial value bebas nilainya
(x1,x2)
%global solution
g=inline('x1.^2+2*x2^2-2*x1.*x2+x2-x1-5')
ezsurf(g)
option=optimset('display','iter')
[x,ymin]=fminsearch(@fminsearch_fun,[100 100],option)

[x,ymin]=fminunc(@fminsearch_fun,[100 100],option)

clc
f=fminsearch_fun1([1 1 1]) %untuk menguji apakah fungsi sudah benar atau
salam dengan
%memanggil fungsi di command window
x=fminsearch(@fminsearch_fun1,[0 0 0]) %minimal value
[x,ymin]=fminsearch(@fminsearch_fun1,[0 0 0]) %initial value bebas nilainya
(x1,x2)
%global solution
g=inline('x1.^2+2*x2^2+x3.^2-2*x1.*x2.*x3-2*x1.*x2+5')
ezsurf(g)
option=optimset('display','iter')
[x,ymin]=fminsearch(@fminsearch_fun1,[0 0 0],option)

[x,ymin]=fminunc(@fminsearch_fun1,[0 0 0],option)
MEETING 7
%Solution to PDE for a slab
clear
global T1 T2 beta N Tavg1 Tavg2
clc
T1=100;T2=50;Tinit=30;
L=2;N=7;dx=L/N;
alfa=0.005;beta=alfa/dx.^2;

Tin(1:N-2)=Tinit; %from1 to N-2

Tavg1=(Tinit+T1)./2;
Tavg2=(Tinit+T2)./2;

tfinal=40;
tspan=[0 tfinal];
[t T]=ode45(@PDE_func1,tspan,Tin)
plot(t,T)

%example of root finding problem

xold=10
xnew=fzero('pangkat3',xold)

%Program for solving problem 5 on page 224

%Main Program
%Inputing the initial guess for temperature (Tinitial)
global CA0 Fv VOL xin xout
global A E R
global delHR rho Cp
Tin=300;
CA0=5;
Fv=200;
VOL=8000;
xin=0;
xout=0.8;
A=3.12e8;
E=18600;
R=1.987;
delHR=-15;
rho=1.1;
Cp=0.8;

Tresult=fzero('integralnum1',Tin)

xopt=fminbnd('x.^2-4*x+8',-10,20)
%fminbnd untuk menghitung x optimum
%hasil 2,000
xopt=fminbnd('x.^2-4*x+8',-10,1)
%hasil 0,999 ini didapatkan karena golden section memungkinkan tempat yang
%kekurangan golden section fminbnd: (1)tidak mendapatkan nilai sesungguhnya
%(2)lambat(besarnya tidak toleransi yang ditetapkan
%Program untuk menyelesaikan Soal No.7 Hal.228
%Main Program minimasi dengan fminbnd dengan variabel suhu
%(1) Subrutin adalah integral numeris dengan batas integrasi konversi x
%(2) Subrutin kedua adalah persamaan reaksi direaktor
clc
global xout xfeed Tfeed

xout=0.64; %konversi keluar reaktor

xfeed=0; %konversi umpan segar (masuk reaktor)
Tfeed=660; %K
RF1=0.1 %Rasio antara resycledengan batas bawah x
RF2=12.5 %Rasio antara resycledengan batas atas x
RFTopt=fminbnd('berkatkat',RF1,RF2)