《2022年MATLAB习题.pdf》由会员分享,可在线阅读,更多相关《2022年MATLAB习题.pdf(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、MATLAB 习题第一次上机作业: 计算clc;cleary=(2*sin(0 、3*pi)*exp(3)/(1+sqrt(5)*log10(6) 1. 生成等差矩阵a=1,3,5,747,49;在经度 122,128 间生成 70 个等差数列。a=(1:2:50) %生成等差矩阵 a=1,3,5,747,49b=linspace(122,128,70) %在经度 122,128间生成 70 个等差数列2. 生成 34 均匀随机矩阵b;提取 b 的第 3 行第 2 列元素赋值为5,求 b 中最小值、平均值 ;提取 b 的第三列 ;b 右边增添 1 列为 2 3 4,;删除 b 的第二列。b=r
2、and(3,4) %生成 34 均匀随机矩阵 b b(3,2)=5 %提取 b 的第 3 行第 2 列元素赋值为 5 m=min(b(:) %求 b 中的最小值n=mean(b(:) %求 b 中的平均值a=b(:,3) %提取 b 的第三列b(:,5)=2 3 4 %在 b 右边增添 1 列为 2 3 4 b(:,2)= %删除 b 的第二列3. 在区间 x=0,2内做 sin(x)图;按 x、sin(x)、cos(x)存成 3 列到 data、txt 文档;再读取文件data、txt, 用读取的数据做cos(x)图。x=0:0、1*pi:2*pim=sin(x) n=cos(x) plot
3、(x,m,r-*) %做 sin(x)图像a=x;sin(x);cos(x) 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 1 页,共 15 页 - - - - - - - - - - MATLAB 习题save -ascii data 、txt a %按 x、sin(x)、cos(x)存成 3 列到 data、txt 文档q=load(data、txt) %读取文件 data、txt plot(q(:,1),q(:,3) %用读取的 data、txt 做 cos(x)图第二次上机作业:1. 生成 34
4、,-1,1 间的随机矩阵A; 求 A 矩阵中的元素总个数,将 A 中小于 0的元素等于1;再取出 A 中大于 0、2 并小于 0、7 的元素。clc;clear A= -1+(1+1)*rand(3,4) %生成 34,-1,1间的随机矩阵 A length(A(:) %求 A 矩阵中的元素总个数A(A0 、2&A0 就是 x-y 折线图。clc;clear x=0:0、1:2*pi %X在0:0、1: 2内y=cos(x 、2) %求 y=cos(x2 ) 值a=find(y0) %y0 m=x(a); n=y(a); plot(m,n,r-*) %作 x-y折线图3. 首先由计算机随机产生
5、一个1,100 之间的一个整数,然后由用户猜测所产生的这个数。根据用户猜测的情况给出不同的提示,如果猜测的数大于产生的数 ,则显示 “High” ,小于则显示 “Low ” ,等于则显示 “You won !” ,同时退出这个游戏 ,如果用户 7 次还没有猜出 ,则显示 “You lost !” ,并显示出那个随机数。 (用 while 编写 )精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 2 页,共 15 页 - - - - - - - - - - MATLAB 习题clc;clear x=round
6、(1+(100-1)*rand(1,1); %随机产生一个 1,100之间的一个整数n=1; while nx disp (High) end if y2)&(data(:,2)=124)&(lat=28); lat(b); lon(b); plot(lat(b),lon(b), 、) hold on bound axis(120 130 27、5 32、5) title(28N 以北鲐鱼幼鱼地理分布图,fontsize,20) 2. 文件 cjk、nc 为模型输出的长江口netcdf 数据 ,绘制 24 个小时 node=5019所有层平均的水温波动折线图;在地图上绘制cell=10661(
7、第 2 层)与cell=1231(第 7层)的 24 个小时的流场变动图;绘制 time=9 时刻整场的温度等值线 (表层 )、盐度假色彩图(所有层平均 )、流场图 (第 3 层)(标注尺度大小)。clc;clear;clf addpath(genpath(netcdf_tools) nc=netcdf(cjk 、nc,nowrite); wd=nctemp(:,:,5019); pwd=mean(wd); 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 9 页,共 15 页 - - - - - - -
8、- - - MATLAB 习题sj=1:24; plot(sj,pwd) title( 水温波动折线图) xlabel(时间 ) ylabel(平均水温 ) lon=nclonc(10661); lon1=nclonc(1231); lat=nclatc(10661); lat1=nclatc(1231); uu=ncu(:,2,10661); uu1=ncu(:,7,1231); vv=ncv(:,2,10661); vv1=ncv(:,7,1231); lon=repmat(lon,1,24) lon1=repmat(lon1,1,24) lat=repmat(lat,1,24) lat1
9、=repmat(lat1,1,24) myquiver=quiver(lon,lat,uu,vv) hold on bound; myquiver=quiver(lon1,lat1,uu1,vv1) axis(120 125 30 35) title( 流场变动图 ) xlabel(经度 ) 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 10 页,共 15 页 - - - - - - - - - - MATLAB 习题ylabel(纬度 ) t=nctemp(9,1,:) lon2=nclon(:) l
10、at2=nclat(:) xlon=min(lon2):0 、1:max(lon2) xlat=min(lat2):0 、1:max(lat2) xt=griddata(lon2,lat2,t,xlon,xlat,cubic) c h=contour(xlon,xlat,xt) clabel(c,h) hold on bound; axis(120 125 26 35) title( 温度等值线 ) xlabel(经度 ) ylabel(纬度 ) yd=ncsalinity(9,:,:) lon3=nclon(:) lat3=nclat(:) xlon=min(lon3):0 、1:max(l
11、on3) xlat=min(lat3):0 、1:max(lat3) yd=mean(yd) yd=griddata(lon3,lat3,yd,xlon,xlat,cubic) pcolor(xlon,xlat,yd) shading interp 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 11 页,共 15 页 - - - - - - - - - - MATLAB 习题hold on bound axis(120 125 26 35) title( 盐度假色彩图 ) xlabel(经度 ) yla
12、bel(纬度 ) v=ncv(9,3,:) u=ncu(9,3,:) lonc=nclonc(:) latc=nclatc(:) m_sca=0、2 quiver(lonc,latc,u*m_sca,v*m_sca,autoscale,off) hold on bound quiver(120,29,1*m_sca,0,autoscale,off) text(120,3,29,1m/s) axis(120 125 26 35) title( 流场图 ) xlabel(经度 ) ylabel(纬度 ) close(nc); 第五次上机作业: 1. 将文件 cjk、nc 中 24 个时刻、经度【1
13、21 123】 、纬度【 30 33】 、第 3 层流场数据用fprintf 写入文件 ,文件名为“ vector001 、dat” 、精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 12 页,共 15 页 - - - - - - - - - - MATLAB 习题“vector002、dat” 、 、 、 “vector024、dat”;将最后时刻上5 层平均温度与下 3 层平均盐度数据写入Execel文件 ts、xls 里。(每个文件都包括经度、纬度以及数据)。clc;clear; addpath(g
14、enpath(netcdf_tools) ; nc=netcdf(cjk 、nc,nowrite); lon=nclonc(:); lat=nclatc(:); m=find(lon=121&lon=30&lat=33); lon=lon(m); lat=lat(m); layer=3; for time=1:24 u=ncu(time,layer,:); v=ncv(time,layer,:); u=u(m); v=v(m); dat=lon lat u v; fid=fopen(tmp/vector,num2str(time,%3,3d),、dat,wt); fprintf(fid,%8
15、、3f %8、3f %8、3f %8、3fn,dat); fclose(fid); end close(nc); clc;clear; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 13 页,共 15 页 - - - - - - - - - - MATLAB 习题addpath(genpath(netcdf_tools) ; nc=netcdf(cjk 、nc,nowrite); time=24; lon=nclon(:); lat=nclat(:); layer=1:5; temp=nctemp(ti
16、me,layer,:); temp=mean(temp); layer=8:10; sal=ncsalinity(time,layer,:); sal=mean(sal); a=lon lat temp sal; xlswrite(ts 、xls,a) close(nc); 2. 将鲐鱼仔鱼输运轨迹文件larvae_tacking、dat,用 textscan 与 fprintf 改写成能够用load 直接读入的单个文本文件,文件名格式为“larvae_0001、 txt ” 、 “larvae_0002、 txt ” 、 、“larvae_0018、 txt ” 、 ;再在地图上作出仔鱼输运
17、的动图clc;clear; fid1=fopen(larvae_tracking 、dat,rt); dat0=textscan(fid1,%s %s %s %s %s %s,1); for i=2:1000 if feof(fid1) break; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 14 页,共 15 页 - - - - - - - - - - MATLAB 习题end dat1=textscan(fid1,%s %d %d %d %d %s %d,1); n=dat17; data=te
18、xtscan(fid1,%f %f,n); dat1=data1 data2; fid2=fopen(tmp2 /larvae_,num2str(i,%3 、3d),wt); fprintf(fid2,%10 、5f %10、5f n,dat1); fclose(fid2); end fclose(fid1); dat=load(tmp2 /larvae_002); myfig=plot(dat(:,1),dat(:,2), 、); hold on; bound; for i=2:258 data=load(tmp2 /larvae_,num2str(i,%3 、3d); set(myfig,xdata,data(:,1),ydata,data(:,2); pause(0、5); end 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 15 页,共 15 页 - - - - - - - - - -