ĐỀ PHƯƠNG PHÁP TÍNH THẦY LỘC
CODE BÀI TẬP LỚN PHƯƠNG PHÁP TÍNH THẦY LỘC
CÂU 1:
function Cau1
clc;
format short
syms x
mn = input('Nhap 2 so cuoi MSSV: ');
M = (mn+12)/10;
%%%%%%%%%%%%%%
f = exp(x)+2*x^2+sin(x)/M-10;
a = 1;
b = 2;
if double(subs(f,x,a)*subs(diff(diff(f,x),x),x,a))>0
X = a;
else
X = b;
end
if double(subs(diff(f,x),x,a))<double(subs(diff(f,x),x,b));
min = subs(diff(f,x),x,a);
else
min = subs(diff(f,x),x,b);
end
for i = 1:2 %chay nghiem tu x1 -> x2
X = X - subs(f,x,X)/subs(diff(f,x),x,X);
Denta = ceil((abs(subs(f,x,X))*10^4)/min)/10^4;
i = i+1;
end
x2 = double(X), SaiSo = double(Denta),
end
CÂU 2:
function Cau2
clc
format short
syms x
mn = input('Nhap 2 so cuoi MSSV: ');
M = (mn+12)/10;
%%%%%%%%%%%%%%%%%%%%
A=[6+M 2 -3 4 5; 4 7+M 4 -2 -6; 3 -3 8+M -2 -5; 2 -3 4 9+M -3; 5 -3 4 -2 10+M];
B=[9;8;7;6;5];
[L , U]= lu(A);
X= inv(A)*B;
l43 = L(4,3);
u55 = U(5,5);
x5 = X(5,1);
a = 'Phan tu L43 la:';
disp(a);
disp (l43);
b = 'Phan tu U55 la:'; disp(b); disp(u55);
c = 'Gia tri x5 la:';
disp(c); disp (x5)
end
CÂU 3:
function Jacobi
clc;
mn = input('Nhap 2 so cuoi MSSV: ');
M = (mn + 12)/10;
%%%%%%%%%%%%%%%%%%%%%%%%
a = [12+M 2 -3 4 5; 4 13+M 4 -2 -6; 3 -3 14+M 2 -5; 2 -2 4 15+M -3; 5 -4 5 -3 16+M];
b = [9; 8; 7; 6; 5];
x = [1.5; 0.3; 3.4; 1.4; 5.6];
i=0;A=x(1);B=x(2);C=x(3);D=x(4);E=x(5);
while i<3 %tim nghiem x3
X=(b(1)-a(1,2)*B-a(1,3)*C-a(1,4)*D-a(1,5)*E)/a(1,1);
Y=(b(2)-a(2,1)*A-a(2,3)*C-a(2,4)*D-a(2,5)*E)/a(2,2);
Z=(b(3)-a(3,1)*A-a(3,2)*B-a(3,4)*D-a(3,5)*E)/a(3,3);
T=(b(4)-a(4,1)*A-a(4,2)*B-a(4,3)*C-a(4,5)*E)/a(4,4);
K=(b(5)-a(5,1)*A-a(5,2)*B-a(5,3)*C-a(5,4)*D)/a(5,5);
A = X; B = Y; C = Z; D = T; E = K;
i=i+1;
end
x1 = A, x2 = B, x3 = C, x4 = D, x5 = E,
end
CÂU 4:
function GaussSeidel
clc;
mn = input('Nhap 2 so cuoi MSSV: ');
M = (mn + 12)/10;
a = [12+M 2 -3 4 5; 4 13+M 4 -2 -6; 3 -3 14+M 2 -5; 2 -2 4 15+M -3; 5 -4 5 -3 17+M];
b = [9; 8; 7; 6; 4];
x = [0.1; 0.3; 0.4; 0.5; 0.9];
i=0;A=x(1);B=x(2);C=x(3);D=x(4);E=x(5);
while i<3 %tim nghiem x3
A=(b(1)-a(1,2)*B-a(1,3)*C-a(1,4)*D-a(1,5)*E)/a(1,1);
B=(b(2)-a(2,1)*A-a(2,3)*C-a(2,4)*D-a(2,5)*E)/a(2,2);
C=(b(3)-a(3,1)*A-a(3,2)*B-a(3,4)*D-a(3,5)*E)/a(3,3);
D=(b(4)-a(4,1)*A-a(4,2)*B-a(4,3)*C-a(4,5)*E)/a(4,4);
E=(b(5)-a(5,1)*A-a(5,2)*B-a(5,3)*C-a(5,4)*D)/a(5,5);
i=i+1;
end
x1 = A, x2 = B, x3 = C, x4 = D, x5 = E,
end
CÂU 5:
function Cau5
clc;
format short
mn = input('Nhap 2 so cuoi MSSV: ');
M = (mn+12)/10;
%%%%%%%%%%%%%%%
syms x
X = [1.3 1.7 2.3 2.7 2.9 3.1];
Y = [1.2 8.6 2.3 2.5 2*M 6.6];
n = size(X,2); h=[]; b=[];d=[];
A = zeros(n);
B = zeros(n,1);
A(1,1)=1; A(n,n)=1;
for i=1:n-1
h(i) = X(i+1)-X(i);
end
for i=2:n-1
A(i,i)=2*(h(i-1)+h(i));
A(i,i-1)=h(i-1);
A(i,i+1)=h(i);
B(i,1)=3*(Y(i+1)-Y(i))/h(i)-3*(Y(i)-Y(i-1))/h(i-1);
end
c = inv(A)*B;
for i=1:n-1
b(i)=(Y(i+1)-Y(i))/h(i)-h(i)*(c(i+1)+2*c(i))/3;
d(i)=(c(i+1)-c(i))/(3*h(i));
end
t = 1.4;
I = 0;
for i =1:n-1
if t>= X(i) && t<X(i+1)
I = Y(i) + b(i)*(t-X(i)) + c(i)*(t-X(i))^2 + d(i)*(t-X(i))^3;
end
end
fprintf('Xap xi gia tri cua ham tai x = %.1f',t);
disp(I);
t = 2.5;
I = 0;
for i =1:n-1
if t>= X(i) && t<X(i+1)
I = Y(i) + b(i)*(t-X(i)) + c(i)*(t-X(i))^2 + d(i)*(t-X(i))^3;
end
end
fprintf('Xap xi gia tri cua ham tai x = %.1f',t);
disp(I);
end
CÂU 6:
function Cau6
clc;
format short ;
syms x m real
mn = input('Nhap 2 so cuoi MSSV: ');
M = (mn+12)/10;
X = [1.3 1.7 2.3 2.7 2.9 3.1];
Y = [1.2 8.6 2.3 2.5 3*M 6.6];
n = size(X,2);
g1 = 0.2;
gn = 0.5;
for i = 1:n-1
H(i) = X(i+1) - X(i);
end
A(1) = 2*H(1);
A(n) = 2*H(n-1);
for j = 2:n-1
A(j) = 2*(H(j-1) + H(j));
end
B(1) = 3*(Y(2)-Y(1))/H(1)-3*g1;
B(n) = 3*gn - 3*(Y(n)-Y(n-1))/H(n-1);
for k = 2:(n-1)
B(k) = 3*(Y(k+1)-Y(k))/H(k) - 3*(Y(k)-Y(k-1))/H(k-1);
end
D = diag(A,0); E = diag(H,1); F = diag(H,-1);
C = inv(E+F+D)*(B');
for l = 1:(n-1)
d(l) = (C(l+1)-C(l))/(3*H(l));
b(l) = (Y(l+1)- Y(l))/H(l) - H(l)*(C(l+1)+2*C(l))/3;
end
t = 1.4;
I = 0;
for i =1:n-1
if t>= X(i) && t<X(i+1)
I = Y(i) + b(i)*(t-X(i)) + C(i)*(t-X(i))^2 + d(i)*(t-X(i))^3;
end
end
fprintf('Xap xi gia tri cua ham tai x = %.1f',t);
disp(I);
t = 3.0;
I = 0;
for i =1:n-1
if t>= X(i) && t<X(i+1)
I = Y(i) + b(i)*(t-X(i)) + C(i)*(t-X(i))^2 + d(i)*(t-X(i))^3;
end
end
fprintf('Xap xi gia tri cua ham tai x = %.1f',t);
disp(I);
end
CÂU 7:
function Cau7
clc;
format short
syms x m
M = input('Nhap M: ');
%%%%%%%%%%%%%%%%%%%%
u = [1.2 1.3 1.4 1.5 1.7];
y = [2*M 2.5 5 4.5 5.5];
gx = sqrt(x.^2+1);
hx = cos(x);
A = zeros(2); B = zeros(2,1);
for i = 1:5
A(1,1)=A(1,1)+subs(gx.^2,u(i)) ;
A(2,2)=A(2,2)+subs(hx.^2,u(i));
A(1,2)=A(1,2)+subs(hx.*gx,u(i));
B(1,1)=B(1,1)+y(i)*subs(gx,u(i));
B(2,1)=B(2,1)+y(i)*subs(hx,u(i));
end
A(2,1) = A(1,2);
C = inv(A)*B;
A = C(1), B = C(2),
end
CÂU 8:
function Cau8
clc;
mn = input('Nhap 2 so cuoi MSSV: ');
M = (mn+12)/10;
syms x
%%%%%%%%%%%%%%%%%
p = [0.1 0.3 0.6 0.9 1.1 1.4];
q = [3*M 0.6 1.5 3.7 3.2 4.3];
x1 = 1; x2 = 1; x3 = 1; x4 =1; x5 = 1;
for i = 1:(size(p,2)-1)
a(i) = (q(i+1)-q(i))/(p(i+1)-p(i));
x1 = x1*(x-p(i));
end
for i = 1:(size(a,2)-1)
b(i) = (a(i+1)-a(i))/(p(i+2)-p(i));
x2 = x2*(x-p(i));
end
for i = 1:(size(b,2)-1)
c(i) = (b(i+1)-b(i))/(p(i+3)-p(i));
x3 = x3*(x-p(i));
end
for i = 1:(size(c,2)-1)
d(i) = (c(i+1)-c(i))/(p(i+4)-p(i));
x4 = x4*(x-p(i));
end
for i = 1:(size(d,2)-1)
e(i) = (d(i+1)-d(i))/(p(i+5)-p(i));
x5 = x5*(x-p(i));
end
A = subs(diff((q(1)+a(1)*x5+b(1)*x4+c(1)*x3+d(1)*x2+e(1)*x1),x),x,0.5);
disp('gia tri xap xi dao ham cap 1 cua ham tai x=0.5 la:');disp(double(A));
end
CÂU 9:
function Cau9
clc;
syms x y;
mn = input('Nhap 2 so cuoi MSSV: ');
M = (mn+12)/10;
%%%%%%%%%%%%%%%%
a= 2;
b = 62;
n =120;
h= (b-a)/n;
m=n/2;
f= (2*M*x^2+x+1)/(7*x^4+x+6);
y= subs(f,x,a)+subs(f,x,b)+4*subs(f,x,a+h);
for i = 1 :m-1
y= y+ 2*subs(f,x,a+2*i*h)+4*subs(f,x,a+(2*i+1)*h);
end
y=double(y*h/3);
disp(y);
end
CÂU 10:
function Cau10
format short;
clear all;
clc;
syms x u y;
mn = input('Nhap 2 so cuoi MSSV: ');
M = (mn+12)/10;
h = 0.2;
x1 = 1;
y1 = 2.4;
xi = 2.2;
f = 2*M.*x+x.*sin(x+2.*y);
K1 = 0; K2 = 0; K3 = 0; K4 = 0;
u = [];u(1) = x1;
k = (xi-x1)/h;
for i=1:k
u(i+1)= u(i) + h;
end
y = []; y(1) = y1;
for i=1:round(k)
K1=h*subs(subs(f,u(i)),y(i));
K2=h*subs(subs(f,u(i)+h/2),y(i)+K1/2);
K3=h*subs(subs(f,u(i)+h/2),y(i)+K2/2);
K4=h*subs(subs(f,u(i)+h),y(i)+K3);
y(i+1)=y(i)+(K1+2*K2+2*K3+K4)/6;
end
n = round(k+1);
disp('Xap xi: '), y(n),
end
CÂU 11:
function Cau11
clc;
format short
syms x real
mn = input('Nhap 2 so cuoi MSSV: ');
M = (mn+12)/10;
a = 0; b = 1;
h = 0.1; ya = 1; yb = 1.2;
n = (b-a)/h;
px = x + 2*M;
qx = x.^3;
rx = -30;
fx = -x.*(x+1);
for i = 1:n
x(i) = a + i*h;
end
n = round(n);
A = zeros(n-1);
B = zeros(n-1,1);
A(1,1) = subs(rx,x(1)) - 2*subs(px,(x(1)))/(h^2);
A(1,2) = subs(px,x(1))/(h^2) + subs(qx,x(1))/(2*h);
A(n-1,n-1) = subs(rx,x(n-1)) - 2*subs(px,x(n-1))/(h^2);
A(n-1,n-2) = subs(px,x(n-1))/(h^2) - subs(qx,x(n-1))/(2*h);
B(1,1) = subs(fx,x(1)) - subs((px/(h^2)-qx/(2*h)),x(1))*ya;
B(n-1,1) = subs(fx,x(n-1))-subs((px/(h^2)+qx/(2*h)),x(n-1))*yb;
for i=2:n-2
A(i,i) = subs(rx,x(i)) - 2*subs(px,(x(i)))/(h^2);
A(i,i-1) = subs(px,x(i))/(h^2) - subs(qx,x(i))/(2*h);
A(i,i+1) = subs(px,x(i))/(h^2) + subs(qx,x(i))/(2*h);
B(i,1) = subs(fx,x(i));
end
C = inv(A)*B;
Xap = 'Xap xi gia tri cua ham y(0.1) = ';disp(Xap);disp(C(1));
Xap = 'Xap xi gia tri cua ham y(0.5) = ';disp(Xap);disp(C(5));
Xap = 'Xap xi gia tri cua ham y(0.9) = ';disp(Xap);disp(C(9));
end