Đang tải... (xem toàn văn)
(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
Chư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; else 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'; end display(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) Tí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: '); R=8.314; fprintf('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; N=10; time=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); end plot(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'); while 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): '); p=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 Bisection 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; . BN: '); M1=input('gt M1: '); M2=input('gt M2: '); R=8.314; fprintf('bang gia tri VTTB u1 va u2 theo T: '); for T=T1:BN:T2 u1=sqrt((3*R*T)/M1); u2=sqrt((3*R*T)/M2); fprintf('T=