You are on page 1of 36

Cao Vn Chnh_B1K52

Tng hp code cc bi thc hnh 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 vo 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
gtext('giao diem 2')
gtext('giao diem 3')
gtext('giao diem 4')
% hinh con 2& 3
x1=0:0.05:10;y2=exp(-0.2.*x1).*cos(x1)
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!!!
%cau a
function tong=bai3a(n,x)
s=0;
for k=1:n
s=s+((-1).^k./k).*((-(x-1).^k).*(x>0 & x<1)+((1./x-1).^k).*(x>=1));
end
tong=s
% bai3b
function tong=bai3b(x)
ak=1;
ss=1e-6; s1=0;
k=0;
while any(abs(ak)>ss)
k=k+1
ak=((-1).^k./k).*((-(x-1).^k).*(x>0 & x<1)+((1./x-1).^k).*(x>=1));
s1=s1+ak;
end
tong=s1
%bai 3c
x=0.1:0.1:10;
plot(x,bai3a(1,x),'r');hold on;
plot(x,bai3a(5,x),'c');hold on;
plot(x,bai3a(10,x),'b');hold on;
plot(x,bai3b(x),'m');hold on;
legend('s1','s5','s10','s')
gtext('s1') ;gtext('s5')
gtext('s10') ;gtext('s')
Bai 4

Cao Vn Chnh_B1K52
[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
title('surf')
subplot(2,2,3);contour(x,y,z)
title('contour')%do thi duong dong muc contour
subplot(2,2,4);contour3(x,y,z)
title('contour3')%do thi duong dong muc 3chieu
Bai 5
% Cau a
subplot(2,2,1);
[teta,phi]=meshgrid(0:pi/30:pi, 0:pi/30:2*pi);
x0=0;y0=0;z01=-1;z02=1;r=1;
x1=x0+r*cos(phi).*sin(teta);
y1=x0+r*sin(phi).*sin(teta);
z11=z01+r*cos(teta);
z12=z02+r*cos(teta);
mesh(x1,y1,z11);hold on
mesh(x1,y1,z12);
% Cau b
subplot(2,2,2);R=2;r=0.25;
[teta,phi]=meshgrid(0:pi/30:2*pi);
x=(R+r*cos(teta)).*cos(phi)
y=(R+r*cos(teta)).*sin(phi)
z=r*sin(teta);mesh(x,y,z);
% Cau c
subplot(2,1,2);
a1=[0,1/sqrt(3),-1/2*sqrt(3)];
b1=[0,0,1/2];c1=[sqrt(2/3),0,0];
fill3(a1,b1,c1,'r');hold on;
a2=[1/sqrt(3),-1/2*sqrt(3),-1/2*sqrt(3)];
b2=[0,1/2,-1/2];c2=[0,0,0];
fill3(a2,b2,c2,'y');
a3=[-1/2*sqrt(3),-1/2*sqrt(3),0];
b3=[1/2,-1/2,0];c3=[0,0,sqrt(2/3)];
fill3(a3,b3,c3,'g');
a4=[-1/2*sqrt(3),0,1/sqrt(3)];
b4=[-1/2,0,0];c4=[0,sqrt(2/3),0];
fill3(a4,b4,c4,'r');
Bai 6:
function t=bai6(n,x)
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')
elseif n==0
t=1;
elseif n==1
t=x;
else
t=2.*x.*bai6(n-1,x)-bai6(n-2,x);
end
% Cau b;x=-1:0.01:1
t0=bai6(0,x)% tinh gia tri T0;
plot(x,t0);hold on
t1=bai6(1,x);plot(x,t1,'r');hold on
t2=bai6(2,x);plot(x,t2,'d');hold on
t3=bai6(3,x);plot(x,t3,'y');hold on %den day ko chay dc???
t4=bai6(4,x);plot(x,t4,'b');hold on
t5=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);

Cao Vn Chnh_B1K52
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
text(xn(k),fxn(k),'< 0 diem')%
end
% Tim cac max
[x1,y1]=ginput;
f1=inline('(-1)*bai6(4,x)','x');
for i=1:length(x1);
[xn(i) fxn(i)]=fminsearch(f1,x1(i));
text(xn(i),(-1)*fxn(i),'< max')%chu y co -1
end
% Tim cac min
[x1,y1]=ginput;
f1=inline('bai6(4,x)','x');
for i=1:length(x1);
[xn(i) fxn(i)]=fminsearch(f1,x1(i));% ham tim cuc tieu
text(xn(i),fxn(i),'< min')
end
Bai 7
function [x0,fx0,n,xn,fxn]=bai7(a,b,fx,dfx,tol);
%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
xi=linspace(a,b,100);
yi=subs(fx,'x',xi);%ham dao bien
plot(xi,yi,'r');grid on
[x0,fx0]=ginput(5);
for i=1:length(x0)
xn(i)=x0(i);%dau ra xn
n(i)=0;
k(i)=fx(x0(i))./dfx(x0(i));%chu y chuyen ve
while abs(k(i))<= tol%dieu kien cua tri tuyet doi
xn(i)=xn(i)-k(i);
k(i)=fx(xn(i))./dfx(xn(i));
n(i)=n(i)+1;% n dau ra thu i
end;
fxn(i)=fx(xn(i));%dau ra fxn thu i
end
%Phan b ra cua so window command de goi an
% cau b
[x0,fx0,n,xn,fxn]=bai7(0,3,'x-sin(x)-1','1-cos(x)',10^(-10))
[x0,fx0,n,xn,fxn]=bai7(1,5,'(log(x))^2-x^2 + 2*x + 3 ','2*log(x)/x - 2*x + 2',10^(-10))
[x0,fx0,n,xn,fxn]=bai7(0,5,'exp(-x)-sin(x^2)-0.5','-exp(-x)-2*cos(x^2)*x',1e-10)
Bai 8
close all;clc
[x,y]=meshgrid(-2:.2:2,0:.2:2);
z1=(x.^2).*sin(y)+cos(2*x).*y-0.5;
z2=(2.^x).*y-x.*(y.^2)-1;
contour(x,y,z1,[0 0],'r');hold on
contour(x,y,z2,[0 0],'b')
f=inline('[(x(1)^2)*sin(x(2))+cos(2*x(1))*x(2)-0.5;(2^x(1))*x(2)-x(1)*(x(2)^2)-1]','x');
%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)]);
end
fprintf(' Cac cap nghiem cua phuong trinh 1 la:\n');
%fprintf la ham hien thi ra man hinh comand window
xn,yn
% Cau b %luu sang editor khac

Cao Vn Chnh_B1K52
figure(2)
[x,y]=meshgrid(-2:.2:2); z=x+i*y;%z la bieu thuc phuc
f=z.^2+cos(log(abs(z)+1)+3)-2.^z-i;
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
[x0,y0]=ginput; z0=x0+i*y0;
fz=inline('z^2+cos(log(abs(z)+1)+3)-2^z-i','z');
for k=1:length(z0)
[zn,fzn]=fsolve(fz,z0(k))
end
fprintf(' Cac cap nghiem cua phuong trinh 2 la:\n');
zn
fzn
Bai 9
% phan a
clear all
R=1
r=R.*sin(pi/10)./cos(pi/5)
k=0:9;
x=R.*sin(k.*pi./5).*((-1).^k==1)+r.*sin(k.*pi./5).*((-1).^k==-1);
y=R.*cos(k.*pi./5).*((-1).^k==1)+r.*cos(k.*pi./5).*((-1).^k==-1);
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')
k=1:2:9;
xt=x(k+1)
yt=y(k+1)
% Muon ve hinh tron
p=linspace(0,2*pi,300);
R=1
x1=R.*cos(p);
y1=R.*sin(p);
figure(1)
fill(x1,y1,'r')
hold on
fill(x,y,'y')
axis equal
% ve quoc ky
figure(2)
fill([2.5 -2.5 -2.5 2.5],[2 2 -2 -2],'r')
hold on
fill(x,y,'y')
Bai 10
clear i
w=linspace(1e+3,1e+5)% tan so goc w
R=10
C1=1e-6
C2=0.1e-6
L=1e-3
ZL=L*2*pi*w*i
ZC1=1./(2*pi*w*C1)*i
ZC2=1./(2*pi*w*C2)*i
ZLC2=ZL.*ZC2./(ZL+ZC2)
G=ZLC2./(R+ZC1+ZLC2)
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
subplot(2,2,4);plot(w,angle(G));legend('Do thi Arg(G)');%ve theo w va goc pha
Bai 11
Lp biu thc tnh Pn: cn tnh t P0- P5 lm tng t nh bi 6b
function t=bai11a(n,x)
if (n<0)|n~=round(n)

