Bai 1 clc x=-2:0.01:2 y=exp(-0.3.*x.^2- 0.5.*x +1).*cos(20.*x) subplot(2,1,1)% lenh subplot nay chia man %hinh do thi theo 2 hang 1 cot vi tri 1 plot(x,y,'m')%lenh nay ve do thi vao phan %hinh minh chia ra vào subplot 1 %hinhcon 2 subplot(2,1,2) % ve dang ham nay thi khai bao truc tiep ham trong dau '' fplot('exp(-0.3.*x.^2- 0.5.*x+1).*cos(20.*x)',[-2 2]) title('do thi ham so ')% lenh nay dien %tieu de cho do thi xlabel('truc x')% ghi tieu de cho truc x xlabel('truc y')% ghi tieu de cho truc y
Trang 1Cao V¨n ChÝnh_B1K52 Tổng hợp code các bài thực hành Matlab
Bai 1
clc
x=-2:0.01:2
y=exp(-0.3.*x.^2- 0.5.*x +1).*cos(20.*x)
subplot(2,1,1)% lenh subplot nay chia man
%hinh do thi theo 2 hang 1 cot vi tri 1
plot(x,y,'m')%lenh nay ve do thi vao phan
%hinh minh chia ra vào subplot 1
%hinhcon 2
subplot(2,1,2)
% ve dang ham nay thi khai bao truc tiep ham trong dau ''
fplot('exp(-0.3.*x.^2- 0.5.*x+1).*cos(20.*x)',[-2 2])
title('do thi ham so ')% lenh nay dien
%tieu de cho do thi
xlabel('truc x')% ghi tieu de cho truc x
xlabel('truc y')% ghi tieu de cho truc y
Bai 2
%hinh con 1
x=0:0.05:4;y=3.*sin(pi.*x);y1=exp(-0.2.*x)
subplot(2,1,1);plot(x,y,'r',x,y1,'b')
gtext('giao diem 1')% gan chu thich vao
%cac giao diem giua 2 do thi
subplot(2,2,3);bar(x1,y2)% ve do thi dang cot
subplot(2,2,4);stairs(x1,y2)%ve do thi dang bac thang
Bai 3
-% Chu y ham function o day chi chay dc tren Window thui,voi may tinh tren truong thi khong chay duoc dau!!!
Trang 2Cao V¨n ChÝnh_B1K52
Bai 4
[x,y]=meshgrid(-2:.1:2);
z=2.*x.*y./(x.^2 +y.^2 +1);
subplot(2,2,1);mesh(x,y,z);% do thi mesh
title('mesh')% gan chu thich mesh
subplot(2,2,2);surf(x,y,z)% do thi surf
if (n<0)|(n~=round(n))% n khac gia tri da lam tron cua n
fprintf('So n nhap vao khong phai la so nguyen duong')
Trang 3Cao V¨n ChÝnh_B1K52t5=bai6(5,x);plot(x,t5,'g');hold off;
legend('t0','t2','t3','t4','t5')% gan nha~n cua do thi
% Cau c
figure(2) % Cau c
x=linspace(-1,1);
plot(x,bai6(4,x));ylim([-1.5 1.5])%gioi han do thi
[x0,y0]=ginput;% lay toa do = cach click chuot
f=inline('bai6(4,x)','x');% de y vao ham nay khi tim khong diem
for k=1:length(x0);%do rong cua khong diem
[xn(k) fxn(k)]=fzero(f,x0(k));%ham tim khong diem thuc fzero
%tol la nhap so lieu dang sai so
%thay vi lap dao ham cua fx ta tinh luon dfx
fx=inline(fx);dfx=inline(dfx);%inline la
%1 ham moi ma minh tu dat no dang nhu 1
%function hoac @ ham
k(i)=fx(x0(i))./dfx(x0(i));%chu y chuyen ve
while abs(k(i))<= tol%dieu kien cua tri tuyet doi
%x(1)=x;x(2)=y,ham f lay chuan hoa
[x0 y0]=ginput;;%ginput la ham truy nhap bang chuot lay ket qua tu do thi
for k=1:length(x0)%ham fsolve la ham lay chinh xac nghiem
[xn,yn]=fsolve(f,[x0(k) y0(k)]);
3
Trang 4Cao V¨n ChÝnh_B1K52
end
fprintf(' Cac cap nghiem cua phuong trinh 1 la:\n');
%fprintf la ham hien thi ra man hinh comand window
contour(x,y,real(f),[0 0],'r');hold on% ve theo phan thuc
contour(x,y,imag(f),[0 0],'b');hold off% ve theo phan ao
disp('Toa do dinh ngoai')
k=1:2:9;% cho k chay tu 1 toi 9 voi buoc nhay la 2
xt=x(k)% toa do theo truc x
yt=y(k)%toa do theo truc y
disp('Toa do dinh trong')
subplot(2,2,1);plot(w,real(G));legend('Dothi Re(G)');%ve theo w va phan thuc
subplot(2,2,2);plot(w,imag(G));legend('Dothi Im(G)');%ve theo w va phan ao
subplot(2,2,3);plot(w,abs(G));legend('Do thi |G|');%ve theo w va tri tuyet doi
4
Trang 5Cao V¨n ChÝnh_B1K52subplot(2,2,4);plot(w,angle(G));legend('Do thi Arg(G)');%ve theo w va goc pha
% Cau c% tim cuc tri
%Tim cuc tieu
(-1)*fcd %chu y nhan -1 voi fcd
Bai 13 (Chua hiu)
%cau a
clc
5
Trang 6Cao V¨n ChÝnh_B1K52A01=[19 -9 -6;25 -11 -9;17 -9 -4];
Trang 7Cao V¨n ChÝnh_B1K52 disp(vn(i))
Trang 8Cao V¨n ChÝnh_B1K52plot(x,bai16b(bai16a(0),x),'r');hold on
legend('T0','T2','T3','T4','T5')% gan nha~n cua do thi
Bai 17 (chua chay dc)
%Doc lai bai tim tiem can cua do thi ham so
close all; clear; clc;
xv=solve(f2);% tim tiem can dung
fprintf(['Tiem can dung cua do thi ham so:x=',char(xv(1)),' Va ','x=',char(xv(2)),'\n']); yv=limit(f,x,-inf);%tim tiem can ngang
8
Trang 9Cao V¨n ChÝnh_B1K52fprintf(['Tiem can ngang cua do thi ham so:y=',char(yv),'\n']);
a=limit(f./x,x,inf);%tim tiem can xien
b21=b2(:)%Dua vecto hang thanh cot
% Lam vi du voi a1%
det(a1)%Tim dinh thuc ma tran
inv(a1)%Tim nghich dao ma tran
diag(a1)%Tim duong cheo ma tran
a=poly(a1)%Tim he so da thuc dac trung
p=poly2sym(a)%Tim da thuc dac trung
N=null(a1)%Ma tran chua n vector cuakhong gian 0
v=eig(a1)%Tim tri rieng cua ma tran
[v,d]=eig(a1)%v:ma tran lam cheo; d: dang cheo a1
n=size(N,2)%So chieu cua khong gian khong
rank(a1)%Tim hang ma tran
%tinh chuan cua cac vector b1,b2 va cuacac ma tran A1,A2,A3
fprintf('Chuan cua cac vector b1 & b2 va cua cac ma tran A1,A2,A3la:\n')
fprintf('Dinh thuc cua ma tranA1,A2,A3:\n')
% Tinh dinh thuc cua ma tran dung 'det'
detA1=det(A1)
detA2=det(A2)
detA3=det(A3)
if det(A1)==0
fprintf('Dinh thuc ma tran A= 0\n');
fprintf('Do do khong co ma tran nghic dao\n')
else
fprintf('Dinh thuc ma tran A=');disp(det(A1))
fprintf('Ma tran nghich dao cua A\n');disp(inv(A1));
end
if det(A2)==0
fprintf('Dinh thuc ma tran A= 0\n');
fprintf('Do do khong co ma tran nghic dao\n')
else
fprintf('Dinh thuc ma tran A=');disp(det(A2))
fprintf('Ma tran nghich dao cua A\n');disp(inv(A2));
end
if det(A3)==0
fprintf('Dinh thuc ma tran A= 0\n');
9
Trang 10Cao V¨n ChÝnh_B1K52fprintf('Do do khong co ma tran nghic dao\n')
else
fprintf('Dinh thuc ma tran A=');disp(det(A3))
fprintf('Ma tran nghich dao cuaA\n');disp(inv(A3));
end
% % Tinh ma tran nghich dao dung 'inv'
% fprintf('Ma tran nghich dao :\n')
%Xac dinh da thuc dac trung cua cac ma tran A1,A2,A3
fprintf('Da thuc dac trung cua cac ma tran A1,A2,A3')
Trang 11%sang editor khac
clc;clear all;close all
%phan a,b
[x,y]=meshgrid(linspace(-4,4,41))
z=2./((x-1.5).^2+2.*(y-1.8).^2+0.2)-1./(1.5.*(x+1.8).^2+(y+2).^2+0.1)
%cuc dai
[zmax,imax]=max(z) %tim gia tri lon nhat cua mang Z
[zmax,jmax]=max(max(z)) %tim vi tri cot cua gia tri lon nhat
imax=imax(jmax) %tim vi tri hang cua gia tri lon nhat
Trang 12Cao V¨n ChÝnh_B1K52fx=inline('2./((x(1)-1.5).^2+2.*(x(2)-1.8).^2+0.2)-1./(1.5.*(x(1)+1.8).^2+
contour(x,y,f1,[0 0],'r');hold on;
contour(x,y,f2,[0 0],'m');hold off;
Trang 13Cao V¨n ChÝnh_B1K52Y=a.*exp(b.*X.^2+c.*X);
Trang 14Cao V¨n ChÝnh_B1K52subplot(2,2,1)
text(xcd,-fcd,'< cuc dai')
% bang phuong phap noi suy spline
Trang 15Cao V¨n ChÝnh_B1K52 thang=inline('-ppval(sp,x1)','x1','sp');
plot([k k],[y1(k) y2(k)],'m')%ve duong chia doi
% To mau 2 phan bang nhau bang mau khac nhau
%vung 1: ta lay x chay tu giao dim x=k toi xn(1) cua duong ben tren,
%sau do di tip tu xn(1) toi x=k cua duong ben duoi;vay la het 1 vong di cua ½ dau
x1=linspace(k,xn(1));f1=y1(x1)
x2=linspace(xn(1),k);f2=y2(x2)
%vung 2: ta lay x chay tip tu x=k toi giao dim xn(2) cua duong ben duoi,
%sau do di tip tu xn(2) tro ve x=k cua duong ben tren ; vay la het 1 vong di ½ nua tipx3=linspace(k,xn(2));f3=y1(x3)
Trang 16%bai toan cauchy
%caua giai hptvp voi dk ban dau [-7.69 -15.61 90.39]);
dy=inline('[10*(u(2)-u(1));126.52*u(1)-u(2)-u(1)*u(3);u(1)*u(2)-8/3*u(3)]','t','u')
[t,u]=ode45(dy,[0 8],[-7.69 -15.61 90.39]);
subplot(2,2,1);plot(t,u(:,1),'r');title('do thi ham x(t)')
subplot(2,2,2);plot(t,u(:,2),'b');title('do thi ham y(t)')
subplot(2,2,3);plot(t,u(:,3),'y');title('do thi ham z(t)')
Trang 17Cao V¨n ChÝnh_B1K52[yn,dyon]=fzero(f,xo,[],yyo,dyend)
[x,y]=ode45(dy,[0 1],[yn -1]);%Gia tri y(0)va y'(0)
Trang 18Cao V¨n ChÝnh_B1K52
-
-Tổng hợp code các đề thi Matlab
figure(2); quiver(x,y,dzx,dzy); % ve ham vecto
%tinh gia tri ham Z bang function
Trang 19Cao V¨n ChÝnh_B1K52[dzx,dzy]=gradient(z);
subplot(1,2,1);plot(x,de3(x,4),'r'); %ve do thi cua tong
subplot(1,2,2);plot(x,de3b(x,1e-8),'b'); %ve dthi cua chuoi
%phan d
%ve mat ron xoay khi quay duong cong quanh truc x
[x,phi]=meshgrid(0 : pi/15 : 2*pi);
Trang 20Cao V¨n ChÝnh_B1K52x=linspace(0,10);
%ve mat tron xoay khi quay duong cong quanh truc x
[x,phi]=meshgrid(0 : pi/15 : 2*pi);
Trang 21Cao V¨n ChÝnh_B1K52
0 5 1
- 1 0 1 5 1
- 1 0 1 0
5 1
- 1 0 1
- 1 -5
- 1
- 5 0 5
1-10 1
Trang 22Cao V¨n ChÝnh_B1K52z1=x.^2-10.*x.*y-4.*y.^4;
%ve do thi phan thuc va phan ao
clear all,close all
%ve phan thuc va phan ao
[x,y]=meshgrid(linspace(-2,2)); %bo di cung dc
% tinh cac duong tiem can
clear all;close all;clc;
Trang 23Cao V¨n ChÝnh_B1K52fprintf(' yt='); disp(xtcnt);
fprintf('Tiem can ngang ben phai\n');
xtcnp=limit(f,inf);
fprintf(' yp='); disp(xtcnp);
fprintf('Tiem can xien\n');
% Ve tiem can ngang
%tiem can ngang ko xd
% Ve tiem can xien
Trang 24Cao V¨n ChÝnh_B1K52x1=-1:0.01:2;
Trang 25Cao V¨n ChÝnh_B1K52
0 0 2 0 4 0 6 0 8 1 1 2 1 4 1 6 1 8 2 1
0 5 1
1 5 2
2 5 3
< k hn d ie m
d ie m th u c n g ie m
hm n o i s u y
De 13
clear all,close all,clc
%noi suy da thuc va noi suy spline
Trang 260 5 1
clear all;close all;clc
%dung noi suy da thuc va noi suy spline
%poly2sym(heso,bien) de chuyen doi dang bieu dien cua x sang sym de tinh TP
%tinh dt theo noi suy da thuc (xap xi)
s2=quad(ff,0,44) %cach2 cho ham la ham goc: sym xi;s2=quad(inline(vectorize(y),'x',0,44))
%tinh dt theo noi suy spline
sp=fnint(spl);%tinh tp
s1=ppval(sp,44)-ppval(sp,0)%thay can vao sp de tinh gt tich phan
%tim nghiem bang pp spline ham y(x)=55.7
figure(2)
plot(x1,ppval(spl,x1)-55.7);hold on
26
Trang 27Cao V¨n ChÝnh_B1K52 plot(x1,0);
%giong bai27, phan cac bai thuc tap
% ve danh gioi va tinh dien tich
clear all,close all;clc
dk=2;%diu kin ban dau dat ra
xm=1.15;%duong chia ranh gioi lay bat ki
fprintf('Duong chia hinh thanh 2 phan co S bang nhau x=');
disp(xm) %gt xm duoc suat ra
s1=quad(f,xn(1),xm) %tinh dt 2 nua, de tim sai so
%vung 1: ta lay x chay tu giao dim x1 toi xm cua duong ben tren,
%sau do quay lai tu xm tro ve x1 cua duong ben duoi;het 1/2 vong
x1=linspace(xn(1),xm,20); y1=30.*sin(x1.^2-0.25);
x2=linspace(xm,xn(1),20); y2=(x2.^4+1)./log(x2+1)-10;
%vung 2: ta lay x chay tu xm toi giao dim x2 cua duong ben tren,
%sau do quay lai tu x2 tro ve xm cua duong ben duoi
Trang 28Cao V¨n ChÝnh_B1K52
%bit trc [0.7 1.2] ma ko can tin nhu bai 27 va de15,bai nay chia lam 3 phan=
% ve danh gioi va tinh dien tich
clear all,close all;clc
% Ve doan thang 1 x=xm de chia hinh thanh 3phan =
dk=1;xm=0.8;ss=1e-4; %gia su duong chia ban dau la xm=0.8
Trang 29Cao V¨n ChÝnh_B1K52
0 7 0 7 5 0 8 0 8 5 0 9 0 9 5 1 1 0 5 1 1 1 1 5 1 2 -1 0
-8 -6 -4 -2 0 2 4 6 8 1
x (x4+ 1 )/lo g (x + )-1 0
De 17
%phan a:tinh va ve do thi
%chu y trong khoang gioi han bai ra ta ko thay cuc tieu dau,kaka!!!
%nhung bai nay ta cu viet code de tim cuc tieu, ke no,okok !!!
clear all,close all,clc;
Trang 30%phan a:tinh va ve do thi
clear all,close all,clc;
%bai toan cauchy
%caua: giai ptvp va ve dthi
Trang 31Cao V¨n ChÝnh_B1K52 for k=1:length(x11)
%bai toan ve phuong phap ban
%phan a giai ptvp tinh y(x),y'(x)
f=inline('[y(2);y(1)+sin(x)*cos(x)+x^2*sin(x+pi/4)/(x^10+1)]','x','y')
[x y]=ode45(f,[0 5],[0 0])%[0 0] la dk ban dau;[0 5] la gioi han cua x
%cau b: ve do thi y(x) va y'(x) hay y(:,1) va y(:,2)
%[xn(:,k) yn(:,k)]=fsolve(inline('ppval(sp1,x)-ppval(sp2,x)','x'),x0(k))%tai sao khai
%bao the nay thi ko chay dc???????????????
%chu y: hoi cac ban cach giai giong bai 27,de gioi han lai dc hinh ta phai
%lam tnao va neu khai bao inline de chinh xac ngh thi lam tnao?????????
%de20 cach 2, hay hon nhieu cach1 cua Thay Chi
%bai toan ve phuong phap ban
31
Trang 32Cao V¨n ChÝnh_B1K52
%phan a giai ptvp tinh y(x),y'(x)
f=inline('[y(2);y(1)+sin(x)*cos(x)+x^2*sin(x+pi/4)/(x^10+1)]','x','y')
[x y]=ode45(f,[0 5],[0 0])%[0 0] la dk ban dau;[0 5] la gioi han cua x
%cau b: ve do thi y(x) va y'(x) hay y(:,1) va y(:,2)
%chu y vong lap for o day dung cho ham noi suy
%khac voi vong for dung voi ham so!!!!bai27
legend('y(x)','dy/dx','d2y/dx');%chu thich cho cac duong da ve
%cau b:tim nghiem pt:y(x)=1.5
%chu y vong lap for o day dung cho ham noi suy
%khac voi vong for dung voi ham so!!!!bai27
legend('y(x)','dy/dx','d2y/dx');%chu thich cho cac duong da ve
%cau b:tim nghiem pt:y(x)=1.5
Trang 33Cao V¨n ChÝnh_B1K52
Cach 1
%cach 1 nay khong chay duoc???????????
clear all;close all;clc;
text(tcd,-ycd,'<cuc dai day ne!!! ')
%chu y co dau tru nhe,bai nay minh thi quyen ko cho dau - vao,huhu !
tcd,-ycd
De 23
clear all;close all;
syms x; %bien syms vi o day ta co buoc tinh dao ham
%Cau a:ve do thi
text(xn,fxn,'< cuc tieu');
%Cau c:Tinh va ve tiem can
a1=limit(y/x,x,inf);a2=limit(y/x,x,-inf);
b1=limit(y-a1*x,x,inf);b2=limit(y-a2*x,x,-inf);
h1=ezplot(a1*x+b1);set(h1,'color','r')
33
Trang 34Cao V¨n ChÝnh_B1K52 h2=ezplot(a2*x+b2);set(h2,'color','r')
axis([-6 6 -3 14])
%Cau d:tinh do dai cung: bang tich phan sqrt(1+dy^2)
%chu y: tinh dao ham thi bien x phai o dang syms
%tinh dien tich cua vat the xoay quanh ox
%ban kinh vat the chinh la truc oy
disp('dien tich xung quanh');
0 10
20 -15 -10 -5 0 5 10 15
De 24
Chu y: Cau b khong bit lam
clear all;close all;clc
%Cau b:lap function chuyen duong cong bac 2 ve dang chinh tac
%cau b nay ko chay dc,ko hiu gi hit
A=[2,-1/2;-1/2,0];
B=[-5,0];
C=1;
34
Trang 35Cao V¨n ChÝnh_B1K52 F=de24b(A,B,C)
Cau b khong chay dc dau?????
clear all;close all;clc
%Cau b:lap function chuyen duong cong bac 2 ve dang chinh tac
%cau b nay ko chay dc,ko hiu gi hit
%bai toan ve phuong phap ban
clear all;close all;clc;
subplot(2,1,1);ezplot(y11,[0 5]);grid on %ve ham y
subplot(2,1,2);ezplot(y22,[0 5]);grid on %ve ham y'
Cau a: cach 2
clear all;close all;clc;
%cau a: giai ptvp voi dk ban dau va ve dthi:y,y'
Trang 36Hay xem lai cach giai PTVP nhe, chua hiu dau day !!!!!!!!!!!!!
Thi tot nhe !
OK !
36