matlab四合一(dream)(6)

2019-04-22 19:42

x=linspace(0,500,500); imagesc(x,x,atan(0,1*M)); axis xy

M=julia(.35,-.297491+i*0.641051,100,250); figure(2);

x=linspace(0,250,250); imagesc(x,x,atan(0,1*M)); axis xy

function n=escapeVelocity(z0,c,k,v) n=0; z=z0;

while abs(z)<=2&n

第四题

目的:

6) 练习高级功能,如数据结构,图像,动画等。

题目说明:所有的题目代码写入相应的脚本中,如果题目没有要求特定的脚本文件名,自行定义一个即可。

1.随机变量生成:

生成一个500个随机数的行向量(使用randn函数),通过计算使之变成一个均值为2,方差为5的随机数向量。之后,使用mean和std函数验证你所生成的随机数向量是否均值和方差接近2和5。

% normalRand

% homework 4 problem 1, making a vector of 500 random numbers that are % normally distributed with mean 2 and standard deviation 5 r=randn(500,1)*5+2;

% verify that the mean and std are close to 2 and 5 disp(['Mean: ' num2str(mean(r))]);

disp(['Standard Deviation: ' num2str(std(r))]);

2.硬币投掷试验:

建立一个脚本命名为coinTest.m,我们来模拟投掷硬币的正面概率问题,我们知道,如果硬币质量均匀,投掷次数足够多,那么两面出现的概率应该为50%,因此首先我们应该设定足够多的投掷次数,例如5000次。记录投出正面的次数,计算正面出现的概率,然后绘制曲线。每次投掷的结果可以用rand和round来模拟,假设1为正面,0则为背面。每次

投掷的结果累积和概率计算当然可以使用一个循环来完成。更好的办法是使用函数cumsum。另外把期望的0.5那条线也画出来以作对比。完成之后多运行几次,看看随着投掷次数的增加是不是正面出现的概率接近0.5。你也可以设置更多的投掷次数,例如下图是我做了10000次的结果。

% coinTest

% generate 1000 random numbers r=rand(5000,1);

% round them so 1 means heads r=round(r);

% count up the number of heads and divide by the count count=1:length(r);

headProb=cumsum(r)./count'; % plot the probability of heads figure

plot(count,headProb,'k','linewidth',1.5); hold on

plot([1 length(r)],[.5 .5],'r--');

title('Sample Probability of Heads in n flips of a simulated coin'); xlabel('Number of coin flips'); ylabel('Probability of heads');

legend('Sample Probability','Fair coin');

sample probability of Heads in n flips of a simulated coin1sample probabilityFair coin 0.90.8probability of heads0.70.60.50.4 0100020003000400050006000Number of coin flips70008000900010000

3.cell练习:

通常cell被用来存储字符串,因为对于每个位置的字符串可以使用不同的长度。 1)建立一个3×3的cell,第一列赋值为‘Joe’,‘Sarah’,‘Pat’。第二列包含了他们的last name:‘Smith’,‘Brown’,‘Jackson’。第三列则是他们各自的收入状况:‘$30,000’, ’$150,000’, ‘$120,000’。赋值后显示结果在命令行查看是否正确。

2)Sarah结婚了将她的last name换成了‘Meyers‘,对之前生成的cell进行修改并显示查看。

3)Pat涨了$50,000的工资,修改她的收入,并显示查看。 结果参考图如下: cellPro =

'Joe' 'Smith' '$30,000' 'Sarah' 'Brown' '$150,000' 'Pat' 'Jackson' '$120,000'

cellPro =

'Joe' 'Smith' '$30,000' 'Sarah' 'Meyers' '$150,000' 'Pat' 'Jackson' '$120,000'

cellPro =

'Joe' 'Smith' '$30,000' 'Sarah' 'Meyers' '$150,000' 'Pat' 'Jackson' '$170,000'

% cellProblem