Cao Vn Chnh_B1K52
fprintf('Hay nhap lai n nguyen duong')
elseif n==0 t=1;
elseif n==1 t=x;
else t=(2*n-1)/n*x*bai11a(n-1,x)-(n-1)/n*bai11a(n-2,x);
end
%Bai11B
function tong=bai11b(n,x);
if (n<0|n~=round(n))
disp('xin hay nhap lai so nguyen duong')
elseif n==0 tong=1;
elseif n==1 tong=x;
else
giaithua=1;
for k=1:n;
giaithua=giaithua*k;
end
tong=(1./(giaithua*2^n)).*diff((x^2-1)^n,n);
end
% Cau c %chua chay duoc
h=(1-x.^2).*diff(p1(n),x,2)-2.*x.*diff(p1(n),x)+n*(n+1)*p1(n)
i=quad(p1(n).^2,-1,1)-2/2*n+1
k=quad(p1(6)*p1(n).^2,-1,1)
Bai 12
% Cau a% ve mat cong
[x,y]=meshgrid(-5:.6:5);
z=(2.*x.^2+3.*y.^2).*exp(-(x.^2+y.^2))
figure(1)
subplot(2,2,1)
mesh(x,y,z)
% Cau b% tinh truong vecto va ve chung
[dzx,dzy]=gradient(z)
subplot(2,2,2)
quiver(x,y,dzx,dzy)
%dz=sqrt(dzx.^2+dzy.^2)
%subplot(2,2,3)
%quiver(x,y,dzx./dz,dzy./dz)
% Cau c% tim cuc tri
%Tim cuc tieu
subplot(2,2,4)
contour(x,y,z);shg
[xo yo]=ginput
f=inline('(2.*x(1).^2+3.*x(2).^2).*exp(-(x(1).^2+x(2).^2))','x')
for i=1:length(xo)
[ct(i,:) fct(i,:)]=fminsearch(f,[xo(i) yo(i)])
end
ct,fct
%Tim cuc dai
[x1 y1]=ginput
f1=inline('(-1)*(2.*x(1).^2+3.*x(2).^2).*exp(-(x(1).^2+x(2).^2))','x')
for j=1:length(x1)
[cd fcd]=fminsearch(f1,[x1(j) y1(j)])
end
cd,
(-1)*fcd %chu y nhan -1 voi fcd
Bai 13 (Chua hiu)
%cau a
clc
A01=[19 -9 -6;25 -11 -9;17 -9 -4];
A02=[-1 4 -2;-3 4 0;-3 1 3];
A=A02;% Thay A01 bang A02 de tinh cho ma tran thu 2
[P,a]=eig(A);A,P
fprintf('P^(-1)*A*P=\n');disp(inv(P)*A*P)
% Cau b
f=inline('5*x^2-4*x*y+8*y^2+20/sqrt(5)*x-80/sqrt(5)*y+4','x','y');
ezplot(f,[-4 4 -2 6]);shg

