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
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);
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);
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');
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
%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
% 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))
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);
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;
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
[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
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:')
(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;
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
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
[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');
%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');
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
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);
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');
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;
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
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);
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
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');
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)
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')