Professional Documents
Culture Documents
Cuoiki PPT
Cuoiki PPT
Câu 2
Đạo hàm
syms x
f = input('Nhap ham so f(x): ');
f = sym(f);
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
%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')