Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Câu 1:

 Trường hợp f(x) = Af1(x)+ Bf2(x):

 Trường hợp f(x) = Af1(x)+ Bf2(x)+Cf3(x):

Câu 2
 Đạo hàm

syms x
f = input('Nhap ham so f(x): ');
f = sym(f);

% nhap các diem can tinh


x0 = input('Nhap cac diem x0:');
% do chinh xac
h = 0.00001;
df = zeros(1, length(x0));
for i=1:length(x0)
f_x0 = subs(f,x,x0(i));
f_x1 = subs(f,x,x0(i) + h);
df(i) = (f_x1 - f_x0)/h;
end

fprintf('ket qua:');
disp(df);
 Tích phân hình thang

clear all
clc
syms x;
f = input('Nhap ham f(x):');
a = input ('Nhap gioi han duoi: ');
b = input ('Nhap gioi han tren: ');
n = input (' Nhap phan doan: ');

h= (b -a)/n;
x_points =a:h:b; %t?o m?ng ?i?m ??u a k?t thúc b kho?ng
cách h
I= (h/2)*(subs(f,x,a)+subs(f,x,b)+
2*sum(subs(f,x,x_points(2:1:end-1)))); % Công th?c hình
thang
fprintf('Ketqua:%9f\n', I)
 Tích phân simson

syms x;
f = input('Nhap ham f(x):');
a = input ('Nhap gioi han duoi: ');
b = input ('Nhap gioi han tren: ');
n = input ('Nhap phan doan: ');

h= (b -a)/n;
x_points =a:h:b; %t?o m?ng ?i?m ??u a k?t thúc b kho?ng cách
h
% Công th?c Simson
I=(h/3)*(subs(f, x, a)+ subs(f, x, b)
+4*sum(subs(f,x,x_points(2:2:end-1)))
+2*sum(subs(f,x,x_points(3:2:end-2))));
fprintf('Ketqua:%f\n', I)
Câu 3

clc;
clear all;
n = 10; %thay
a = 0; %thay
b = 1; %thay
delta = (b-a)/n;
x = 0;
y = 0.5;
y_euler = y;
y_heun = y;
y_RK4 = y;

for i = 1:n
%Phuong phap Euler
y_euler = y_euler + delta*(y_euler - x^2 + 1);
%Phuong phap Heun
y_pre = y_heun + delta*(y_heun-x^2+1);
y_heun = y_heun + (delta/2)*((y_heun-x^2+1) + (y_pre -
(x + delta)^2 + 1));
%Phuong phap RK4
K1 = y_RK4 - x^2 + 1;
K2 = (y_RK4 + 0.5 * delta * K1) - (x + 0.5 * delta)^2 +
1;
K3 = (y_RK4 + 0.5 * delta * K2) - (x + 0.5 * delta)^2 +
1;
K4 = (y_RK4 + delta * K3) - (x + delta)^2 + 1;
y_RK4 = y_RK4 + delta * ((K1 + K4) + 2 * (K2 + K3 ))/6;
%Nghiem gan dung Euler
E(i,:) = y_euler;
%Nghiem gan dung Heun
H(i,:) = y_heun;
%Nghiem gan dung RK4
RK4(i,:) = y_RK4;
x = x+delta;
f = (x+1)^2 - 0.5*exp(x);
F(i,:) = f; %Nghiem chinh xac
X(i,:) = x;
end

%Tinh sai so, Do chinh xac


saiso_Euler = norm(E-F)/norm(F) %sai so Euler
dochinhxac_Euler = 1 - saiso_Euler %Do chinh xac Euler
saiso_Heun = norm(H-F)/norm(F) %sai so Heun
dochinhxac_Heun = 1 - saiso_Heun %Do chinh xac Heun
saiso_RK4 = norm(RK4-F)/norm(F) %sai so RK4
dochinhxac_RK4 = 1 - saiso_RK4 %Do chinh xac RK4

%Ve do thi
plot(X,E,'r-x','Displayname','Euler')
hold on
plot(X,H,'g-*','Displayname','Heun')
hold on
plot(X,RK4,'k--','Displayname','Runge Kutta 4')
hold on
plot(X,F,'b-','Displayname','Nghiem chinh xac')

You might also like