(phương pháp chia đôi) F=(x) (x3)14xx+59x70; xl=1.8; yl=F(xl); xr=2.1; yr=F(xr); while (xrxl)>eps xm=(xl+xr)2; ym=F(xm); if sign(yl)~=sign(ym) xr=xm; else xl=xm; end end xm Incremental Search method (Ppdaycung) F=(x) (x3)14xx+59x70; x2=input(x2: ); delta=0.3; y2=F(x2); y1=y2; while y2y1>0 x1=x2; x2=x1+delta; y2=F(x2); end y3=1+eps; eps=1E6; while abs(y3)>eps x3=(x1y2x2y1)(y2y1); y3=F(x3); if sign(y3)~=sign(y1) x2=x3; y2=y3; else x1=x3; y1=y3; end end x3 Secant method (PPdaycung2) F=(x) (x3)14xx+59x70; x2=input(x2: ); delta=0.3; y2=F(x2); y1=y2; while y2y1>0 x1=x2; x2=x1+delta; y2=F(x2); end x3=x2; y3=y2; x2=x1; y2=y1; eps=1E6; while abs(x3x2)>eps x1=x2; y1=y2; x2=x3; y2=y3; x3=(x1y2x2y1)(y2y1); y3=F(x3); end x3
Trang 1Chưng cất
Pmt=input('Pmt: ');
a1=input('a1: ');
a2=input('a2: ');
b1=input('b1: ');
b2=input('b2: ');
N=input('So mam N: ');
x=input('phan mol x: ');
T1=a1/(log(Pmt)-b1);
T2=a1/(log(Pmt)-b2);
if T1>T2
tmp=a1;a1=a2;a2=tmp;
tmp=b1;b1=b2;b2=tmp;
tmp=T1;T1=T2;T2=tmp;
end
eps=1E-10;
F = @(x,T) x*exp(a1/T1+b1)+(1-x)*exp(a2/T1+b2)-Pmt; fprintf('man\t long\t hoi\t NDsoi\n')
for i=1:N
A=T1;
B=T2;
while (B-A)>eps
mid=(A+B)/2;
if F(x,mid)==0;
B=mid;
A=B;
Trang 2else
if sign(F(x,A))==sign(F(x,mid))
A=mid;
else B=mid;
end
end
end
T=(A+B)/2;
y=x*exp(a1/T+b1)/Pmt;
fprintf('%6.0f\t %6.4f\t %6.4f\t %6.3f\n',i,x,y,T-273); x=y;
end
Switch, case anh otherwise
input_num=input('nhap so: ');
switch input_num
case -1
input_str='minus one';
case 0
input_str='zero';
case 1
input_str='plu one';
case {-10,10}
input_str='+/-ten';
otherwise
input_str='other value';
Trang 3display(input_str)
The while loop
i=1;j=10;
while i<=N
j=1;
while j<=N
A(i,j)=1/(i+j-1);
j=j+1;
end
i=i+1;
end
Tích phân theo MonteCarlo
N=input('N: ');
a=input('a: ');
b=input('b: ');
sum=0;
for i=1:N
x=a+(b-a)*rand;
y=x*x;
sum=sum+y;
end
h=sum/N;
TP=h*(b-a);
fprintf('TP= %0.2f',TP)
Trang 4Tích phân theo MonteCarlo 3D
N=input('N: ');
xL=input('xL: ');
xU=input('xU: ');
yL=input('yL: ');
yU=input('yU: ');
zL=input('zL: ');
zU=input('zU: ');
sum=0;
V=(xU-xL)*(yU-yL)*(zU-zL);
for i=1:N
x=xL+rand*(xU-xL);
y=yL+rand*(yU-yL);
z=zL+rand*(zU-zL);
F=x*x+y*y+z*z;
sum=V+F;
TP=sum*V/i
fprintf('TP= %0.2f',TP)
end
Vận tốc khí theo nhiệt độ
T1=input('gt T1: ');
T2=input('gt T2: ');
BN=input('gt buoc nhay BN: ');
M1=input('gt M1: ');
M2=input('gt M2: ');
Trang 5fprintf('bang gia tri VTTB u1 va u2 theo T:\n');
for T=T1:BN:T2
u1=sqrt((3*R*T)/M1);
u2=sqrt((3*R*T)/M2);
fprintf('T= %d\t, u1= %0.2f\t, u2= %0.2f\n',T,u1,u2);
end
Vận tốc trung bình của phân tử khí theo nhiệt độ
T=input('temperature is K: ');
Po=input('outside pressure is atm: ');
Pi=input('inside pressure is atm: ');
M=input('Molecule weight: ');
K=input('Cp/Cv: ');
% R=8.314J/(K.mol)
% 1J=1E7 erg
% 1 erg=1cm^2*g/(s^2)
R=8.314*1E7; %change R
K9=(K-1)/K;
P9=Po/Pi;
R9=R*T/M;
u=sqrt(2/K9*R9*(1-P9^K9))/100;
fprintf('vt : %g.\n',u)
Euler, RK4
F=@(x,t) (10-x)/10;
Trang 6time=60;
h=time/N;
t=linspace(0,time,N+1);
CA_Euler=zeros(0,N+1);
CA_Euler(i)=1;
hold on;
for i=1:N
CA_Euler(i+1)=CA_Euler(i)+h*F(CA_Euler(i),t(i));
end
plot(t,CA_Euler,'red')
CA_E_improved=zeros(0,N+1);
CA_E_improved(i)=1;
for i=1:N
k1=h*F(CA_E_improved(i),t(i));
k2=h*F(CA_E_improved(i)+k1,t(i)+h)
CA_E_improved(i+1)=CA_E_improved(i)+(k1+k2)/2;
end
plot(t,CA_E_improved,'blue')
CA_E_modified=zeros(0,N+1);
CA_E_modified(i)=1;
for i=1:N
k1=h*F(CA_E_modified(i),t(i));
CA_E_modified(i+1)=CA_E_modified(i)+h*F(CA_E_modified(i)+k1/2,t(i)+h/2);
Trang 7plot(t,CA_E_modified,'y')
RK4=zeros(0,N+1);
RK4(i)=1;
for i=1:N
k1=h*F(RK4(i),t(i));
k2=h*F(RK4(i)+k1/2,t(i)+h/2);
k3=h*F(RK4(i)+k2/2,t(i)+h/2);
k4=h*F(RK4(i)+k3,t(i)+h);
RK4(i+1)=RK4(i)+(k1+2*k2+2*k3+k4)/6;
end
plot(t,RK4,'black')
CA=10-9*exp(-t/10);
plot(t,CA,'green')
Hệ số truyền nhiệt
x=input('x: ');
t=input('t: ');
L=input('L: ');
eps=input('eps: ');
k=100;
T=10+10*x;
i=1;
flag=true;
fprintf('step\t term\t\t sum\n');
Trang 8while flag
an=(30/(i*pi))*(-2)*exp((-2*i^2*pi^2*t)/(k*L^2));
an=an*sin(i*pi*x/L);
T=T+an;
fprintf('%2.0f\t %e\t %5.2f\n',i,an,T);
i=i+2;
if (abs(an)<eps)
flag=false;
end
end
The if, elseif and else statements
I=input('nhap gia tri I=');
J=input('nhap gia tri J=');
if I==J
A(I,J)=2;
elseif abs(I-J)==1
A(I,J)=-1;
else
A(I,J)=0;
end
Bài thi giữa kì
clc;
clear all;
D=input('Nhap duong kinh ong D(m): ');
u=input('Nhap do nhot u(kg/m.s): ');
Trang 9p=input('Nhap khoi luong rieng p(kg/m3): ');
v=input('Nhap van toc trung binh dong v(m/s): ');
Re=p*v*D/u;
if Re<=2300
hf=16/Re;
fprintf('He so ma sat la: %5.2f.\nRe dong chay tang la: %5.2f.\n',hf,Re) else Re>2300;
hf=0.079*Re^-0.25;
fprintf('He so ma sat la: %5.2f.\nRe dong chay roi la: %5.2f.\n',hf,Re) end
Gỉai phương trình bậc 2
a=input('nhap gia tri a = ');
b=input('nhap gia tri b = ');
c=input('nhap gia tri c = ');
delta = b^2-4*a*c;
if delta>0
X1=(-b+sqrt(delta))/(2*a);
X2=(-b-sqrt(delta))/(2*a);
fprintf('nghiem cua gia tri X1 la %4.2f, X2 la %4.2f',X1,X1);
elseif delta<0
fprintf('phuong trinh vo nghiem');
else
X=-b/(2*a);
fprintf('nghiem cua phuong trinh la X %4.2f',X);
end
Trang 10Bisection method (phương pháp chia đôi)
F=@(x) (x^3)-14*x*x+59*x-70;
xl=1.8;
yl=F(xl);
xr=2.1;
yr=F(xr);
while (xr-xl)>eps
xm=(xl+xr)/2;
ym=F(xm);
if sign(yl)~=sign(ym)
xr=xm;
else
xl=xm;
end
end
xm
Incremental Search method (Ppdaycung)
F=@(x) (x^3)-14*x*x+59*x-70;
x2=input('x2: ');
delta=0.3;
y2=F(x2);
y1=y2;
while y2*y1>0
x1=x2;
Trang 11x2=x1+delta;
y2=F(x2);
end
y3=1+eps;
eps=1E-6;
while abs(y3)>eps
x3=(x1*y2-x2*y1)/(y2-y1);
y3=F(x3);
if sign(y3)~=sign(y1)
x2=x3;
y2=y3;
else
x1=x3;
y1=y3;
end
end
x3
Secant method (PPdaycung2)
F=@(x) (x^3)-14*x*x+59*x-70;
x2=input('x2: ');
delta=0.3;
y2=F(x2);
y1=y2;
while y2*y1>0
Trang 12x1=x2;
x2=x1+delta;
y2=F(x2);
end
x3=x2;
y3=y2;
x2=x1;
y2=y1;
eps=1E-6;
while abs(x3-x2)>eps
x1=x2;
y1=y2;
x2=x3;
y2=y3;
x3=(x1*y2-x2*y1)/(y2-y1);
y3=F(x3);
end
x3
Phương pháp dò tìm
FNF=@(x) (x^3)-14*x*x+59*x-70
delta=0.3;
x_new=0;
y_new=FNF(x_new);
y_old=y_new;
while y_new*y_old>0
Trang 13x_old=x_new;
x_new=x_new+delta;
y_new=FNF(x_new);
end
x_old
x_new
Fixed-point iteration method
Kw=10^-14;
Ka=input('Ka: ');
Ca=input('Ca: ');
F=@(x) Kw/x+Ka*Ca/(Ka+x);
x2=1E-7;
x1=x2+1;
eps=1E-6;
while abs(x1-x2)>eps
x1=x2;
x2=F(x1);
end
PH=-log10(x2);
fprintf('PH=%6.4f\n',PH)
Newton-Raphson method
F=@(x) (x^3)-14*x*x+59*x-70;
D=@(x) 3*(x^2)-28*x+59;
x2=input('x2: ');
x1=x2+1+eps;
Trang 14while abs(x1-x2)>eps
x1=x2;
x2=x1-(F(x1)/D(x2));
y2=D(x2);
end
x2
Quy luật phân bố vận tốc
T=input('temperature is K: ');
M=input('molecular weight: ');
U=input('molecular velocity: ');
% R=8.31441J/(K.mol)
% 1J=E7erg
% 1erg=1cm^2*g/(a^2)
R=8.314;
RH=8.314*1E7; %change R
UH=U*100; %change velocity to cm/s
dN=4*pi*UH*UH*(M/(2*pi*RH*T))^(3/2);
dN=dN*exp(-M*UH*UH/(2*RH*T));
dN=dN*100;
fprintf('partic: %g.\n',dN)
Gía trị trung bình và sai số chuẩn
x=[5 5.5 3 7.2 8.1 5.95 4.7 4 6.05];
N=length(x);
M=0;
Trang 15for i=1:N
M=N+x(i);
S=S+x(i)^2;
end
M=N/N;
S=S-N*M^2;
S=sqrt(S/(N-1));
fprintf('Mean value: %g.\n',N)
fprintf('Standard diviation: %g.\n',S)
Chuỗi Fourier
x=input('x: ');
eps=input('eps: ');
s=1;
i=1;
flag=true;
fprintf('step\t last term\t\t sum\n');
while flag
i=i+2;
an=((2/i*pi)^2)*(-2)*cos((i*pi*x)/2);
s=s+an;
fprintf('%2.0f\t %e\t %5.2f\n',i,an,s);
if (abs(an)<eps)
Trang 16flag=false;
end
end
Chuỗi hình học
an=input('an: ');
q=input('q: ');
eps=input('error; ');
i=0;s=an; %first term first sum
flag=true;
fprintf('step\t last term \t\t sum\n');
while flag
an=an*q;
s=s+an;
fprintf('%2.0f\t %e\t %9.5f\n',i,an,s);
i=i+1;
if abs(an)<(eps*(1-q))
flag=false;
end
end
1
k1=0.2;
k2=0.1;
F=@(Ca,Cb,t) -k1*Ca;
G=@(Ca,Cb,t) k1*Ca-k2*Cb;
Trang 17time=50;
h=time/N;
Ca(1)=Ca0;
Cb(1)=0;
t=linspace(0,time,N+1);
Ca=zeros(0,N+1);
Ca(i)=1;
for i=1:N
k1a=h*F(Ca(i),Cb(i),t(i));
k1b=h*G(Ca(i),Cb(i),t(i));
k2a=h*F(Ca(i)+k1a/2,Cb(i)+k1b/2,t(i)+h/2); k2b=h*G(Ca(i)+k1b/2,Cb(i)+k1b/2,t(i)+h/2); k3a=h*F(Ca(i)+k2a/2,Cb(i)+k2b/2,t(i)+h/2); k3b=h*G(Ca(i)+k2a/2,Cb(i)+k2b/2,t(i)+h/2); k4a=h*F(Ca(i)+k3a,Cb(i)+k3b,t(i)+h);
k4b=h*G(Ca(i)+k3a,Cb(i)+k3b,t(i)+h);
Ca(i+1)=Ca(i)+(k1a+2*k2a+2*k3a+k4a)/6; Cb(i+1)=Cb(i)+(k1b+2*k2b+2*k3b+k4b)/6; end
CC=Ca0-(Ca+Cb);
plot(t,Ca);
hold on
plot(t,Cb);
Trang 182
clc;
clear all;
FNF=@(x,t) (10-x)/10;
N=10;
time=60;
h=time/N;
t=linspace(0,time,N+1);
ca_euler=zeros(0,N+1);
ca_euler(1)=1;
hold on;
for i=1:N
ca_euler(i+1)=ca_euler(i)+h*FNF(ca_euler(i),t(i)); end
plot(t,ca_euler,'b');
ca_improved=zeros(0,N+1);
ca_improved(1)=1;
for i=1:N
k1=h*FNF(ca_improved(i),t(i));
k2=k1+h*FNF(ca_improved(i),t(i)+h);
ca_improved(i+1)=ca_improved(i)+(k1+k2)/2;
Trang 19plot(t,ca_improved,'p');
ca_modified=zeros(0,N+1);
ca_modified(1)=1;
for i=1:N
k1=h*FNF(ca_modified(i),t(i));
ca_modified(i+1)=ca_modified(i)+h*FNF(ca_modified(i)+k1/2,t+h/2); end
plot(t,ca_modified,'g');
r_o=zeros(0,N+1);
r_o(1)=1;
for i=1:N;
k1=h*FNF(r_o(i),t(i));
k2=h*FNF(r_o(i)+k1/2,t(i)+h/2);
k3=h*FNF(r_o(i)+k2/2,t(i)+h/2);
k4=h*FNF(r_o(i)+k3,t(i)+h);
r_o(i+1)=r_o(i)+1/6*(k1+2*k2+2*k3+k4);
end
plot(t,r_o,'red');