Cao Vn Chnh_B1K52
% Cau c
function [A1,b1,c1,V,T]=bai13c(A,b,c)
[V,A1]=eig(A);
a=diag(A1);
b1=b*V; c1=c;
for k=1:length(a);
if a(k)~=0
T(k)=-b1(k)/(2*a(k));
c1=c1-b1(k)^2/(4*a(k));
b1(k)=0;
end
end
% Thu lai cho cau b
clc
A=[5 -2;-2 8];
b=[20/sqrt(5) -80/sqrt(5)]; c=4;
[A1,b1,c1,V,T]=bai13c(A,b,c);
syms x y;
XY=[x y];
pt=XY*A1*(XY.')+b1*(XY.')+c1;pretty(pt)
ezplot(pt)
% Cau d
syms T
D=[T 1 1;1 T 1;1 1 T];
b=[1;T;T^2];
Dx=[b D(:,2) D(:,3)]; Dy=[D(:,1) b D(:,3)]; Dz=[D(:,1) D(:,2) b];
d=det(D); dx=det(Dx); dy=det(Dy); dz=det(Dz);
ng=solve(d,T);
% Don gian nghiem
xx=[];
for i=1:length(ng)
t=0;
for j=1:length(xx)
if ng(i)==xx(j);
t=t+1;
end
end
if t==0
xx=[xx ng(i)];
end
end
ng=xx;vd=[];vn=[];
for i=1:length(ng);
if (subs(dx,'T',ng(i))==0)|(subs(dy,'T',ng(i))==0)|(subs(dz,'T',ng(i))==0)
vd=[vd,ng(i)];
else
vn=[vn,ng(i)];
end
end
fprintf('Phuong trinh vo dinh voi T=');
for i=1:length(vd);
disp(vd(i))
end
fprintf('Phuong trinh vo nghiem voi T=');
for i=1:length(vn);
disp(vn(i))
end
fprintf('PT co nghiem duy nhat voi T khac');disp(ng)
fprintf('
x=');disp(dx/d)
fprintf('
y=');disp(dy/d)
fprintf('
z=');disp(dz/d)
Bai 14
%Cau a

Cao Vn Chnh_B1K52
syms z
x=sqrt(5*pi/2-z).*cos(sqrt(5*pi/2-z))+pi
dx=diff(x,z)
f=inline('dx')
l=quad(f,0,2*pi)%Tinh chieu dai cung
%Cau b
%Tinh dien tich xung quanh
x1=x.*(sqrt(1+dx))
f1=inline('x1')
s=2*pi*quad(f1,0,2*pi)
%Tinh the tich xoay quanh Oz
x2=x.^2
f2=inline('x2')
v=pi*quad(f2,0,2*pi)
%Tinh the tich xoay quanh Ox
x3=z.*x
f3=inline('x3')
v1=2*pi*quad(f3,0,2*pi)
%Cau c
ezplot(x3,[0 2*pi])
Bai 15
clear all;clc;close all
syms x
a=input('nhap a=');
Io=input('nhapIo=');
d=sqrt(x.^2+a^2);
I=Io.*x./d.^3;
dt=diff(I,x);
f1=diff(I,x,2);
x0=solve(dt,x)
f2=subs(f1,x,x0)
for k=1:length(f2)
if double(f2(k))<0
xcd=x0(k);
end
end
xcd
Icd=subs(I,x,xcd)
Bai 16
%Cau a: Chu y luu ra 1 editor khac,luu voi ten: bai16a
function t=bai16a(n)
if n==0
t=[0 1]
elseif n==1
t=[1 0]
else n>1
t=polyadd(2*conv([1 0],s(n-1)),-s(n-2))
end;
t
%Cau b: luu ra 1 editor khac lay ten la:bai16b
function s=bai16b(a,b)
na=length(a);
nb=length(b);
s=[zeros(1,(na-nb)),b]+[zeros(1,(nb-na)),a]
%Luu doan code nay sang editor khac, ten file la gi cung dc
figure(1);x=-1:.1:1;
plot(x,bai16b(bai16a(0),x),'r');hold on
plot(x,bai16b(bai16a(1),x),'m');hold on
%cach 2
function T=bai16(n,x);
if n<0 |n~=round(n)
fprintf('Nhap lai n khong am');
elseif n==0
T=1;
elseif n==1

Cao Vn Chnh_B1K52
else
end

T=x;
T=2.*x.*bai16(n-1,x)-bai16(n-2,x);

clear;clc;
x=linspace(-1,1,100);
T0=bai16(0,x)% tinh gia tri T0
plot(x,T0);hold on
T1=bai16(1,x)
plot(x,T1,'r');hold on
T2=bai16(2,x)
plot(x,T2,'d');hold on
T3=bai16(3,x) %den cho nay ko chay dc, chua hiu tai sao?????
plot(x,T3,'dr');hold on
T4=bai16(4,x)
plot(x,T4,'b');hold off
legend('T0','T2','T3','T4','T5')% gan nha~n cua do thi
Bai 17 (chua chay dc)
% cau a
function s=bai17(xj,t)
ts=1;
for k=1:length(xj)
if k~=t
ts=conv(polyadd([1 0],[0 -xj(k)]),ts);
end
end
ms=polyval(ts,xj(t));
s=ts/ms
%phan b
xj=-2:.5:2;t=-2:.2:2;
mau='rmgcbk';so='123456';
close all
for i=1:5
plot(t,polyval(bai17(xj,i),t),mau(i))
text(-1.8,polyval(bai17(xj,i),-1.8),so(i));hold on
end
Bai 18
%Doc lai bai tim tiem can cua do thi ham so
close all; clear; clc;
syms x;
f1=abs(x.^3+x+1)-3;
f2=x.^2-x-6;
f=f1/f2 + x;
yi=linspace(-30,30,100);
ezplot(f,[-10 10]);axis ([-10 10 -30 30]);hold on;
[x0,fx0]=ginput;
fx=inline(char(f),'x');% cach 2 fx=inline('(abs(x.^3+x+1)-3)/(x.^2-x-6) + x','x');
for k=1:length(x0);
[xn(k,:)]=fsolve(fx,x0(k));
end
xn
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
fprintf(['Tiem can ngang cua do thi ham so:y=',char(yv),'\n']);
a=limit(f./x,x,inf);%tim tiem can xien
b=limit(f-a*x,x,inf);
fprintf(['Tiem can xien cua do thi ham so:y=',char(a),'x + ',char(b),'\n']);
plot(double(xv(1)*ones(size(yi))),yi,'r')
;% cach 2 :plot(double(xv(1)),yi,'r')
plot(double(xv(2)*ones(size(yi))),yi,'r')
;% cach 2 :plot(double(xv(2)),yi,'r')
xi=(yi-1)/2;plot(xi,yi,'r');
plot(xi,double(-1*ones(size(xi))),'r');%

Cao Vn Chnh_B1K52
%cach 2 :plot(xi,-1,'r');
hold off;
Bai 19
% Bai tap so 19%
a1=[1 2 3;4 5 6; 7 8 9];
a2=[1 1 3;4 5 6;7 8 9];
a3=[0.9999 2 3;4 5 6;7 8 9]
b1=[1 1 3]
b11=b1(:)
b2=[1 2 3]
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
%----------------------------------------A1=[1 2 3;4 5 6;7 8 9];
A2=[1 1 3;4 5 6;7 8 9];
A3=[0.9999 2 3;4 5 6;7 8 9];
b1=[1;1;3];b2=[1;2;3];
%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')
norb1=norm(b1)
norb2=norm(b2)
norA1=norm(A1)
norA2=norm(A2)
norA3=norm(A3)
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');
fprintf('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')
% invA1=inv(A1)
% invA2=inv(A2)
% invA3=inv(A3)

Cao Vn Chnh_B1K52
%Tinh tri rieng cua cac ma tran dung
'eig'
[V1,d1]=eig(A1) %tri rieng d1, Vectorrieng V1
[V2,d2]=eig(A2)
[V3,d3]=eig(A3)
fprintf('Tri rieng d1:\n')
disp(d1)
fprintf('Tri rieng d2:\n')
disp(d2)
fprintf('Tri rieng d3:\n')
disp(d3)
fprintf('Vector rieng V1:\n')
disp(V1)
fprintf('Vector rieng V1:\n')
disp(V2)
fprintf('Vector rieng V1:\n')
disp(V3)
%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')
dactrungA1=poly(A1)
dactrungA2=poly(A2)
dactrungA3=poly(A3)
Bai 20
% Lap function de giai
function [ln,x]=bai20(A,b)
if (rank(A)==rank([A,b]))&(rank(A)==size(A,1))
ln='Pt co nghiem duy nhat';
x=A\b ;
elseif (rank(A)==rank([A,b]))&(rank(A)<size(A,1))
ln='Pt Vo so nghiem';
syms x1 c;
x0=null(A);% Nghiem pt thuan nhat
xc1=solve((A(1,1)+A(1,2))*x1+A(1,3)-b(1),x1);
xc=[xc1 xc1 1].';
x=c*x0+xc;
else
ln='Pt Vo nghiem voi tua nghiem';
x=pinv(A)*b;
end
%sang editor moi
%cau a b c
clc
A1=[1 2 3;4 5 6;7 8 9];
A2=[1 1 3;4 5 6;7 8 9];
A3=[0.999 2 3;4 5 6;7 8 9];
b1=[1 1 3].'; b2=[1 2 3].';
[ln,x11]=bai20(A1,b1)
[ln,x12]=bai20(A1,b2)
[ln,x21]=bai20(A2,b1)
[ln,x22]=bai20(A2,b2)
[ln,x31]=bai20(A3,b1)
[ln,x32]=bai20(A3,b2)
Bai 21
function [t,lap]=bai21(x)
s=1;k=0;mau=1;ak=1;
while any(abs(ak)>1e-5);
k=k+1;
n=2*k;
mau=(-1)*mau*(k^2);
ak=(1/mau)*(x/2).^n;
s=s+ak;
end
lap=k;t=s;
%sang editor khac

Cao Vn Chnh_B1K52
clc;close all;
x=0:0.01:10;
plot(x,bai21(x));grid on
[x0,y0]=ginput;
f=inline('bai21(x)','x');
fprintf('Cac nghiem cua phuong trinh:')
for k=1:length(x0);
[xn(k) fxn(k)]=fzero(f,x0(k));
end
xn
fxn
clc;close all;
x=0:0.01:10;
plot(x,bai21(x));grid on
% Tim cac min
f=inline('bai21(x)','x');
[x1 y1]=ginput;
fprintf('Cac diem cuc tieu:')
for i=1:length(x1);
[xmin(i) fxmin(i)]=fminsearch(f,x1(i));
end;
xmin
fxmin
% Tim cac max
[x1,y1]=ginput;
fprintf('Cac diem cuc dai:')
f1=inline('(-1)*bai21(x)','x');
for i=1:length(x1);
[xmax(i) fxmax(i)]=fminsearch(f1,x1(i));
end;
xmax
-fxmax
Bai 22
function z=bai22(x,y)
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);
%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
%cuc tieu
[zmin,imin]=min(z)
[zmin,jmin]=min(min(z))
imin=imin(jmin)
%ve
subplot(2,1,1);surf(x,y,z);
subplot(2,1,2);contour(x,y,z,20);
%tim cuc tri
[x0 y0]=ginput
fx=inline('2./((x(1)-1.5).^2+2.*(x(2)-1.8).^2+0.2)-1./(1.5.*(x(1)+1.8).^2+
(x(2)+2).^2+0.1)','x');
for k=1:length(x0);
[xct(k,:) fxct(k,:)]=fminsearch(fx,[x0(k) y0(k)]);
end
xct
fxct
%========================
[x1,y1] =ginput
g=inline('(-1).*(2./((x(1)-1.5).^2+2.*(x(2)-1.8).^2+0.2)-1./(1.5.*(x(1)+1.8).^2+
(x(2)+2).^2+0.1))','x');

Cao Vn Chnh_B1K52
for k=1:length(x1);
[xcd(k,:) fxcd(k,:)]=fminsearch(g,[x1(k) y1(k)]);
end
xcd
-fxcd
Bai 23
%cau a
syms x;
f1=inline('x^2-5*x*sin(x^2)-x+6.2','x');
figure(1);fplot(f1,[0 4]);grid on
[x0,y0]=ginput;
for k=1:length(x0);
[xn(k),fxn(k)]=fzero(f1,x0(k));
end;
xn,fxn
%cau b
figure(2);clear all
[x,y]=meshgrid(0:.1:2);
z=x+i*y;
f=z.^2+2.^z-1.5-3*i;
contour(x,y,real(f),[0 0],'r');hold on
contour(x,y,imag(f),[0 0],'b');hold off
[x0,y0]=ginput
z0=x0+i*y0;
fz=inline('z^2+2^z-1.5+3*i','z');
for k=1:length(z0)
[zn(k,:),fzn(k,:)]=fsolve(fz,z0(k,:));
end
zn,fzn
%cau c
clear all; clc;
[x y]=meshgrid(0:.1:2);
f1=sin(x).^2+sin(y).^2-1;
f2=4*x.*y+0.1*x+0.2*y-1;
contour(x,y,f1,[0 0],'r');hold on;
contour(x,y,f2,[0 0],'m');hold off;
pt1='sin(x(1)).^2+sin(x(2)).^2-1';
pt2='4.*x(1).*x(2)+0.1.*x(1)+0.2.*x(2)-1';
hpt=['[',pt1,',',pt2,']'];
fx=inline(hpt,'x');
[x0 y0]=ginput
for k=1:length(x0);
[xn(k,:),fxn(k,:)]=fsolve(fx,[x0(k) y0(k)]);
end
xn,fxn
Bai 24
xi=[0 0.2 0.4 0.6 0.8 1]
yi=[2.3 2.87 3.72 5.02 7.05 10.3]
Y=log(yi)
h=polyfit(xi,Y,2)
a=h(3)
b=h(1)
c=h(2)
X=linspace(0,1);
Y=a.*exp(b.*X.^2+c.*X);
plot(xi,yi,'o');hold on;
plot(X,Y,'-');
Bai 25
clc;close all;clear all
x1=[0 1 2 3 4 5 6 7 8]
y1=[0.6 0.75 0.8 0.5 0.1 -0.5 -0.7 -0.3 0.1]
plot(x1,y1,'+');hold on
x2=0:0.001:8
a=polyfit(x1,y1,5);
sp=spline(x1,y1);

Cao Vn Chnh_B1K52
y=polyval(a,x2);plot(x2,y,'r');hold on
y1=ppval(sp,x2);plot(x2,y1,'y');hold on
%Cau b
% Tim khong diem ham noi suy spline
f1=inline('ppval(sp,x2)','x2','sp');
[x1,y1]=ginput
for k=1:length(x1)
[xn1(k),yn1(k)]=fsolve(f1,x1(k),[],sp);
end
xn1
yn1
% tinh cuc tri bang phuong phap noi suy spline
% cuc tieu
[x11,y11]=ginput;
for k=1:length(x11)
[xn12(k),fxn12(k)]=fminsearch(f1,x11(k),[],sp);
end
text(xn12,fxn12,'*');
%cuc dai
f2=inline('-ppval(sp,x)','x','sp');
[x21,y21]=ginput;
for k=1:length(x21)
[xn21(k),fxn21(k)]=fminsearch(f2,x21(k),[],sp);
end
text(xn21,-fxn21,'*');
%Tim khong diem ham noi suy xap xi
f3=inline('polyval(a,x2)','x2','a');
[x0,y0]=ginput
for k=1:length(x0)
[xn01(k),yn01(k)]=fsolve(f3,x0(k),[],a);
end
xn01
yn01
% TIm cuc tri ham noi suy xap xi
[x10 y10]=ginput
for i=1:length(x10)
[xn10(i),yn10(i)]=fminsearch(f3,x10(i),[],a)
end
xn10
yn10
plot(xn10,yn10,'*')
f4=inline('-polyval(a,x2)','x2','a')
[x20 y20]=ginput
for i=1:length(x20)
[xn20(i), yn20(i)]=fminsearch(f4,x20(i),[],a)
end
xn20
-yn20
Bai 26
clc;close all;clear all;
xi=0:0.3:3;
yi=sqrt(xi+1).*cos(xi.^2+1)./(xi.^4+1);
%Ve cac diem ung voi bang da cho cua xi,yi
figure(1);
subplot(2,2,1)
plot(xi,yi,'*');hold on
%Ve cac diem noi suy spline
x1=0:0.001:3;
sp=spline(xi,yi);
y1=ppval(sp,x1)
plot(x1,y1,'r')
% Ve do thi ham goc
subplot(2,2,2)
syms x
y=sqrt(x+1).*cos(x.^2+1)./(x.^4+1);

Cao Vn Chnh_B1K52
ezplot(y,[0 3]);hold on
% Cau B
%Tinh dao ham cap 1,2 ham noi suy spline dung "fnder" va ve do thi
subplot(2,2,3)
dh1=fnder(sp)
y2=ppval(dh1,x1); plot(x1,y2,'y');hold on
dh2=fnder(dh1)
y3=ppval(dh2,x1);plot(x1,y3,'r');hold on
% Tinh dao ham cap 1,2 ham goc va ve do thi
subplot(2,2,4)
dy1=diff(y,x)
dy2=diff(dy1,x)
ezplot(dy1,[0 3]);hold on
ezplot(dy2,[0 3]);hold on
% Tinh tich phan ham noi suy spline bang "fnint" va ham goc bang "quad"
tpsp=fnint(sp)
tp03=ppval(tpsp,3)-ppval(tpsp,0)
tphg=quad(inline(vectorize(y),'x'),0,3)
% Cau C
%tim ko diem,cuc tri cua ham goc
thu=inline('sqrt(x+1)*cos(x^2+1)/(x^4+1)');
[x0,y0]=ginput;
for k=1:length(x0)
[xn(k) fxn(k)]=fsolve(thu,x0(k))
end
text(xn,fxn,'<ko diem')
%cuc tieu
[x01,y01]=ginput;
for k=1:length(x01)
[xct(k) fct(k)]=fminsearch(thu,x01(k));
end
text(xct,fct,'< cuc tieu')
%cuc dai
thu1=inline('-sqrt(x+1)*cos(x^2+1)/(x^4+1)');
[x02,y02]=ginput;
for k=1:length(x02)
[xcd(k) fcd(k)]=fminsearch(thu1,x02(k))
end
text(xcd,-fcd,'< cuc dai')
% bang phuong phap noi suy spline
%tinh ko diem
thang=inline('ppval(sp,x1)','x1','sp');
[x11,y11]=ginput;
for k=1:length(x11)
[xn11(k),fxn11(k)]=fsolve(thang,x11(k),[],sp);
end
text(xn11,fxn11,'*');
% cuc tieu
[x12,y12]=ginput;
for k=1:length(x12)
[xn12(k),fxn12(k)]=fminsearch(thang,x12(k),[],sp);
end
text(xn12,fxn12,'*');
%cuc dai
thang=inline('-ppval(sp,x1)','x1','sp');
[x21,y21]=ginput;
for k=1:length(x21)
[xn21(k),fxn21(k)]=fminsearch(thang,x21(k),[],sp);
end
text(xn21,-fxn21,'*');
Bai 27
clc;clear all;close all;
y1=inline('(x-1.5).^2-1');
y2=inline('exp(-log(x).^2)');

Cao Vn Chnh_B1K52
y=inline('(x-1.5).^2-1-exp(-log(x).^2)');%hoac
fplot(y1,[0 3],'r');hold on
fplot(y2,[0 3],'b');
% Tinh vi tri giao nhau de gioi han lai diem ve
[x0,y0]=ginput
for k=1:length(x0)
[xn(k),yn(k)]=fsolve(y,x0(k))
end
xn(1);
xn(2);
fplot(y1,[xn(1) xn(2)],'r')
fplot(y2,[xn(1) xn(2)],'r')
%Tinh dien tich ranh gioi giua 2 dung cong
tp=quad(y,xn(1),xn(2))

f=inline(vectorize(y1-y2))

%Ve duong thang song song oy chia hinh thanh 2 phan bang nhau
tol=1e-3;
k=xn(1);
dt=0;
while (abs(dt-tp/2)>tol)
k=k+0.001;
dt=quad(y,xn(1),k);
end
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 tip
x3=linspace(k,xn(2));f3=y1(x3)
x4=linspace(xn(2),k);f4=y2(x4)
fill([x1 x2],[f1,f2],'r',[x3 x4],[f3,f4],'b')
1
0
.
8
0
.
6
0
.
4
0
.
2
0
0
.
2
0
.
4
0
.
6
0
.
8
1

0
.5

1
.
5

Bai 28
clear all,close all,clc;
f=inline('cos(t.^2)./(t+1)','t');
xi=linspace(0,1.5,50);
for k=1:length(xi)
yi(k)=quad(f,0,xi(k).^2+1)+0.25*xi(k)-0.8;
end
plot(xi,yi); grid on
%cau b
sp=spline(xi,yi);
fx=inline('ppval(sp,x)','x','sp');
[x0,y0]=ginput
for k=1:length(x0)
[xn(k),fxn(k)]=fsolve(fx,x0(k),[],sp);
end

2
.5

Cao Vn Chnh_B1K52
text(xn,fxn,'*nghiem')
fprintf('cac nghiem cua pt:')
xn
Bai 29
%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)')
%Cau b
dy=inline('[y(2);(8*x^3*y(1)^3+y(2))./x]','x','y')
[x,y]=ode45(dy,[1 4],[1/2 -1/2]);
figure(2);
subplot(2,1,1);plot(x,y(:,1),'b'); xlabel('x');ylabel('y')
subplot(2,1,2);plot(x,y(:,2),'r'); xlabel('x');ylabel('y1')
Bai 30
%giai ptvp bang pp ban
%Cau a
dy=inline('[y(2);6*y(1)^2-y(2)]','x','y');
dyo=-2:.1:2;
for i=1:length(dyo)
[x,y]=ode45(dy,[0 1],[1 dyo(i)])%ham ode45 de giai PTVP, cho ngh thuc+phuc
yend(i)=y(end,1)-2;
end;
f=inline('spline(dyo,yend,t)','t','dyo','yend');
t=linspace(dyo(1),dyo(end),50);
u=f(t,dyo,yend);
figure(3)
subplot(2,1,1)
plot(t,u,'m');
[xo,yo]=ginput
[dyon,yn]=fzero(f,xo,[],dyo,yend)
[x,y]=ode45(dy,[0 1],[1 dyon]);
subplot(2,2,3);
plot(x,y(:,1),'b');
subplot(2,2,4)
plot(x,y(:,2),'r');
%Cau b
dy=inline('[y(2);6*y(1)^2-y(2)]','x','y');
yyo=-2:.1:2;%y tai cac gia tri khac nhau%
for k=1:length(yyo)
[x,y]=ode45(dy,[0 1],[yyo(k) 1]);
%Gia tri y tai gia tri # va y'(1)
dyend(k)=y(end,2)-1;
end
f=inline('spline(yyo,dyend,t)','t','yyo','dyend');
t=linspace(yyo(1),yyo(end),50)
u=f(t,yyo,dyend);
figure(2)
subplot(2,1,1)
plot(t,u,'m');
[xo,yo]=ginput
[yn,dyon]=fzero(f,xo,[],yyo,dyend)
[x,y]=ode45(dy,[0 1],[yn -1]);%Gia tri y(0)va y'(0)
subplot(2,2,3);
plot(x,y(:,1),'b');
xlabel('x')
ylabel('y')
subplot(2,2,4)
plot(x,y(:,2),'r');
xlabel('x')
ylabel('y1')
% Cau c

