Cao 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
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')
1
Cao V¨n ChÝnh_B1K52
gtext('s10') ;gtext('s')
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
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???
2
Cao V¨n ChÝnh_B1K52
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);
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
3
Cao V¨n ChÝnh_B1K52
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
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
4
Cao V¨n ChÝnh_B1K52
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
L p bi u th c tính Pn: còn tính t P0-ậ ể ứ ừ P5 làm t ng t nh bài 6bươ ự ư ở
function t=bai11a(n,x)
if (n<0)|n~=round(n)
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)
5
Cao V¨n ChÝnh_B1K52
%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
% 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))
6
Cao V¨n ChÝnh_B1K52
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
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);
7
Cao V¨n ChÝnh_B1K52
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
T=x;
else
T=2.*x.*bai16(n-1,x)-bai16(n-2,x);
end
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
8
Cao V¨n ChÝnh_B1K52
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');%
%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))
9
Cao V¨n ChÝnh_B1K52
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)
%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)
10
Cao V¨n ChÝnh_B1K52
[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
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
11
Cao V¨n ChÝnh_B1K52
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');
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]
12
Cao V¨n ChÝnh_B1K52
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);
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
13
Cao V¨n ChÝnh_B1K52
-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);
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
14
Cao V¨n ChÝnh_B1K52
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)');
y=inline('(x-1.5).^2-1-exp(-log(x).^2)');%hoac f=inline(vectorize(y1-y2))
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))
%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')
15
0.5 1 1.5 2 2.5
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Cao V¨n ChÝnh_B1K52
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
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');
16
Cao V¨n ChÝnh_B1K52
[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
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
17
Cao V¨n ChÝnh_B1K52
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]);
Tổng hợp code các đề thi Matlab
De 1:
clc;close all;clear all
%cau a
%luoi 41 chia deu
[x,y]=meshgrid(linspace(-2,2,41));
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
18
Cao V¨n ChÝnh_B1K52
%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)
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)
19
Cao V¨n ChÝnh_B1K52
%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
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
20
0
5
10
-10
0
10
0
5
10
-10
0
10
0
5
10
-10
0
10
-10
-5
0
5
10
-10
-5
0
5
10
-1
0
1
Cao V¨n ChÝnh_B1K52
%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);
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';
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);
21
Cao V¨n ChÝnh_B1K52
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
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
22
Cao V¨n ChÝnh_B1K52
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)));
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)
23
-15 -10 -5 0 5 10 15
-30
-20
-10
0
10
20
30
x
abs (x
3
+5 x+3)/(5-2 x-x
2
)
Do thi ham f(x)
Giao voi Ox
Tiem can dung 1
Tiem can dung 2
Tiem can xien
-1 -0.5 0 0.5 1 1.5 2
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
<cuc dai
diem thuc nghiem
ham noi suy
Cao V¨n ChÝnh_B1K52
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
24
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2
<cuc dai
<cuc tieu
diem thuc nghiem
ham noi suy
Cao V¨n ChÝnh_B1K52
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
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);
25