intTypePromotion=1
ADSENSE

Tổng hợp code các bài tập thực hành Matlab

Chia sẻ: Cao Van Chinh Chinh | Ngày: | Loại File: DOCX | Số trang:36

2.409
lượt xem
418
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Đây là các code một số bài tập mâtb cho dẫn vật lý, rất hay và dễ hiểu mà mình sưa tầm được...

Chủ đề:
Lưu

Nội dung Text: Tổng hợp code các bài tập thực hành Matlab

  1. 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)); 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)); 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
  2. Cao V¨n ChÝnh_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
  3. Cao V¨n ChÝnh_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))
  4. Cao V¨n ChÝnh_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 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
  5. Cao V¨n ChÝnh_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
  6. Cao V¨n ChÝnh_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
  7. Cao V¨n ChÝnh_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))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
  8. Cao V¨n ChÝnh_B1K52 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 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');%
  9. Cao V¨n ChÝnh_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)
  10. Cao V¨n ChÝnh_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)
  11. Cao V¨n ChÝnh_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');
  12. Cao V¨n ChÝnh_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);
  13. Cao V¨n ChÝnh_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);
  14. Cao V¨n ChÝnh_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,'
  15. Cao V¨n ChÝnh_B1K52 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') 1 0.8 0.6 0.4 0.2 0 -. 0 2 -. 0 4 -. 0 6 -. 0 8 - 1 0 .5 1 1.5 2 2 .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
  16. Cao V¨n ChÝnh_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
  17. Cao V¨n ChÝnh_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]); -------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- 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));
  18. Cao V¨n ChÝnh_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)
  19. Cao V¨n ChÝnh_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
  20. Cao V¨n ChÝnh_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); 10 10 5 0 0 - 0 1 10 1 0 10 10 0 5 5 0 - 0 0 1 0 - 0 1 1 0 -1 10 5 10 0 0 5 -5 - 5 - 0 1 - 0 1 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';
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2