Bài tập nhỏ Matlab
Nhập vào danh sách thuế xe ô tô, mỗi người bao gồm các thông tin sau : mã ,đơn vị, loại xe, số ngày thuê, số xe . Trong đó đơn vị dạng kí tự. Hãy lập trình thực hiện các mục sau :
1. Tính tiền thuê cho từng người :
Tiền thuê được tính như sau: 1 ngày đầu mỗi ngày là 500000 cho 1 xe, 3 ngày tiếp theo mỗi ngày là 400000 cho 1 xe, 4 tiếp theo mỗi ngày là 300000 cho 1 xe, các ngày tiếp theo mỗi ngày là 200000 cho 1 xe.
2. Sắp xếp theo số xe giảm dần.
3. Tìm thông tin của người khi nhập vào mã.
4. Tính tổng tiền thuê của từng đơn vị, tổng tiền thuê của cả danh sách.
5. Xóa đi một người khi nhập vào mã.
6. Thêm vào một người ở vị trí i bất kì.
7. Sửa chữa thông tin của một người khi nhập vào mã.
8. Vẽ đồ thị biểu diễn tương quan giữa tiền thuê và số ngày thuê.
9. Vẽ đồ thị dạng cột để so sánh tiền thuê và số xe.
10. Vẽ đồ thị dạng Pie cho cột tổng tiền thuê của từng đơn vị.
n1 %d\n',n1)
fprintf('\tBan chon tiep(12:thoat): ')
chon=input('');
case 6
mang1=size(arrdonvi);
if(mang1<1)
fprintf('\tBan hay chon 1 truoc! \n')
return
end
%Cau 6
%Them vao 1 nguoi o vi tri bat ky
n2=length(Nguoi);
fprintf('Ban muon them nguoi vao vi tri nao: ')
nhap2 = input('');
while(nhap2>n2)
fprintf('Vi tri ban them ngoai khoang cho phep, Yeu cau nhap lai: ')
nhap2 = input('');
end
%Chuan bi truoc khi them nguoi moi
tam1=0;
tam2='A';
tam3=0;
tam4=0;
tam5=0;
tam6=0;
% n=length(Nguoi);
% fprintf('kich thuoc truoc %d',n);
% Nguoi=Nguoi([2:end 1]);
n2=length(Nguoi);
%fprintf('kich thuoc sau %d',n2);
% for i =1:n+1
% if(nhap2==i)
% for j=nhap2:n
% %doi cho Ma
% tam1=Nguoi(j).Ma;
% Nguoi(j).Ma=Nguoi(j+1).Ma;
% Nguoi(j+1).Ma=tam1;
% %Doi cho don vi
% tam2=Nguoi(j).Donvi;
% Nguoi(j).Donvi=Nguoi(j+1).Donvi;
% Nguoi(j+1).Donvi=tam2;
% %doi cho loai xe
% tam3=Nguoi(j).Loaixe;
% Nguoi(j).Loaixe=Nguoi(j+1).Loaixe;
% Nguoi(j+1).Loaixe=tam3;
% %doi cho so ngay thue
% tam4=Nguoi(j).Songaythue;
% Nguoi(j).Songaythue=Nguoi(j+1).Songaythue;
% Nguoi(j+1).Songaythue=tam4;
% %doi cho so xe
% tam5=Nguoi(j).Soxe;
% Nguoi(j).Soxe=Nguoi(j+1).Soxe;
% Nguoi(j+1).Soxe=tam5;
% %doi cho tien thue
% tam6=Nguoi(j).Tienthue;
% Nguoi(j).Tienthue=Nguoi(j+1).Tienthue;
% Nguoi(j+1).Tienthue=tam6;
%
% end
% end
% end
%Ket thuc chua bi truoc khi them nguoi moi
%Them thong tin cho nguoi moi
fprintf('Nhap thong tin cho nguoi moi\n')
fprintf('\tNhap ma: ')
Nguoi(n2+1).Ma = input('');
fprintf('\tNhap vao don vi: ')
Nguoi(n2+1).Donvi=input('','s');
fprintf('\tNhap Loai xe: ')
Nguoi(n2+1).Loaixe=input('');
fprintf('\tNhap so ngay thue: ')
Nguoi(n2+1).Songaythue=input('');
fprintf('\tNhap so xe: ')
Nguoi(n2+1).Soxe=input('');
if(Nguoi(n2+1).Songaythue==1)
Nguoi(n2+1).Tienthue=500000;
else if(Nguoi(n2+1).Songaythue<=4)
Nguoi(n2+1).Tienthue=500000+(Nguoi(n2+1).Songaythue-1)*400000;
else if(Nguoi(n2+1).Songaythue<=8)
Nguoi(n2+1).Tienthue=500000 + 3*400000 +(Nguoi(n2+1).Songaythue-4)*300000;
else
Nguoi(n2+1).Tienthue= 500000 + 3*400000 +4*300000+(Nguoi(n2+1).Songaythue-8)*200000;
end
end
end
%Ket thuc them thong tin cho nguoi moi
%Sau do se doi cho nguoi moi them va nguoi thu nhap vao
%doi cho Ma
for i=n2+1:-1:nhap2+1
tam1=Nguoi(i).Ma;
Nguoi(i).Ma=Nguoi(i-1).Ma;
Nguoi(i-1).Ma=tam1;
%Doi cho don vi
tam2=Nguoi(i).Donvi;
Nguoi(i).Donvi=Nguoi(i-1).Donvi;
Nguoi(i-1).Donvi=tam2;
%doi cho loai xe
tam3=Nguoi(i).Loaixe;
Nguoi(i).Loaixe=Nguoi(i-1).Loaixe;
Nguoi(i-1).Loaixe=tam3;
%doi cho so ngay thue
tam4=Nguoi(i).Songaythue;
Nguoi(i).Songaythue=Nguoi(i-1).Songaythue;
Nguoi(i-1).Songaythue=tam4;
%doi cho so xe
tam5=Nguoi(i).Soxe;
Nguoi(i).Soxe=Nguoi(i-1).Soxe;
Nguoi(i-1).Soxe=tam5;
%doi cho tien thue
tam6=Nguoi(i).Tienthue;
Nguoi(i).Tienthue=Nguoi(i-1).Tienthue;
Nguoi(i-1).Tienthue=tam6;
end
n2=length(Nguoi);
fprintf('Sau khi them nguoi ta co \n')
fprintf('Ma DonVi LoaiXe SoNgayThue SoXe TienThue\n')
for i=1:n2
fprintf('%d %s %d %d %d %d\n', Nguoi(i).Ma, Nguoi(i).Donvi, Nguoi(i).Loaixe, Nguoi(i).Songaythue, Nguoi(i).Soxe,Nguoi(i).Tienthue)
end
fprintf('\tBan chon tiep(12:thoat): ')
chon=input('');
case 7
mang1=size(arrdonvi);
if(mang1<1)
fprintf('\tBan hay chon 1 truoc! \n')
return
end
%Cau 7
%Sua thong tin cua 1 nguoi khi nhap vao ma
fprintf('Ban muon sua thong tin nguoi co ma la gi? ')
nhap3 = input('');
n3= length(Nguoi);
vitri=0;
dvtam='A';
lxtam=0;
snttam=0;
sxtam=0;
tttam=0;
for i=1:n3
if(nhap3==Nguoi(i).Ma)
vitri=i;
dvtam=Nguoi(i).Donvi;
lxtam=Nguoi(i).Loaixe;
snttam=Nguoi(i).Songaythue;
sxtam=Nguoi(i).Soxe;
tttam=Nguoi(i).Tienthue;
fprintf('\n\tNhap vao don vi: ')
Nguoi(i).Donvi=input('','s');
fprintf('\tNhap Loai xe: ')
Nguoi(i).Loaixe=input('');
fprintf('\tNhap so ngay thue: ')
Nguoi(i).Songaythue=input('');
fprintf('\tNhap so xe: ')
Nguoi(i).Soxe=input('');
if(Nguoi(i).Songaythue==1)
Nguoi(i).Tienthue=500000;
else if(Nguoi(i).Songaythue<=4)
Nguoi(i).Tienthue=500000+(Nguoi(i).Songaythue-1)*400000;
else if(Nguoi(i).Songaythue<=8)
Nguoi(i).Tienthue=500000 + 3*400000 +(Nguoi(i).Songaythue-4)*300000;
else
Nguoi(i).Tienthue= 500000 + 3*400000 +4*300000+(Nguoi(i).Songaythue-8)*200000;
end
end
end
end
end
n3=length(Nguoi);
fprintf('TRUOC KHI SUA THONG TIN NGUOI CO MA %d TAI VI TRI %d NHU SAU \n',nhap3,vitri);
fprintf('Ma DonVi LoaiXe SoNgayThue SoXe TienThue\n')
fprintf('%d %s %d %d %d %d\n', Nguoi(vitri).Ma,dvtam,lxtam, snttam, sxtam, tttam)
fprintf('Sau khi sua thong nguoi ta co ma %d tai vi tri %d tro thanh\n',nhap3,vitri)
fprintf('Ma DonVi LoaiXe SoNgayThue SoXe TienThue\n')
fprintf('%d %s %d %d %d %d\n', Nguoi(vitri).Ma, Nguoi(vitri).Donvi, Nguoi(vitri).Loaixe, Nguoi(vitri).Songaythue, Nguoi(vitri).Soxe,Nguoi(vitri).Tienthue)
fprintf('CA DANH SACH SAU KHI SUA THONG TIN NHU SAU\n')
fprintf('Ma DonVi LoaiXe SoNgayThue SoXe TienThue\n')
for i=1:n3
fprintf('%d %s %d %d %d %d\n', Nguoi(i).Ma, Nguoi(i).Donvi, Nguoi(i).Loaixe, Nguoi(i).Songaythue, Nguoi(i).Soxe,Nguoi(i).Tienthue)
end
fprintf('\tBan chon tiep(12:thoat): ')
chon=input('');
case 8
mang1=size(arrdonvi);
if(mang1<1)
fprintf('\tBan hay chon 1 truoc: ')
return
end
%Cau 8
%Ve do thi bieu dien tuong quan giua tien thue va so ngay thue
n=length(Nguoi);
for i=1:n
ngaythue(i)=Nguoi(i).Songaythue;
tienthue(i)=Nguoi(i).Tienthue;
end
figure
plot(ngaythue,tienthue,'r*-')
xlabel('Ngay thue')
ylabel('Tien thue')
title('Tuong quan giua ngay thue va tien thue')
fprintf('\tBan chon tiep(12:thoat): ')
chon=input('');
case 9
%cau 9
%Ve do thi dang cot de so sanh tien thue va so xe
n=length(Nguoi);
for i=1:n
soxe(i)=Nguoi(i).Soxe;
tienthue1(i)=Nguoi(i).Tienthue;
end
figure
bar(soxe,tienthue1)
xlabel('So Xe')
ylabel('Tien Thue')
title('Tuong quan giua ngay thue va so xe')
fprintf('\tBan chon tiep(12:thoat): ')
chon=input('');
case 10
%cau 10
%Ve do thi dang pie Cho cot tien thue cua tung don vi
n=length(Nguoi);
for i=1:n
tienthue2(i)=Nguoi(i).Tienthue;
end
figure
pie(tienthue2)
fprintf('\tBan chon tiep(12:thoat): ')
chon=input('');
case 11
fprintf('====================================================\n')
fprintf('\t1.Nhap Du Lieu va tinh tien thue cho tung nguoi.\n')
fprintf('\t2.Sap xep theo so xe giam dan.\n')
fprintf('\t3.Tim thong tin tung nguoi khi nhap vao ma.\n')
fprintf('\t4.Tinh tong tien thue cua tung don vi va ca danh sach.\n')
fprintf('\t5.Xoa nguoi khi nhap va ma.\n')
fprintf('\t6.Them nguoi o vi tri bat ky.\n')
fprintf('\t7.Sua thong tin nguoi khi nhap ma.\n')
fprintf('\t8.Do thi tuong quan tien thue so ngay thue.\n')
fprintf('\t9.Do thi danh cot so sanh tien thue so ngay thue.\n')
fprintf('\t10.Do thi dang pie cho cot tong tien thue.\n')
fprintf('\t11.Ve Menu\n')
fprintf('\t12.Thoat.\n\n')
fprintf('====================================================\n')
fprintf('\tBan chon: ')
chon=input('');
end
end
end%ket thuc function
3,Câu 2
Code:
function Bai2()
dem=1;
for i=7:1.7:17
x=[-4:0.2:i];
y=3*x.^4 + 2*x.^3-2*x+1;
Xlabel('truc x')
Ylabel('truc y')
title('y=3*x^4 + 2*x^3-2*x+1')
subplot(3,2,dem)
plot(x,y,'r.-')
dem=dem+1;
Xlabel('truc x')
Ylabel('truc y')
title('y=3*x^4 + 2*x^3-2*x+1')
grid on
end
end
Hình ảnh:
Hình 2 : Đồ thị hàm số y=3x4 + 2x3-2x+1
4,Câu 3
Code:
function Bai3()
x=[-2.5:8.5]
y=x;
[X1,Y1]=meshgrid(x,y);
r=X1+Y1;
z=4*(cos(r)).^2+2*cos(r);%ve day hinh vuong
subplot(2,3,1),mesh(X1,Y1,z)
title('Meshgrid day hinh vuong')
xlabel('truc x')
ylabel('truc y')
zlabel('truc z')
y=x;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;%ve day hinh tron
z=4*(cos(R)).^2+2*cos(R);
subplot(2,3,4),mesh(X,Y,z)
title('Meshgrid day hinh tron')
xlabel('truc x')
ylabel('truc y')
zlabel('truc z')
[X1,Y1]=meshgrid(x,y);
r=X1+Y1;
z=4*(cos(r)).^2+2*cos(r);%ve day hinh vuong
subplot(2,3,2),surfc(X1,Y1,z)
title('Surfc day hinh vuong')
xlabel('truc x')
ylabel('truc y')
zlabel('truc z')
y=x;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;%ve day hinh tron
z=4*(cos(R)).^2+2*cos(R);
subplot(2,3,5),surfc(X,Y,z)
title('Surfc day hinh tron')
xlabel('truc x')
ylabel('truc y')
zlabel('truc z')
[X1,Y1]=meshgrid(x,y);
r=X1+Y1;
z=4*(cos(r)).^2+2*cos(r);%ve day hinh vuong
subplot(2,3,3),waterfall(X1,Y1,z)
title('Waterfall day hinh vuong')
xlabel('truc x')
ylabel('truc y')
zlabel('truc z')
y=x;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;%ve day hinh tron
z=4*(cos(R)).^2+2*cos(R);
subplot(2,3,6),waterfall(X1,Y1,z)
title('Waterfall day hinh tron')
xlabel('truc x')
ylabel('truc y')
zlabel('truc z')
Hinh ảnh:
Hình 3 : Đồ thị hàm số y= 4cos2x+ 2cosx
5,Câu 4
Code:
function Bai4()
syms x y
%Tinh Dao ham
y=2*tan(x).^3+2*sin(x).^2+3*tan(x)*cos(x)+1;
fprintf('Hien thi o dang quen thuoc c?a ham so: y=')
pretty(y)
fprintf('Dao ham cap 1:')
y1 = diff(y,x)
fprintf('Hien thi o dang quen thuoc c?a Dao ham cap 1: y1=:')
pretty(y1)
fprintf('Dao ham cap 2:')
y2 = diff(y,x,2)
fprintf('Hien thi o dang quen thuoc cua Dao ham cap 2: y2= :')
pretty(y2)
fprintf('Dao ham cap 5:')
y5 = diff(y,x,5)
fprintf('Hien thi o dang quen thuoc cua Dao ham cap 5: y5=')
pretty(y5)
%Tinh tich phan bat dinh cua ham y
fprintf('Tinh tich phan bat dinh theo bien x')
t=int(y,x)
fprintf('Hien thi o dang quen thuoc cua cua tich phan: S=')
pretty(t)
%Tinh gioi han khi x->0.6
fprintf('Tinh gioi han cua y khi x->0.6:')
L=limit(y,x,0.6)
end
IV,PHIẾU CHO ĐIỂM
1,Nhận xét
2,Cho điểm
Sinh Viên 1
Nguyễn Văn Thành
Sinh Viên 2
Nguyễn Xuân Trường
Sinh Viên 3
Nguyễn Chí Nhiên
Sinh Viên 4
Nguyễn Văn Việt
Chúng em xin cám ơn cô trong thời gian qua đã giúp chúng em hoàn thành môn học…!!!
File đính kèm:
Bài tập nhỏ Matlab.doc