Cao Vn Chnh_B1K52
dy=inline('[y(2);6*y(1)^2-y(2)]','x','y');
dyo=-2:.1:2;
for k=1:length(dyo)
[x,y]=ode45(dy,[0 1],[1 dyo(k)]);
%y(0) va y'tai gia tri #
dyend(k)=y(end,2)-3;
end
f=inline('spline(dyo,dyend,t)','t','dyo','dyend');
t=linspace(dyo(1),dyo(end),100);
u=f(t,dyo,dyend);
figure(2)
subplot(2,1,1)
plot(t,u,'g');
[xo,yo]=ginput
[dyon,yn]=fzero(f,xo,[],dyo,dyend)
[x,y]=ode45(dy,[0 1],[1 dyon]);%y(0) va y'(0)
subplot(2,2,3);
plot(x,y(:,1),'b');
subplot(2,2,4)
plot(x,y(:,2),'r');

% Cach 2

%cau a
clear all;close all;clc;
hpt='Dy1=y2,Dy2=6.*y1-y2';
dkb='y1(0)=1,y1(1)=2';
dsb=dsolve(hpt,dkb,'x')
y1b=simple(dsb.y1)
y2b=simple(dsb.y2)
figure(1);
subplot(2,1,1);
ezplot(y1b,[0 1]);grid on;hold on
ezplot(y2b,[0 1]);
%cau b
dkb='y2(0)=-1,y2(1)=1';
dsb=dsolve(hpt,dkb,'x')
y1b=simple(dsb.y1)
y2b=simple(dsb.y2)
subplot(2,1,2);
ezplot(y1b,[0 1]);grid on ;hold on
ezplot(y2b,[0 1]);
%cau c
dkb='y1(0)=1,y2(1)=1';
dsb=dsolve(hpt,dkb,'x')
y1b=simple(dsb.y1)
y2b=simple(dsb.y2)
figure(2);
subplot(2,1,1);
ezplot(y1b,[0 1]);grid on;hold on
ezplot(y2b,[0 1]);

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Tng hp code cc thi Matlab

De 1:
clc;close all;clear all
%cau a
%luoi 41 chia deu
[x,y]=meshgrid(linspace(-2,2,41));

Cao Vn Chnh_B1K52
z=8./(x.^4+y.^4+2.*x+2.*y+4)-exp(-x.^2-y.^2+x+y+1);
%cau b
%tim chi so hang i, cot j, ung voi gi tri lon nhat cua Z
[zmax,imax]=max(z)
[zmax,jmax]=max(max(z))
i=imax(jmax)
%tim chi so hang i, cot j, ung voi gi tri lon nhat cua Z
[zmin,imin]=min(z)
[zmin,jmin]=min(min(z))
i=imin(jmin)
%ve mat cong
subplot(1,2,1); surf(x,y,z)
%ve 15 duong dong muc
subplot(1,2,2); contour(x,y,z,15);
%cau d: tinh min, max
[x0 y0]=ginput
f=inline('8./(x(1).^4+x(2).^4+2.*x(1)+2.*x(2)+4)-exp(-x(1).^2-x(2).^2+x(1)+x(2)+1)','x');
for k=1:length(x0)
[xn(k,:),fxn(k,:)]=fminsearch(f,[x0(k) y0(k)]);
end
disp('cuc tieu la');
xn
%cau c:tinh dao hs ham z theo huong x,y,voi cac dim chia cach deu
[dzx,dzy]=gradient(z);
figure(2); quiver(x,y,dzx,dzy); % ve ham vecto
%tinh gia tri ham Z bang function
function z=de1a(x,y)
z=8./(x.^4+y.^4+2.*x+2.*y+4)-exp(-x.^2-y.^2+x+y+1);
end
De 2
%giong bai 1
%thiet lap luoi chia 31x31
[x,y]=meshgrid(linspace(-2,2,31));
z=(x.^2-2).*exp(-x.^2-y.^2)+2./(x.^2+y.^2-2.*(x+y)+3);
%tim gia tri lon nhat cua mang Z va tim vt hang cot tuong ung
[zmax,imax]=max(z)
[zmax,jmax]=max(max(z))
imax=imax(jmax)
[zmin,imin]=min(z)
[zmin,jmin]=min(min(z))
imin=imin(jmin)
%ve mat cong bang surf
subplot(1,2,1); surf(x,y,z)
%Ve 15 duong dong muc
subplot(1,2,2); contour(x,y,z,15);
x0=ginput
f=inline('(x(1).^2-2).*exp(-x(1).^2-x(2).^2)+2./(x(1).^2+x(2).^2-2.*(x(1)+x(2))+3)','x');
for k=1:size(x0,1)
[xn(k,:),fxn(k)]=fminsearch(f,x0(k,:));
end
disp('cuc tieu la');
xn
%tinh dao ham theo huong x,y va ve dthi truong vecto
[dzx,dzy]=gradient(z);
figure(2);
quiver(x,y,dzx,dzy);
%tinh gia tri ham Z bang function
function z=de2a(x,y)
z=8./(x.^4+y.^4+2.*x+2.*y+4)-exp(-x.^2-y.^2+x+y+1);
end
De 3
%tao m file 2 bin n,x de tinh tong cua n so hang dau
function tong=de3(x,n)