% initializes a cell, puts some values in it, and changes some of them % part a

% initialize the cell c=cell(3,3); % add elements c{1,1}='Joe'; c{2,1}='Sarah'; c{3,1}='Pat'; c{1,2}='Smith'; c{2,2}='Brown'; c{3,2}='Jackson'; c{1,3}=30000; c{2,3}=150000; c{3,3}=120000; % display the cell disp(c)

% part b

% change sarah's name to Meyers c{2,2}='Meyers'; disp(c) % part c

% add 50000 to pat's salary c{3,3}=c{3,3}+50000; disp(c)

4.使用structs:

Structs在处理多样数据时非常有用。例如在命令行输入a=dir,可以看到返回的a就是一个structs,其中包括了很多域,例如name,bytes,isdir等。

1)使用a.name, a.bytes等查看当前路径下文件的情况;

2)编写一个循环代码,遍历a中每一个文件,如果这个文件不是一个文件夹,则输出如下文本“File name contains X bytes”。

运行结果可参考下例:

HW4.doc contains 127488 bytes Pro1.m contains 58 bytes cellPrc.asv contains 161 bytes cellPrc.m contains 198 bytes coinTest.asv contains 374 bytes coinTest.m contains 382 bytes structPrc.asv contains 79 bytes structPrc.m contains 151 bytes ~$HW4.doc contains 162 bytes

~WRL0001.tmp contains 128000 bytes

% displayDir %

% displays a list of the files in the current directory and the size of % each file in bytes function displayDir

% get current directory info a=dir;

% loop through it and display each file name and size for n=1:length(a)

% if it's not a directory, display the information if ~a(n).isdir

disp(['File ' a(n).name ' contains ' num2str(a(n).bytes) ' bytes.']); end end

5.Handels使用:

使用handels来完成一个如上图的图样来: 1)建立一个脚本命名为handelsPractice.m;

2)首先生成一个变量x,0:0.01:2pi,生成y=sin(x); 3)使用figure打开一个绘图界面,使用plot(x,y,‘r’),绘制正弦曲线‘ 4)使用xlim设置x轴范围为0~2pi;

5)使用set和gca(get current axis)来设置xtick属性,值设为[0 pi 2*pi],设置xticklabel属性,值为{‘0’,’1’,’2’};

6)使用set和gca设置ytick属性,值为-1:0.5:1; 7)使用grid on打开网格;

8)使用set和gca设置ycolor属性为green,xcolor属性为cyan,另外设置color属性值为black;

9)使用set和gcf(注意这里是gcf,get current figure),设置figure的color属性为[0.3 0.3 0.3];

10)使用title添加标题“One sine wave from 0 to 2π”,fontsize属性为14,fontweight属性为bold,color属性为white,为了显示为π,使用\\pi来正确显示这些数学符号;

11)设置xlabel和ylabel,fontsize属性为12,color属性分别为cyan和green; 12)如果你想粘贴到word中保持我们调成的这个样式,在copy figure之前,在copy options中figure background color中选择use figure color。至此完成。

% handlesPractice % plot the function x=linspace(0,2*pi,100); y=sin(x); figure;

plot(x,y,'r'); xlim([0 2*pi]); % set the ticks

set(gca,'xtick',[0 pi 2*pi],'xticklabel',{'0','1','2'}); set(gca,'ytick',[-1:.5:1]);

% turn on grid and set axis and background colors grid on;

set(gca,'ycolor','g') set(gca,'xcolor','c'); set(gca,'color','k');

set(gcf,'color',[.3 .3 .3]); % add the labels

title('One sine wave from 0 to

2\\pi','fontsize',14,'fontweight','bold','color','w')

xlabel('X values (in terms of \\pi)','fontsize',12,'color','c'); ylabel('Sin(X)','fontsize',12,'color','g');


matlab四合一(dream)(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2018年郑州市初中毕业年级适应性测试

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: