数学软件作业7次全部整合版.docx
《数学软件作业7次全部整合版.docx》由会员分享,可在线阅读,更多相关《数学软件作业7次全部整合版.docx(16页珍藏版)》请在冰豆网上搜索。
数学软件作业7次全部整合版
第1次作业
功能:
选择语句的运用(以卖苹果为例)
functions=feiyong(n)
ifn>5000
a=15;
elseifn>=2500
a=16;
elseifn>=1000
a=17;
elseifn>=500
a=18;
elseifn>=250
a=19;
elseifn>=100
a=20;
elseifn>=50
a=22;
elseifn>=10
a=24;
elseifn>=0
a=28;
else
fprintf('Wrongnumberofinput.');
end
s=a*n;
第2次作业
【1】功能:
练习输入变量和输出变量的选择(以Fibonacci数列求和为例)
function[F,summ,max1]=fibonacci(m,a)
%FIBONACCI----求给定项数的fibonacci数列的和
%输出变量:
%F---输出项数
%summ---输出和
%max1--
%输入变量:
%m---输入所要求和的项数
%a--输入首项
%该函数是利用fibonacci数列的通项,通过if语句判断求和项数,在用for循环q求累加和,事先判断输入和输出变量个数在相应的执行
ifnargin~=2
error('ToomoreorToolittle')
end
ifa~=1
error('起始项必须是1')
end
F
(1)=1;F
(2)=1;
ifm==1
summ=F
(1);
elseifm==2
summ=F
(1)+F
(2);
else
summ=F
(1)+F
(2);
fori=3:
m
F(i)=F(i-1)+F(i-2);
summ=summ+F(i);
end
end
ifnargout==1
max1=F(m);
elseifnargout==2
F=m;
summ=summ;
end
end
【2】功能:
已知起始值和步长,以及最小终止值,求累加和。
function[ms]=leijia(n,t,k)
%n为起始值,t为步长,k为累加和的上限
s=0;
m=0;
whiles+n<=k
s=s+n;
n=n+t;
m=m+1;
end
第3次作业
功能:
极坐标绘图
symsxyt
t=0:
.01:
2*pi;
x=3*cos(t).^3;
y=3*sin(t).^3;
polar(x,y)
第4次作业
功能:
三维图镂空
clear;clc
[x1,y1]=meshgrid(-pi:
.1:
pi,-pi:
0.1:
pi);
z1=sin(x1).*cos(y1);
[ab]=size(z1);
fori=1:
a
forj=1:
b
ifz1(i,j)<=0.35
x1(i,j)=nan;y1(i,j)=nan;
z1(i,j)=nan;
end
end
end
mesh(x1,y1,z1)
第5次作业
【1】功能:
绘图并标注极值
x=0.5:
0.0001:
2*pi;
y1=3*sin(3*x);
y2=2*exp(-0.5*x).*log10(2*pi*x);
holdon
plot(x,y1,'b',x,y2,'r');
legend('y1','y2');
[x0,y0]=fminsearch('3*sin(3*x)',1);
[x3,y3]=fminsearch('-2*exp(-0.5*x)*log10(2*pi*x)',1)
X=x0:
pi/3:
2*pi;
Y=3*sin(3*X);
plot(X,Y,'*g',x3,-y3,'xg');
text(X
(1),Y
(1),'极小值=-3');
text(X(3),Y(3),'极小值=-3');
text(X(5),Y(5),'极小值=-3');
text(X
(2),Y
(2),'极大值=3');
text(X(4),Y(4),'极大值=3');
text(x3,-y3,'极大值=-0.9694');
holdoff
【2】功能:
绘图并添加相应图例和标注极值
x=1:
0.01:
4;
y1=3*sin(3.*x);
y2=2.*exp(-0.5.*x).*log(2*pi.*x);
plot(x,y1,'r.',x,y2,'g-.')
title('曲线图像')
xlabel('x')
ylabel('y')
legend('y=3*sin(3*x)','y=2*exp(-0.5*x).*log(2*pi*x)')
gtext('极大值')
gtext('极小值')
第6次作业
【1】功能:
建立矩阵对话框,输入相关数据,输出矩阵
a=inputdlg('Juzhen','row')
a1=str2double(a)
b=inputdlg('Juzhen','line')
b1=str2double(b)
fori=1:
a1
forj=1:
b1
c(i,j)=str2double(inputdlg('juzhen'));
end
end
c1=c(1:
a1,1:
b1)
【2】功能:
利用程序编程实现可视化界面和简单操作
%建立图形界面和坐标
f=figure('color','w','position',[4050900500],'numbertitle','off','name','myfig1','toolbar','none','menubar','none')
aa1=axes('position',[0.2.20.70.5],'xcolor','r','ycolor','r','Color','b')
holdon
%建立相关菜单
m1=uimenu('label','Color');
m2=uimenu('label','Linestyle');
m3=uimenu(f,'label','Edit')
m4=uimenu(f,'label','Save')
m11=uimenu(m1,'label','red')
m12=uimenu(m1,'label','blue','callback','Color')
m13=uimenu(m1,'label','grain','callback','Color')
m21=uimenu(m2,'label','--')
m22=uimenu(m2,'label',':
')
%建立静态文本框用于显示相关信息
f_1=uicontrol('parent',f,'style','text','tag','text1','position',[103005030],...
'backgroundcolor','g','string','entera')
f_2=uicontrol('parent',f,'style','text','tag','text2','position',[102505030],...
'backgroundcolor','g','string','enterb')
f_3=uicontrol('parent',f,'style','text','tag','text3','position',[102005030],...
'backgroundcolor','g','string','enterc')
f_4=uicontrol('parent',f,'style','text','tag','text4','position',[101505030],...
'backgroundcolor','g','string','enterd')
%建立相应的动态文本框用于输入相关的数据
f_11=uicontrol('parent',f,'style','edit','tag','text11','position',[1003005030],...
'backgroundcolor','r','string','')
f_21=uicontrol('parent',f,'style','edit','tag','text21','position',[1002505030],...
'backgroundcolor','r','string','')
f_31=uicontrol('parent',f,'style','edit','tag','text31','position',[1002005030],...
'backgroundcolor','r','string','')
f_41=uicontrol('parent',f,'style','edit','tag','text41','position',[1001505030],...
'backgroundcolor','r','string','')
%建立按钮
b1=uicontrol('parent',f,'style','pushbutton','tag','pushbutton1','position',[2004005030],...
'backgroundcolor','b','string','plots','callback','ht1')%调用函数ht1
b2=uicontrol('parent',f,'style','pushbutton','tag','pushbutton2','position',[5004005030],...
'backgroundcolor','b','string','plotg','callback','ht2')%调用函数ht2
b3=uicontrol('parent',f,'style','pushbutton','tag','pushbutton3','position',[8004005030],...
'backgroundcolor','b','string','plotv','callback','ht3')%调用函数ht3
%建立滚动条
h1=uicontrol('parent',f,'style','slider','tag','slider1','position',[2004050020],...
'min',0,'max',.57,'callback','set(aa1,''position'',[0.2.2get(h1,''Value'').5])')
第7次作业
功能:
可视化界面设计
第一步:
建立如图所示按钮控件:
设计个控件关属性:
主要设计相关控件的string属性、
第三步:
保存为文件myfig.fg,并打开生成的相应得m文件myfig.m编辑
functionvarargout=myfig(varargin)
%MYFIGMATLABcodeformyfig.fig
%MYFIG,byitself,createsanewMYFIGorraisestheexisting
%singleton*.
%
%H=MYFIGreturnsthehandletoanewMYFIGort