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

Vd1

%giai phuong trinh bac 2


a=input('nhap gia tri cua a=');
b=input('nhap gia tri cua b=');
c=input('nhap gia tri cua c=');
delta=b*b-4*a*c
if delta > 0
x1=(-b-sqrt(delta))/(2*a)
x2=(-b+sqrt(delta))/(2*a)
disp('phuong trinh co hai nghiem')
disp(x1)
disp(x2)
end

Vd2
%giai phuong trinh bac 2 voi a=3 b-6 c-1
a=input('nhap gia tri cua a=');
b=input('nhap gia tri cua b=');
c=input('nhap gia tri cua c=');
delta=b*b-4*a*c
x1=(-b-sqrt(delta))/(2*a);
x2=(-b+sqrt(delta))/(2*a);
if delta > 0
disp('phuong trinh co hai nghiem');
disp(x1);
disp(x2);
elseif delta==0
disp('phuong trinh co nghiem kep');
x=-b/a;
disp(x);
else
disp('phuong trinh vo nghiem')
end
Vd3
%giai phuong trinh bac 2
a=input('nhap gia tri cua a=');
b=input('nhap gia tri cua b=');
c=input('nhap gia tri cua c=');
if a==0
disp('phuong trinh bac nhat')
x=-c/b;
disp(x)
else
fprintf('phuong trinh bac hai\n')
delta=b*b-4*a*c
x1=(-b-sqrt(delta))/(2*a);
x2=(-b+sqrt(delta))/(2*a);
if delta > 0
disp('phuong trinh co hai nghiem');
disp(x1);
disp(x2);
elseif delta==0
disp('phuong trinh co nghiem kep');
x=-b/a;
disp(x);
else
disp('phuong trinh vo nghiem')
end
end
Vd4
a=menu('ham luong giac','sin','cos','tan')
if a==1
t=-pi:0.1:pi
plot(t,sin(t),'b')
xlabel('truc x')
ylabel('truc y')
grid on;
hold on;
elseif a==2
t=-pi:0.1:pi;
plot(t,cos(t),'r')
xlabel('truc x')
ylabel('truc y')
grid on;
hold on;
else
t=-pi:0.1:pi;
plot(t,tan(t),'y')
xlabel('truc x')
ylabel('truc y')
grid on;
end
Vd5
s=0
for i=1:1:100
s=s+i;
end
disp('tong so tu nhien lien tiep')
disp(s)

Vd6
s=0
n=input('nhap gia tri cua n=')
for i=1:1:n
s=s+i;
end
disp('tong so tu nhien lien tiep')
disp(s)

Vd7
s=0;

for i=1:2:21
s=s+i;
end
disp('tong so tu nhien lien tiep')
disp(s)

Vd8
G=1
n=input('nhap gia tri cua n=')
for i=1:1:n
G=G*i;
end
disp('tich giai thua so tu nhien lien tiep')
disp(G)

Vd9
s=0;i=0;
while i<=10
s=s+i;
i=i+1;
end
disp('tong cua so tu nhien la')
disp(s)
Vd10
n=input('xin moi nhap thang ma ban muon tim:','s');
switch n
case 'thang 2'
disp('thang 2 co 28 ngay')
case 'thang 4'
disp('co 30 ngay')
case 'thang 6'
disp('co 30 ngay')
case 'thang 9'
disp('co 30 ngay')
case 'thang 12'
disp('co 30 ngay')
otherwise
disp('co 31 ngay')
end

Vd11
n=input('xin moi nhap diem cua ban:');
switch n
case {9,10}
disp('hoc tot')
case {7,8}
disp('hoc kha')
case {6}
disp('hoc trung binh')
otherwise
disp('hoc kem')
end
Bài 3 – Giống ví dụ 3

Bài 4

Câu a – giống ví dụ 4

Câu b
%tim so hang thu n cua day fibonanci
function D=f(n)
if (n==1|n==2)
D = 1;
else
D=f(n-1)+f(n-2);
end
Bài 5

Câu a – giống vd 10

Câu b
password=input('xin moi nhap mat khau cua ban:','s')
switch password
case 'trungchonhihi1'
disp('chuc mung ban da nhap dung mat khau');
otherwise
disp('ban da nhap sai mat khau, hay nhap lai');
end

Vd6
Câu a
%tong cua 10 so bat ky
n=input('nhap vao so phan tu cua day:')
for i=1:n
a(i)=input(['a(',num2str(i),')=']);
end;
disp(a);
s=0;i=1;
while i<=10
s=s+a(i);
i=i+1;
end
disp(['tong cua 10 phan tu bat ki nhap tu ban phim la:',num2str(s)])

You might also like