You are on page 1of 4

Bài 3:

Nhóm:
TT Họ và tên MSSV Lớp Ghi chú
1 Trần Lê Kim Ngân 21207183 21DVT_CLC3

Bài 1

Bài 2

function [c,n] = chiadoi(fx, a, b, saiso)


fx = str2func(['@(x)', fx]);
n = 0;
while(1)
c = (a+b)/2;
if(fx(a)*fx(c)<0)
b = c;
else
a = c;
end
n = n + 1;
e = abs(b - a);
if(e < saiso)
break;
end
end
end
function [x1,n] = lap(fx, fp, a, b, saiso)
fx = str2func(['@(x)', fx]);
fp = str2func(['@(x)', fp]);
n = 0;
c = (a+b)/2;
if(fx(a)*fx(c) < 0)
x0 = a;
else
x0 = b;
end
while(1)
x1 = fp(x0);
n = n + 1;
e = abs(x1 - x0);
if(e < saiso)
break;
end
x0 = x1;
end
end

function [y, n] = newton(fx, a, b, saiso)


fxi = str2func(['@(x)', fx]);
x0 = (a + b)/2;
n = 0;
while (1)
syms x;
df_dx = diff(fxi, x);
df = matlabFunction(df_dx);
z = fxi(x0) / df(x0);
y = x0 - z;
if abs(y - x0) < saiso
break;
else x0 = y;
n = n + 1;
end
end
end

fx = 'x - sin(x) - 0.25';


fp = 'sin(x) + 0.25';
saiso = 0.005;
a = -1;
b = 2;
[nghiem, solanlap] = chiadoi(fx, a, b, saiso);
disp(['Nghiem Chiadoi = ', num2str(nghiem)]);
disp(['So lan lap Chiadoi = ', num2str(solanlap)]);
[nghiem, solanlap] = lap(fx, fp, a, b, saiso);
disp(['Nghiem Lap = ', num2str(nghiem)]);
disp(['So lan lap Lap = ', num2str(solanlap)]);

fx = 'x - sin(x) - 0.25';


fp = 'sin(x) + 0.25';
saiso = 0.005;
a = 1;
b = 2;
[nghiem, solanlap] = chiadoi(fx, a, b, saiso);
disp(['Nghiem Chiadoi = ', num2str(nghiem)]);
disp(['So lan lap Chiadoi = ', num2str(solanlap)]);
[nghiem, solanlap] = lap(fx, fp, a, b, saiso);
disp(['Nghiem Lap = ', num2str(nghiem)]);
disp(['So lan lap Lap = ', num2str(solanlap)]);

fx = 'x^2 - sin(pi)*x';
saiso = 5*10^(-5);
a1 = -0.5;
b1 = 0.5;
a2 = -0.05;
b2 = 0.5;
[nghiem, solanlap] = chiadoi(fx, a1, b1, saiso);
disp(['Nghiem1 Chiadoi = ', num2str(nghiem)]);
disp(['So lan lap1 Chiadoi = ', num2str(solanlap)]);
[nghiem, solanlap] = chiadoi(fx, a2, b2, saiso);
disp(['Nghiem2 Chiadoi = ', num2str(nghiem)]);
disp(['So lan lap2 Chiadoi = ', num2str(solanlap)]);
[nghiem, solanlap] = lap(fx, fp, a1, b1, saiso);
disp(['Nghiem1 Lap = ', num2str(nghiem)]);
disp(['So lan lap1 Lap = ', num2str(solanlap)]);
[nghiem, solanlap] = chiadoi(fx, a2, b2, saiso);
disp(['Nghiem2 Lap = ', num2str(nghiem)]);
disp(['So lan lap2 Lap = ', num2str(solanlap)]);

You might also like