Cao Vn Chnh_B1K52
ak=0;
for k=1:n
ak=ak+sin((2*n+1).*x)./(2*n+1);
end
tong=4./pi.*ak;
%tao m file 1 bien x, sai so tol de tinh chuoi
function to=de3b(x,tol)
ak=1;
k=0;
t=0;
while any(abs(ak)>tol)
ak=sin((2*k+1).*x)./(2*k+1);
t=ak+t;
k=k+1;
end
to=t;
clear all;clc;
%phan c
x=linspace(-3*pi,3*pi);
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);
r=(3*sin(x)-sin(3*x))./(3*x)+0.5;
y=r.*cos(phi);
z=r.*sin(phi);
figure(2);
subplot(2,2,1);surf(x,y,r)
%quanh truc y
x1=x.*sin(phi);
y1=r;
z1=x.*cos(phi);
subplot(2,2,2);surf(x1,y1,z1);
%quanh truc Z
x2=r.*cos(phi);
y2=r.*sin(phi);
z2=0.*phi;%neu thay z2=0 thi no khong hien thi hinh ve
subplot(2,1,2);surf(x2,y2,z2);
De 4
function tong=de4(x)
tol=1e-6;
ak=1;
k=0;
s=0;
while any(abs(ak)>tol);
ak=(-1).^k.*x.^(2.*k+1)./factorial(2.*k);
s=ak+s;
k=k+1;
end
tong=s;
%ve do thi
x=linspace(0,10);
figure(1); plot(x,de4(x));
%phan b,tim nghiem thuc cua pt
x=linspace(0.5,10);
f=de4(x)-(x+0.5)/x.^2+2
figure(2);
plot(x,0,'r');hold on; %ve duong oy bang mau do
plot(x,f,'b');
fx=inline('de4(x)-(x+0.5)/x.^2+2');
[x0 y0]=ginput
for k=1:length(x0)
[xn(k),fxn(k)]=fzero(fx,x0(k))
text(xn,fxn,'< nghiem') %chu thich cho vi tri nghiem

Cao Vn Chnh_B1K52
end
xn
fxn
%phan c
%cuc tieu ham dc lap bang function de4
x=linspace(0,10);
f1=de4(x)
figure(3);
plot(x,0,'r');hold on;
plot(x,g,'b');
fx1=inline('de4(x)');
[x1 y1]=ginput
for k=1:length(x1)
[xct(k),yct(k)]=fminsearch(fx1,x1(k))
text(xct,yct,'< cuc tieu') %chu thich cho vi tri cuc tieu
end
xct
yct
%cuc dai
f2=inline('(-1).*de4(x)');
[x2 y2]=ginput
for k=1:length(x2)
[xcd(k),ycd(k)]=fminsearch(f2,x2(k))
text(xcd,(-1).*ycd,'< cuc dai')
end
xcd
-ycd
%cau d
%ve mat tron xoay khi quay duong cong quanh truc x
[x,phi]=meshgrid(0 : pi/15 : 2*pi);
r=de4(x);
y=r.*cos(phi);
z=r.*sin(phi);
figure(2);
subplot(2,2,1);surf(x,y,r)
%quanh truc y
x1=x.*sin(phi);
y1=r;
z1=x.*cos(phi);
subplot(2,2,2);surf(x1,y1,z1);
%quanh truc Z
x2=r.*cos(phi);
y2=r.*sin(phi);
z2=0.*x;%neu thay z2=0 thi no khong hien thi hinh ve
subplot(2,1,2);surf(x2,y2,z2);
1
0

1
0

0
1
0

1
0
1
0

1
0

1
0

1
0 0

0
0 1
0

1
0
1
1
0

1
0

1
0

De 5
%phan a,b
[x,y]=meshgrid(linspace(-0.5,0.5),linspace(-2,2));
f1=cos(x.^2) +3.*y.*sin(2.*x)-2.*x-0.3;
f2=x.^4+2.*x.*y+y.^2+8.*x+y-2;
contour(x,y,f1,[0 0],'r');hold on;
contour(x,y,f2,[0 0],'b');
pt1='cos(x(1).^2) +3.*x(2).*sin(2.*x(1))-2.*x(1)-0.3';

1
0

