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

 

docx36 trang | Chuyên mục: MATLAB | Chia sẻ: dkS00TYs | Lượt xem: 6818 | Lượt tải: 1download
Tóm tắt nội dung Tổng hợp code các bài thực hành Matlab, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
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') 
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
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)
 % 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)
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'); 
 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)) 
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 
%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
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 !

File đính kèm:

  • docxTổng hợp code các bài thực hành Matlab.docx