Cao Vn Chnh_B1K52
pt2='x(1).^4+2.*x(1).*x(2)+x(2).^2+8.*x(1)+x(2)-2';
hpt=['[',pt1,',',pt2,']'];
fx=inline(hpt,'x');
[x0 y0]=ginput;
for k=1:length(x0);
[xn(k,:),fxn(k,:)]=fsolve(fx,[x0(k,:) y0(k,:)]);
end
xn,fxn
fprintf('nghiem cua he phuong trinh la:\n');
disp('
x
,
y');
disp(xn);
xlabel('truc x')
ylabel('truc y')
%cau 3:ve mat cong
[x,y]=meshgrid(linspace(-0.2,0.2),linspace(-2,2));
z1=cos(x.^2)+3.*y.*sin(2.*x)-2.*x;
z2=x.*4+2.*x.*y+y.^2+8.*x+y;
subplot(1,2,1);surf(x,y,z1);
subplot(1,2,2);surf(x,y,z2);
De 6
%phan a,b
clear all;close all;clc;
[x,y]=meshgrid(linspace(-0.3,0.3),linspace(-1,1));
f1=x.^2-10.*x.*y-4.*y.^4;
f2=x.^4+2.*x.*y+y.^2+8.*x+y-0.1;
figure(1);
contour(x,y,f1,[0 0],'r'); hold on;
contour(x,y,f2,[0 0],'b');
pt1='x(1).^2-10.*x(1).*x(2)-4.*x(2).^4';
pt2='x(1).^4+2.*x(1).*x(2)+x(2).^2+8.*x(1)+x(2)-0.1';
hpt=['[',pt1,',',pt2,']'];
fx=inline(hpt,'x');
[x0 y0]=ginput
for k=1:length(x0);
[xn(k,:),fxn(k,:)]=fsolve(fx,[x0(k,:) y0(k,:)]);
end
text(xn(k,1),xn(k,2),'*');
xn
fxn
legend('do thi ham so');
xlabel('truc x')
ylabel('truc y')
%phan c ve mat cong
[x,y]=meshgrid(linspace(-0.2,0.2),linspace(-2,2));
z1=x.^2-10.*x.*y-4.*y.^4;
z2=x.^4+2.*x.*y+y.^2+8.*x+y-0.1;
figure(2);
subplot(1,2,1);surf(x,y,z1);
subplot(1,2,2);surf(x,y,z2);
De 7
%ve do thi phan thuc va phan ao
clear all,close all
[x,y]=meshgrid(linspace(-2,2));
z=x+i.*y;
u=z.^2-(1+i.*sqrt(3)).*z-1+i.*sqrt(3);
subplot(2,1,1);plot(x,real(u));
subplot(2,1,2);plot(y,imag(u));
%phan b,c
%tim nghiem gan dung
figure(2);
contour(x,y,real(u),[0 0],'r');hold on
contour(x,y,imag(u),[0 0],'b');
[x0,y0]=ginput;
z0=x0+i.*y0;
%tim nghiem chinh xac

Cao Vn Chnh_B1K52
f=inline('z.^2-(1+i.*sqrt(3)).*z-1+i.*sqrt(3)','z');
for k=1:size(z0,1)
[zn(k,:),fzn(k,:)]=fsolve(f,z0(k,:));
end
zn
fzn
plot(zn,fzn,'*')
De 8
%ve phan thuc va phan ao
[x,y]=meshgrid(linspace(-2,2)); %bo di cung dc
z=x+i.*y;
u=(cos(z).^2-2).^2+6.^z+4-10.*i;
subplot(2,1,1);plot(x,real(u));
subplot(2,1,2);plot(y,imag(u));
%phan b,c tim nghiem gan dung va chinh xac nghiem
figure(2);
contour(x,y,real(u),[0 0],'r');hold on
contour(x,y,imag(u),[0 0],'b');
[x0,y0]=ginput;
z0=x0+i.*y0;
f=inline('(cos(z).^2-2).^2+6.^z+4-10.*i','z');
for k=1:size(z0,1)
[zn(k,:),fzn(k,:)]=fsolve(f,z0(k,:));
end
zn,fzn
De 9
% tinh cac duong tiem can
clear all;close all;clc;
syms x;
f1=abs(x^3+5*x+3);
f2=5-2*x-x^2;
f=f1/f2;
fprintf('Cac giao diem voi truc hoanh\n')
x0=double(solve(f,x));
disp(x0);
fprintf('Cac tiem can dung\n');
xtcd=solve(f2);
for k=1:length(xtcd);
fprintf('
x=');
disp(xtcd(k));
end
fprintf('Tiem can ngang ben trai\n');
xtcnt=limit(f,-inf);
fprintf('
yt='); disp(xtcnt);
fprintf('Tiem can ngang ben phai\n');
xtcnp=limit(f,inf);
fprintf('
yp='); disp(xtcnp);
fprintf('Tiem can xien\n');
a1=limit(f/x,-inf);
b1=limit(f-a1*x,-inf);
tcx1=a1*x+b1;
fprintf('
y='); disp(tcx1);
a2=limit(f/x,+inf);
b2=limit(f-a2*x,+inf);
tcx2=a2*x+b2;
fprintf('
y=');disp(tcx2);
%ve cac duong tiem can
ezplot(f,[-15,15]);
ylim([-30 30]);
hold on
% giao voi ox
plot(x0,subs(f,x,x0),'ro');
% Ve tiem can dung
ycd=[-30 30];
xcd1=double(xtcd(1)*ones(size(ycd)));

Cao Vn Chnh_B1K52
xcd2=double(xtcd(2)*ones(size(ycd)));
plot(xcd1,ycd,'g',xcd2,ycd,'g')
% Ve tiem can ngang
%tiem can ngang ko xd
% Ve tiem can xien
xcx1=linspace(-15,double(xtcd(2)));
ycx1=subs(tcx1,x,xcx1);
plot(xcx1,ycx1,'k')
xcx2=linspace(double(xtcd(1)),15);
ycx2=subs(tcx2,x,xcx2);
plot(xcx2,ycx2,'k')
x11=linspace(-15,15);
plot(x11,0);
legend('Do thi ham f(x)','Giao voi Ox','Tiem can dung 1','Tiem can dung 2','Tiem can xien',0)
3
2
a
b
s
(
x
+
5x
+
3
)
/(
5
2x
x
)

3
0
D
oth
ih
a
m
f
(
x
)
G
ia
ov
o
iO
x
T
ie
m
c
a
nd
u
n
g1
T
ie
m
c
a
nd
u
n
g2
T
ie
m
c
a
nx
ie
n

2
0

1
0

1
0

2
0

3
0
1
5

1
0

0
x

De10
%phan a,b
clear all;close all,clc;
xi=-0.4:0.2:1.6;
yi=[0.86,0.92,0.95,0.93,0.86,0.76,0.64,0.53,0.44,0.36,0.30];
heso=polyfit(xi,1./asin(yi),2);
a=heso(1)
b=heso(2)
c=heso(3)
x1=-1:0.01:2;
y1=sin(1./polyval(heso,x1));
plot(xi,yi,'*r',x1,y1);
legend('diem thuc nghiem','ham noi suy')
%phan c
f=inline('-sin(1./polyval(heso,x))','x','heso');
[x0,y0]=ginput;
for k=1:length(x0)
[xn(k),fxn(k)]=fminsearch(f,x0(k),[],heso);
end
text(xn,-fxn,'<cuc dai')
xn
fxn

1
0

1
5

Cao Vn Chnh_B1K52
1
d
ie
mth
u
c n
g
h
ie
m
h
a
mn
o
i su
y

<
c
u
cd
a
i
0
.9
0
.8
0
.7
0
.6
0
.5
0
.4
0
.3
0
.2
-1

-0
.5

0
.5

1
.5

De11
%phan a,b
%tim he so va ve hinh
clear all;close all;clc;
xi=0:0.2:1.6;
yi=[1.00,1.13,1.16,1.14,1.12,1.11,1.12,1.14,1.17];
hs=polyfit(xi.^2,(exp(xi)./yi).^2,2);
a=hs(1),b=hs(2),c=hs(3),
x1=0:0.01:2;
y1=exp(x1)./sqrt(polyval(hs,x1.^2));
plot(xi,yi,'*r',x1,y1);ylim([1,2]);
legend('diem thuc nghiem','ham noi suy')
%phan c
%cuc dai
f=inline('-exp(x)./sqrt(polyval(hs,x.^2))','x','hs');
[x0,y0]=ginput;
for k=1:length(x0)
[xn(k),fxn(k)]=fminsearch(f,x0(k),[],hs);
end
text(xn,-fxn,'<cuc dai')
xn
fxn
%cuc tieu
f1=inline('exp(x)./sqrt(polyval(hs,x.^2))','x','hs');
[x11,y11]=ginput;
for k=1:length(x11)
[xn1(k),fxn1(k)]=fminsearch(f1,x11(k),[],hs);
end
text(xn1,fxn1,'<cuc tieu')
xn1
fxn1
2
d
ie
m
th
u
cn
g
h
ie
m
h
a
m
n
o
is
u
y

1
.9
1
.8
1
.7
1
.6
1
.5
1
.4
1
.3
1
.2
<
c
u
cd
a
i
<
c
u
ct
ie
u

1
.1
1
0

0
.2

0
.4

0
.
6

0
.
8

1
.
2

1
.
4

1
.6

1
.8

Cao Vn Chnh_B1K52
De 12
clear all,close all,clc
%phan a,b
%tim he so va ve hinh
xi=-.2:.2:1.2;
yi=[-.54 -.33 -.16 -.01 .11 .23 .41 2.61];
hs=polyfit(xi,yi.*(2.*xi.^2-3)./cos(xi)+0.2.*exp(xi),2);
a=hs(1),b=hs(2),c=hs(3),
x1=-0.2:0.01:1.2;
y1=(polyval(hs,x1)-0.2.*exp(x1)).*cos(x1)./(2.*x1.^2-3);
plot(xi,yi,'*r',x1,y1);hold on
plot(x1,0);
legend('diem thuc nghiem','ham noi suy')
%phan c
%tim ko diem
f=inline('(polyval(hs,x)-0.2.*exp(x)).*cos(x)./(2.*x.^2-3)','x','hs');
[x0,y0]=ginput;
for k=1:length(x0)
[xn(k),fxn(k)]=fsolve(f,x0(k),[],hs);
end
text(xn,fxn,'<khong diem')
xn
fxn
3
d
ie
m
th
u
c n
g
h
ie
m
h
a
m
n
o
is
u
y

2
.5
2
1
.5
1
0
.5
0

<
k
h
o
n
gd
ie
m

-0
.5
1
0
.2

0
.2

0
.4

0
.6

0
.8

De 13
clear all,close all,clc
%noi suy da thuc va noi suy spline
xi=1.5:0.25:3.5;
yi=[0.52 0.05 -0.38 -0.42 -0.01 0.35 0.14 -0.28 -0.09];
x1=linspace(1,4);
spl=spline(xi,yi); y1=ppval(spl,x1)
hs=polyfit(xi,yi,5); y2=polyval(hs,x1)
plot(xi,yi,'+m',x1,y1,'r',x1,y2,'b');hold on
plot(x1,0);ylim([-0.52,1]);
legend('diem thuc nghiem','noi suy spline','noi suy da thuc');
%phan b
%tinh ko diem,cuc tri bang ham noi suy spline
%tinh ko diem
f11=inline('ppval(spl,x)','x','spl');
[x01,y01]=ginput;
for k=1:length(x01)
[xn11(k),fxn11(k)]=fsolve(f11,x01(k),[],spl);
end
text(xn11,fxn11,'*');
% tinh cuc tri bang phuong phap noi suy spline
% cuc tieu
[x11,y11]=ginput;
for k=1:length(x11)

1
.2

Cao Vn Chnh_B1K52
[xn12(k),fxn12(k)]=fminsearch(f11,x11(k),[],spl);

end
text(xn12,fxn12,'o');
%cuc dai
f12=inline('-ppval(spl,x)','x','spl');
[x21,y21]=ginput;
for k=1:length(x21)
[xn21(k),fxn21(k)]=fminsearch(f12,x21(k),[],spl);
end
text(xn21,-fxn21,'<');
%phan c
%tinh tich phan cua ham noi suy spline
sp=fnint(spl);
I2=ppval(sp,3.5)-ppval(sp,1.5)
1
die
mth
uc n
gh
iem
no
i suy splin
e
no
i suy d
athu
c

0.5

-0
.5
1

1.5

2.5

3
.5

De14
clear all;close all;clc
%dung noi suy da thuc va noi suy spline
xi=0:4:44;
yi=[82.3 74.3 67.6 62.5 58.1 54.3 51.2 48.6 46.1 43.9 41.9 40.1];
spl=spline(xi,yi);
hs=polyfit(xi,yi,5);
x1=linspace(0,44);
plot(xi,yi,'+r',x1,ppval(spl,x1),'r',x1,polyval(hs,x1),'y');
%tinh dien tich theo noi suy da thuc va spline trong khoang [0 44]
ff=inline(vectorize(poly2sym(hs)));
%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
plot(x1,0);
f=inline('ppval(spl,x)-55.7','x','spl');
[x0,y0]=ginput;
for k=1:length(x0)
[xn(k),fxn(k)]=fsolve(f,x0(k),[],spl);
end
text(xn,fxn,'o')
fprintf('nghiem cua no la\n');
disp(xn)

De 15

Cao Vn Chnh_B1K52
%giong bai27, phan cac bai thuc tap
% ve danh gioi va tinh dien tich.
clear all,close all;clc
syms x;
y1=30.*sin(x.^2-0.25);
y2=(x.^4+1)./log(x+1)-10;
f=inline(vectorize(y1-y2));
figure(1);
ezplot(y1,[0,2]);hold on %ve ranh gioi
ezplot(y2,[0 2]);ylim([-30,30])
[x0,y0]=ginput; %tim nghiem chinh xac cua pt f=0 de tinh dt
for i=1:length(x0)
[xn(i),fxn(i)]=fsolve(f,x0(i));
end
xn(1)
xn(2)
s=abs(quad(f,xn(1),xn(2))) %tinh dien tich voi can xn(1),xn(2)
%phan b
% Ve doan thang
dk=2;%diu kin ban dau dat ra
xm=1.15;%duong chia ranh gioi lay bat ki
ss=1e-4;
while (dk==2)
xm=xm+ss;
s1=quad(f,xn(1),xm);
if (abs(s/2)-abs(s1))<=ss;
dk=0; %den khi bang 0 thi suat ra gia tri xm chia doi hinh
end
end
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
s2=quad(f,xm,xn(2))
fprintf('
Sai so S2-S1=');
disp(s2-s1)
%gioi han lai hinh, chi trong khoang tu giao dim xn(1) den xn(2)
figure(2);
ezplot(y1,[xn(1) xn(2)]);hold on
ezplot(y2,[xn(1) xn(2)]);ylim([-30,30]);hold on
yi=-2:0.01:2;
plot(xm,yi,'k')%ve duong chia doi xm
% % To mau vung 1,2
%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
x3=linspace(xm,xn(2),20); y3=30.*sin(x3.^2-0.25);
x4=linspace(xn(2),xm,20); y4=(x4.^4+1)./log(x4+1)-10;
fill([x1,x2],[y1,y2],'r',[x3,x4],[y3,y4],'b')

De 16
%de nay giong de15, bai 27, khac o cho hinh gioi han de bai cho ta
%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
syms x;
y1=-(x.^2+x+1)./(2.*x-1)+0.3;
y2=(x.^4+1)./log(x+1)-10;
f=inline(vectorize(y1-y2));
figure(1);
ezplot(y1,[0.7 1.2]);hold on
ezplot(y2,[0.7 1.2]);ylim([-10,10])
s=abs(quad(f,0.7,1.2))
%phan b

Cao Vn Chnh_B1K52
% 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
while (dk==1)
xm=xm+ss;
s1=quad(f,0.7,xm);
if (abs(s/3)-abs(s1))<=ss;
dk=0;
end
end
fprintf('Duong chia hinh thanh 2 phan co S bang nhau x=');
disp(xm)
%ve doan thang 2 x=xm1 de chia hinh thanh 3 phan =
dk=2;xm1=1;ss=1e-4;
while (dk==2)
xm1=xm1+ss;
s2=quad(f,xm,xm1);
if (abs(s/3)-abs(s2))<=ss;
dk=0;
end
end
fprintf('Duong chia hinh thanh 2 phan co S bang nhau x=');
disp(xm1)
%tinh dt 3 phan vua chia de so sanh
s1=quad(f,0.7,xm)
s2=quad(f,xm,xm1)
s2=quad(f,xm1,1.2)
%ve lai ranh gioi
figure(2);
ezplot(y1,[0.7 1.2]);hold on
ezplot(y2,[0.7 1.2]);ylim([-10,10]);hold on
%ve 2 duong thang chia hinh thanh 3 phan =
yi=-8:0.01:-2;
plot(xm,yi,'k',xm1,yi,'k')
% % To mau vung 1,2,3
x1=linspace(0.7,xm,20); y1=-(x1.^2+x1+1)./(2.*x1-1)+0.3;
x2=linspace(xm,0.7,20); y2=(x2.^4+1)./log(x2+1)-10;
%vung 2
x21=linspace(xm,xm1,20); y21=-(x21.^2+x21+1)./(2.*x21-1)+0.3;
x22=linspace(xm1,xm,20); y22=(x22.^4+1)./log(x22+1)-10;
%vung 3
x3=linspace(xm1,1.2,20); y3=-(x3.^2+x3+1)./(2.*x3-1)+0.3;
x4=linspace(1.2,xm1,20); y4=(x4.^4+1)./log(x4+1)-10;
fill([x1,x2],[y1,y2],'r',[x21 x22],[y21 y22],'c',[x3,x4],[y3,y4],'b')
(x4
+
1
)/lo
g
(x+
1
)-1
0
1
0
8
6
4
2
0
-2
-4
-6
-8
-1
0
0
.7

0
.7
5

De 17
%phan a:tinh va ve do thi

0
.8

0
.8
5

0
.9

0
.9
5
x

1
.0
5

1
.1

1
.1
5

1
.2

Cao Vn Chnh_B1K52
%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;
f=inline('(cos(t)+1)./(t.^2+1)','t');
xi=linspace(0,1.5);
for k=1:length(xi)
yi(k)=sin(2.*xi(k)).*quad(f,0,xi(k).^2+1)-xi(k).^2-0.2;
end
plot(xi,yi);
grid on;
%cau b,c:tim nghiem va tinh cuc tri,ve
%tim nghiem
sp=spline(xi,yi);
fx=inline('ppval(sp,x)','x','sp');
[x0,y0]=ginput
for k=1:length(x0)
[xn(k),fxn(k)]=fsolve(fx,x0(k),[],sp);
end
text(xn,fxn,'o')
fprintf('cac nghiem cua pt:');
disp(xn)
%cuc tieu
[x01,y01]=ginput
for k=1:length(x01)
[xct(k),fct(k)]=fminsearch(fx,x01(k),[],sp);
end
text(xct,fct,'*')
xct
%cuc dai
fx1=inline('-ppval(sp,x)','x','sp');
[x02,y02]=ginput
for k=1:length(x02)
[xcd(k),fcd(k)]=fminsearch(fx1,x02(k),[],sp);
end
text(xcd,-fcd,'*')
%phan d
syms a t
xi=linspace(0,1.5);
for k=1:length(xi)
a=xi(k); %dat cai nay la do trong de co an a
f1=inline(vectorize((cos(t)+1)./(t.^2+a.^2+1)));
%tinh lan luot cac gt cua ham ung voi tung gt xi
yi1(k)=quad(f1,0,xi(k).^2+1)-xi(k).^2-0.2;
end
figure(2);
plot(xi,yi1);

De 18

%phan a:tinh va ve do thi


clear all,close all,clc;
f=inline('(cos(t)+1)./(t.^2+1)','t');
xi=linspace(0,2);
for k=1:length(xi)
yi(k)=cos(2.*xi(k)).*quad(f,0,xi(k).^2+1)-xi(k).^2-0.8;
end
plot(xi,yi);
grid on;
%cau b,c:tim nghiem va tinh cuc tri,ve
%tim nghiem
sp=spline(xi,yi);
fx=inline('ppval(sp,x)','x','sp');
[x0,y0]=ginput
for k=1:length(x0)
[xn(k),fxn(k)]=fsolve(fx,x0(k),[],sp);
end
text(xn,fxn,'o')

Cao Vn Chnh_B1K52
fprintf('cac nghiem cua pt:');
disp(xn)
%cuc dai
fx1=inline('-ppval(sp,x)','x','sp');
[x01,y01]=ginput
for k=1:length(x01)
[xcd(k),fcd(k)]=fminsearch(fx1,x0(k),[],sp);
end
text(xcd,-fcd,'*')
%phan d
syms a t
xi=linspace(0,2);
for k=1:length(xi)
a=xi(k); %dat cai nay la do trong de co a
f1=inline(vectorize((cos(t)+1)./(t.^2+a.^2+1)));
yi1(k)=quad(f1,0,xi(k).^2+1)-xi(k).^2-0.2;
end
figure(2);
plot(xi,yi1);grid on;

De 19
%bai toan cauchy
%caua: giai ptvp va ve dthi
D=inline('[2*u(2)-u(1);3*u(1)+u(3)-u(1)*u(3);4*u(1)*u(2)-u(3)]','t','u');
[t,u]=ode45(D,[0 5],[-5 10 0])
%[0 5] ung voi dk doi so vao t;[-5 10 0] ung voi dk ban dau:
%x(0),y(0),z(0)
plot(t,u(:,1),'r',t,u(:,2),'b',t,u(:,3),'y');shg;
legend('x(t)','y(t)','z(t)');
y1=u(:,1);
%liet ke cac hang ung voi cot1 cua MT u, tuc la liet ke gt cua x(t),ta
%dat ham x(t) do la y1
%Cau b: tinh ko diem va cuc tri
spl=spline(t,y1);
f11=inline('ppval(spl,x)','x','spl');
[x01,y01]=ginput;
for k=1:length(x01)
[xn11(k),fxn11(k)]=fsolve(f11,x01(k),[],spl);
end
text(xn11,fxn11,'*');
% tinh cuc tri bang phuong phap noi suy spline
% cuc tieu
[x11,y11]=ginput;
for k=1:length(x11)
[xn12(k),fxn12(k)]=fminsearch(f11,x11(k),[],spl);
end
text(xn12,fxn12,'o');
%cuc dai
f12=inline('-ppval(spl,x)','x','spl');
[x21,y21]=ginput;
for k=1:length(x21)
[xn21(k),fxn21(k)]=fminsearch(f12,x21(k),[],spl);
end
text(xn21,-fxn21,'<');
%Cau c: chu y chua lam dc

Cao Vn Chnh_B1K52

De 20

%cach 1
%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)
subplot(2,1,1);plot(x,y(:,1),'r');
subplot(2,1,2);plot(x,y(:,2),'b');
%cau c:
figure(2);
plot(x,y(:,1),'y',x,y(:,2),'g');grid on
%noi suy tung duong y,y'
sp1=spline(x,y(:,1));gtsp1=ppval(sp1,x);
sp2=spline(x,y(:,2));gtsp2=ppval(sp2,x);
[x0 y0]=ginput
[xn yn]=fsolve(@(x)(ppval(sp1,x)-ppval(sp2,x)),x0)
%for k=1:length(x0)
%[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???????????????
%end
%xn,yn
%cau c tinh dt bao boi duong bien tren
sp=spline(x,y(:,2)-y(:,1));
tp=fnint(sp);
dt=ppval(tp,xn)-ppval(tp,0)
%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
%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)
subplot(2,1,1);plot(x,y(:,1),'r');
subplot(2,1,2);plot(x,y(:,2),'b');
%cau c:
figure(2);
plot(x,y(:,1),'y',x,y(:,2),'g');grid on
%noi suy tung duong y'-y
sp=spline(x,y(:,2)-y(:,1));
% Tim khong diem 2ham giao nhau tu ham noi suy spline sp
%x2=0:.01:5
f1=inline('ppval(sp,x)','x','sp');
[x0,y0]=ginput
for k=1:length(x0)
%chu y vong lap for o day dung cho ham noi suy
%khac voi vong for dung voi ham so!!!!bai27
[xn(k),yn(k)]=fsolve(f1,x0(k),[],sp);
end
xn(1)
xn(2)

Cao Vn Chnh_B1K52
% Tinh tich phan ham noi suy spline bang "fnint" va ham goc bang "quad"
tpsp=fnint(sp)
dt=abs(ppval(tpsp,xn(1))-ppval(tpsp,xn(2)))

De 21
Cach 1
%cau a: %giai ptvp tim y,y',y''va ve chung
f=inline('[y(2);y(3);8*x-y(1)^2*y(2)-y(1)*y(3)]','x','y')
[x y]=ode45(f,[0 pi/2],[0 2 5]);
plot(x,y(:,1),'r',x,y(:,2),'y',x,y(:,3),'b');grid on;
xlabel('x');ylabel('y(x),dy/dx,d2y/dx')
legend('y(x)','dy/dx','d2y/dx');%chu thich cho cac duong da ve
%cau b:tim nghiem pt:y(x)=1.5
sp=spline(x,y(:,1)-1.5);
%x2=linspace(0,pi/2);
y=ppval(sp,x);
figure(2);plot(x,y);grid on
% Tim khong diem 2ham giao nhau tu ham noi suy spline sp
f1=inline('ppval(sp,x)','x','sp');
[x0,y0]=ginput
for k=1:length(x0)
%chu y vong lap for o day dung cho ham noi suy
%khac voi vong for dung voi ham so!!!!bai27
[xn(k),yn(k)]=fsolve(f1,x0(k),[],sp);
end
xn,yn

Cach 2
%de21 cach 2
%cau a: %giai ptvp tim y,y',y''va ve chung
f=inline('[y(2);y(3);8*x-y(1)^2*y(2)-y(1)*y(3)]','x','y')
[x y]=ode45(f,[0 pi/2],[0 2 5]);
plot(x,y(:,1),'r',x,y(:,2),'y',x,y(:,3),'b');grid on;
xlabel('x');ylabel('y(x),dy/dx,d2y/dx')
legend('y(x)','dy/dx','d2y/dx');%chu thich cho cac duong da ve
%cau b:tim nghiem pt:y(x)=1.5
sp=spline(x,y(:,1)-1.5);
y=ppval(sp,x);
figure(2);plot(x,y);grid on
[x0 y0]=ginput
[xn yn]=fsolve(@(x)ppval(sp,x),x0)

De 22
Cach 1
%cach 1 nay khong chay duoc???????????
clear all;close all;clc;
hpt='Dy1=y2,Dy2=-9.8*sin(y1)-y2';
dkb='y1(0)=0.5,y2(0)=0';
dsb=dsolve(hpt,dkb,'x')
y11=simple(dsb.y1)
y22=simple(dsb.y2)
figure(1);
subplot(2,1,1);
ezplot(y11,[0 3]);grid on
subplot(2,1,2)
ezplot(y22,[0 3]);grid on

Cach 2
%cach 2
clear all;close all;clc;
f=inline('[y(2);-y(2)-9.8*sin(y(1))]','t','y')
[t y]=ode45(f,[0 3],[0.5 0])
plot(t,y(:,1),'r',t,y(:,2),'b');grid on;
xlabel('t');ylabel('teta');
legend('teta(t)','dteta/dt')
%cau b: tim 0 diem va cuc tri ham:y=teta(t)

Cao Vn Chnh_B1K52
sp=spline(t,y(:,1));
ysp=ppval(sp,t)
figure(2);plot(t,ysp)
f=inline('ppval(sp,t)')
[t0 y0]=ginput
for i=1:length(t0)
[tn yn]=fsolve(f,t0(i),[],sp)
end
tn,yn
%tim cuc tieu ham noi suy cua dx/dt hay ham noi suy cua y(:,1)
[t1 y1]=ginput
for i=1:length(t1)
[tct yct]=fminsearch(f,t1(i),[],sp)
end
text(tct,yct,'<cuc tieu day ne !!!')
tct,yct
%tim cuc dai ham noi suy cua dx/dt hay ham noi suy cua y(:,1)
f1=inline('-1*ppval(sp,t)')
[t2 y2]=ginput
for i=1:length(t2)
[tcd ycd]=fminsearch(f1,t2(i),[],sp)
end
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
y=x-7/2+sqrt(4.*x.^2+2.*x+7);
ezplot(y,[-6 6]);
%Cau b:tim cuc tri
f=inline('xi-7/2+sqrt(4.*xi.^2+2.*xi+7)','xi')
[x0 y0]=ginput;
for k=1:length(x0)
[xn(k),fxn(k)]=fminsearch(f,x0(k));
end
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')
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
dy=diff(y,x);
dyy=sqrt(1+dy^2);
fdx=inline(vectorize(dyy),'x');
%vectorize(hoac char):de chuyen tu sym sang vecto
I=quad(fdx,-6,6)
%Cau e:
%quay quanh truc x
figure(2);
[xi phi]=meshgrid(-6:0.7:6,0:pi/10:2*pi);
ri=xi-7/2+sqrt(4*xi.^2+2*xi+7);
yi=ri.*cos(phi);
zi=ri.*sin(phi);
surf(xi,yi,zi);
%tinh dien tich cua vat the xoay quanh ox
%ban kinh vat the chinh la truc oy
disp('dien tich xung quanh');
S=int(2*pi*y,x,-6,6);
Sxq=double(S)
disp('dien tich toan phan');

Cao Vn Chnh_B1K52
R1=subs(y,x,-6);
R2=subs(y,x,6);
Stp=Sxq+pi*double(R1^2+R2^2)
disp('the tich khoi tron xoay');
V=double(pi*int(y^2,x,-6,6))

15
10
5
0
-5
-10
-15
20
10

10
5

-10

-5
-20

-10

De 24
Chu y: Cau b khong bit lam
clear all;close all;clc
%Cau a: ve do thi
[x,y]=meshgrid(linspace(-10,10));
f1=x.^2+2.*y.^2-3;
f2=2.*x.^2-x.*y-5.*x+1;
contour(x,y,f1,[0 0],'r');hold on;
contour(x,y,f2,[0 0],'b')
%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;
F=de24b(A,B,C)
h=ezplot(F);set(h,'color','r')
%cau c
%tinh chu vi va dien tich
disp('dien tich ');
f=inline('sqrt((3-x.^2)./2)','x')
S1=quad(f,-sqrt(3),sqrt(3)); %can nay lay o dau ra nhi ???
dt=2*double(S1)
%do dai cung
disp('chu vi');
syms x;
y1=sqrt((3-x.^2)./2);
dy=diff(y1,x);
dyy=sqrt(1+dy^2);
fdx=inline(vectorize(dyy),'x'); %bien dyy ve dang vecto
I=quad(fdx,-sqrt(3),sqrt(3))
cvi=I*2

De 25
Cau b khong chay dc dau?????
clear all;close all;clc

Cao Vn Chnh_B1K52
%ve do thi
[x,y]=meshgrid(linspace(-10,10));
f1=2.*x.^2-4.*x.*y-y.^2+8;
f2=x.^2+2.*x.*y+y.^2+8.*x+y;
contour(x,y,f1,[0 0],'r');hold on;
contour(x,y,f2,[0 0],'b')
%Cau b:lap function chuyen duong cong bac 2 ve dang chinh tac
%cau b nay ko chay dc,ko hiu gi hit
A1=[2,-2;-2,-1];B1=[0,0];C1=8
F1=standardform(A1,B1,C1);
figure(2);h1=ezplot(F1);grid on;hold on
A2=[1,1;1,1];B2=[8,1];C2=0
F2=standardform(A2,B2,C2);
h2=ezplot(F2);set(h2,'color','r')
De 26
Cau a: Cach 1
%bai toan ve phuong phap ban
clear all;close all;clc;
hpt='Dy1=y2,Dy2=sin(x)-y1-y2';
dkb='y1(pi/2)=1,y1(0)=0';
dsb=dsolve(hpt,dkb,'x')
y11=simple(dsb.y1) %nghiem y
y22=simple(dsb.y2) %nghiem y'
figure(1);
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'
dy=inline('[y(2);sin(t)-y(1)-y(2)]','t','y')
[t y]=ode45(dy,[0 5],[0 0]);
plot(t,y(:,1),'r',t,y(:,2),'b');grid on
%cau b: giai ptvp voi dk bien

De 27
R1=1;R2=1;R3=1;R4=1;R5=1;
syms V
%cau a dung DL Kirchoff lap hpttt va giai no

Cao Vn Chnh_B1K52
A=[R1 0 R3 0 0;0 R2 -R3 R4 0;0 0 0 R4 -R5;1 -1 -1 0 0;0 1 0 -1 -1];
b=[V;0;0;0;0]
I=A\b
%cau b ve dthi dong ra I
ezplot(I(5),[-5,5]);grid on

Hay xem lai cach giai PTVP nhe, chua hiu dau day !!!!!!!!!!!!!
Thi tot nhe !
OK !

You might also like