matlab应用解析作业
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
MATLAB应用解析
第2章
1.创建double的变量,并进行计算。
(1) a=87,b=190,计算 a+b 、a-b 、a*b 。
(2) 创建 uint8 类型的变量,数值与(1)中相同,进行相同的计算。
(1)
>> a=87
a =
87
>> b=190
b =
190
>> a+b
ans =
277
>> a-b
ans =
-103
>> a*b
ans =
16530
(2)
>> c=uint8(87)
c =
87
>> d=uint8(190)
d =
190
>> c+d
ans =
255
>> c-d
ans =
>> c*d
ans =
255
2.计算:
(1) ()sin 60
(2) e 3 (3) 3cos 4⎛⎫π ⎪⎝⎭
(1)
>> sind(60)
ans =
0.8660
(2)
>> exp(3)
ans =
20.0855
(3)
>> cos(3*pi/4)
ans =
-0.7071
3.设2u =,3v =,计算: (1) 4log uv v
(2)
()22e u v v u +-
(3)
(1) >> u=2;
>> v=3;
>> 4*u*v/log(v)
ans =
21.8457
(2)
>> (exp(u)+v)^2/(v^2-u)
ans =
15.4189
(3)
>> sqrt(u-3*v)/(u*v)
ans =
0 + 0.4410i
4.计算如下表达式:
(1) ()()3542i i -+
(2) ()sin 28i -
(1)
>> (3-5*i)*(4+2*i)
ans =
22.0000 -14.0000i
(2)
>> sin(2-8*i)
ans =
1.3553e+03 + 6.2026e+02i
5.判断下面语句的运算结果。
(1)4 < 20
(2)4 <= 20
(3)4 == 20
(4)4 ~= 20
(5)'b'<'B'
:
(1)
>> 4<20
ans =
1
(2)
>> 4<=20
ans =
1
(3)
>> 4==20
ans =
(4)
>> 4~=20
ans =
1
(5)
>> 'b'<'B'
ans =
6.设39
c=,7
a=,58
b=,3
d=,判断下面表达式的值。
(1) a b
>
(2) a c
<
(3) &&
>>
a b b c
(4) a d
==
(5) |a b c
>
(6) ~~d
:
(1)
>> a=39;
>> b=58;
>> c=3;
>> d=7;
>> a>b
ans =
(2)
>> a<c
ans =
(3)
>> a>b&&b>c
ans =
(4)
>> a==d
ans =
(5)
>> a|b>c
ans =
1
(6)
>> ~~d
ans =
1
7.编写脚本,计算上面第2题中的表达式。
脚本文件内容为:
disp('sin(60)=');
disp(sind(60));
disp('exp(3)=');
disp(exp(3));
disp('cos(3*pi/4)=');
disp(cos(3*pi/4));
8.编写脚本,输出上面第6题中的表达式的值。
脚本文件内容为:
a=39;
b=58;
c=3;
d=7;
disp('a>b'),disp(a>b);
disp('a<c'),disp(a<c);
disp('a>b&&b>c'),disp(a>b&&b>c)
disp('a==d'),disp(a==d);
disp('a|b>c'),disp(a|b>c);
disp('~~d'),disp(~~d);
第3章
1. 在命令提示符下输入以下两条命令:
>> x = [ 9 3 0 6 3]
>> y = mod((sqrt(length(((x+5).*[1 2 3 4 5]))*5)),3)
求y值为多少?
:
y =2
第4章习
1. 有如下数据:
利用本章介绍的几种插值方法对其进行插值,得到每隔0.05的结果。
编写脚本文件,文件内容为:
% Interpolation using the four methods
x=[1 1.1 1.2 1.3 1.4];
y=[1.00000 1.23368 1.55271 1.99372 2.61170];
length_of_x=length(x);
scalar_x=x(1):0.05:x(length_of_x);
length_of_sx=length(scalar_x);
y_nearest = zeros(length(scalar_x),1);
y_linear = zeros(length(scalar_x),1);
y_spline = zeros(length(scalar_x),1);
y_cubic = zeros(length(scalar_x),1);
for i=1:length_of_sx
y_nearest(i)=interp1(x,y,scalar_x(i),'nearest');
y_linear(i) =interp1(x,y,scalar_x(i),'linear');
y_spline(i) =interp1(x,y,scalar_x(i),'spline');
y_cubic(i) =interp1(x,y,scalar_x(i),'cubic');
end
subplot(2,2,1),plot(x,y,'*'),hold on,plot(scalar_x,y_nearest),title('method=nearest');
subplot(2,2,2),plot(x,y,'*'),hold on,plot(scalar_x,y_linear),title('method=linear');
subplot(2,2,3),plot(x,y,'*'),hold on,plot(scalar_x,y_spline),title('method=spline');
subplot(2,2,4),plot(x,y,'*'),hold on,plot(scalar_x,y_cubic),title('method=cubic');
得到结果为:
2. 求函数5
x=的解,并绘制图形。
e x
=-,初始点为8
y x
>> y=@(x)exp(x)-x^5;
>> x = fzero(y,8)
x =
12.7132
>> fplot(y,[x-1,x+1]);
>> hold on
>> plot(x,y(x),'r*');
3. 求下列函数的极值。
(1) ()2
21
z x y
=--
(2) ()21
=-+
z x y
:
(1)
>> z = @(x)x(1)^2-(x(2)-1)^2;
>> [x,fvalue,flag,output]=fminsearch(z,[0,0])
Exiting: Maximum number of function evaluations has been exceeded
- increase MaxFunEvals option.
Current function value: -35921226633944080000000000000000000000000000000000000000000000000000000000000
0000000.000000
x =
1.0e+41 *
0.9523 -6.0686
fvalue =
-3.5921e+83
flag =
output =
iterations: 200
funcCount: 401
algorithm: 'Nelder-Mead simplex direct search'
message: [1x233 char]
(2)
>> z = @(x)(x(1)-x(2)+1)^2;
>> [x,fvalue,flag,output]=fminsearch(z,[0,0])
x =
-0.5425 0.4575
fvalue =
2.4109e-11
flag =
1
output =
iterations: 40
funcCount: 74
algorithm: 'Nelder-Mead simplex direct search'
message: [1x194 char]
4. 计算下列积分。
(1) 1
35 1d x x x x -++⎰ (2) 10102 11sin d d 4
x y y x y x ++⎰⎰ :
(1)
>> f = @(x)x+x.^3+x.^5;
>> q = quad(f,-1,1)
q =
2.2204e-16
(2)
>> f5 = @(x,y)sin(y).*(x+y)./(x.^2+4);
>> q = dblquad(f5,1,10,1,10)
q =
5.5254
第5章习
1.编制一个脚本,查找给定字符串中指定字符出现的次数和位置。
:
脚本文件内容如下。
% find the times and places of the specified letter in the string
letter = 'a'; % The specified letter to be searched for
string = 'China'; % The specified tring to be searched
places = findstr(string,letter) % The places of the letter
ntimes = length(places) % The times of the letter
2.编写一个脚本,判断输入字符串中每个单词的首字母是否为大写,若不是则将其修改为大写,其他字母为小写。
:
脚本文件内容为:
str = 'this is the string to be converted';
nlength = length(str);
for k=1:nlength
if (k==1 || isspace(str(k-1))) && (str(k)<='z' && str(k)>='a')
str(k) = char(double(str(k)) - 32);
end
end
disp(str);
3.创建2×2 单元数组,第1、2 个元素为字符串,第三个元素为整型变量,第四个元素为双精度(double)类型,并将其用图形表示。
:
>> cellA = cell(2,2);
>> cellA(1,1) = {'the first element of the cell'};
>> cellA(1,2) = {'the second element of the cell'};
>> cellA(2,1) = {uint8(5)};
>> cellA(2,2) = {[2,3;3,4]};
>> cellplot(cellA)
第6章习题
1. 假设x = [-3, 0, 0, 2, 5, 8]且y = [-5, -2,0, 3, 4, 10]。
通过手算得到以下运算的结果,并使用MATLAB检验计算的结果。
(1) z = y<~x
(2)z = x&y
(3) z = x|y
(4) z = xor(x,y)
:(1) z = [1, 1, 1, 0, 0, 0];
(2) z = [1, 0, 0, 1, 1, 1];
(3) z = [1, 1, 0, 1, 1, 1];
(4) z = [0, 1, 0, 0, 0, 0]
2. 在MATLAB 中使用一个循环确定:如果用户最初在一个银行帐户中存储$10000,并且在每年的年终再存储$10000(银行每年支付6%的利息),那么账户上要积累$1000000要需要多长时间。
: 33年。
3. 某个特定的公司生产和销售高尔夫手推车。
每周周末,公司都将那一周所生产的手推车转移到仓库(库存)之中。
卖出的所有手推车都是从库存中提取。
这个过程的一个简单模型为:
I (k + 1) = P (k ) + I (k ) - S (k )
其中:
P (k ) = 第k 周所生产的手推车数量; I (k ) = 第k 周库存中的手推车数量; S (k ) = 第k 周所卖出的手推车数量; 以下为10周计划中的每周销售额;
假设每周的产量都基于前一周的销售额,所以有P (k ) = S (k - 1)。
假设第一周的产量为50辆手推车:即,P (1) = 50。
编写一个MA TLAB 程序计算:10周之内每周库存之中的手推车数量或者计算手推车库存数量减少到0为止的时间,并同时绘制图形。
针对以下两种情况运行该程序:(1)初始库存为50辆手推车,所以I(1)= 50;(2)初始库存为30辆手推车,所以I (1) = 30。
:情况(1)和情况(2)的每周库存量
第七章习题
1. 创建符号表达式()sin f x x x =+。
:
>> f = sym('sin(x)+x');
2. 计算习题 1 中表达式在/6x π=处的值,并将结果设置为以下 5 种精度:小数点之后 1 位、2 位、4 位和10位有效数字。
:
>> digits(2);vpa(subs(f,x,pi/6)) ans =
1.0
>> digits(3);vpa(subs(f,x,pi/6)) ans = 1.02
>> digits(5);vpa(subs(f,x,pi/6)) ans = 1.0236
>> digits(10);vpa(subs(f,x,pi/6)) ans = 1.023*******
3.设x 为符号变量,()421f x x x =++,()32458g x x x x =+++,试进行如下运算:
(1) ()()f x g x + (2) ()()f x g x ⨯ (3) 求()g x 的反函数
(4) 求g 以()f x 为自变量的复合函数 :
>> f = sym('x^4 + x^2 + 1'); >> g = sym('x^3 + 4*x^2 + 5*x + 8');
(1)
>> f+g ans =
x^4 + x^3 + 5*x^2 + 5*x + 9
(2)
>> f*g ans =
(x^4 + x^2 + 1)*(x^3 + 4*x^2 + 5*x + 8)
(3)
>> finverse(g) ans =
1/(9*(x/2 + ((x/2 - 82/27)^2 - 1/729)^(1/2) - 82/27)^(1/3)) + (x/2 + ((x/2 - 82/27)^2 - 1/729)^(1/2) - 82/27)^(1/3) - 4/3
(4)
>> syms x
>> compose(g,f,x) ans =
4*(x^4 + x^2 + 1)^2 + (x^4 + x^2 + 1)^3 + 5*x^2 + 5*x^4 + 13
4.合并同类项
(1) 2
2
325325x x x x -++--
(2) 2222322521x xy y xy x xy y -+--+-+(对x 和y ) : (1)
>> f = sym('3*x - 2*x^2 + 5 + 3*x^2 - 2*x -5'); >> collect(f) ans = x^2 + x
(2)
>> f = sym('2*x^2 - 3*x*y + y^2 - 2*x*y - 2*x^2 + 5*x*y - 2*y + 1'); >> collect(f) ans = y^2 - 2*y + 1
5.因式分解
(1) 将 7798666 进行因数分解,分解为素数乘积的形式
(2) 8
-2m +512
(3) 2
3
2
2
3a (x-y)-4b (y-x) : (1)
>> factor(sym('779866')) ans = 2*149*2617
(2)
>> factor(sym('-2*m^8 + 512')) ans =
-2*(m - 2)*(m + 2)*(m^2 + 4)*(m^4 + 16)
(3)
>> factor(sym('3*a^2*(x-y)^3 - 4*b^2*(y-x)^2')) ans =
(x - y)^2*(3*a^2*x - 3*a^2*y - 4*b^2)
6.绘制下列函数的图像
(1) ()2sin f x x x =+,[]0,2π (2) ()3221f x x x =++,[]2,2- : (1)
>> f = sym('sin(x) + x^2'); >> ezplot(f,[0,2*pi]);
(2)
>> f = sym('x^3 + 2*x^2 + 1'); >> ezplot(f,[-2 2]);
7.计算下列各式
(1) 0tan sin lim
1cos 2x x x
x
→--
(2) 3
2
2sin y x x x =-+,求y '
(3) ()ln y xy x y =+,求/f x ∂∂ (4) ln(1)y t dx =+⎰,27
ln(1)y t dx =+⎰
: (1)
>> limit(sym('(tan(x) - sin(x))/(1-cos(2*x))')) ans = 0
(2)
>> y = sym('x^3 - 2*x^2 + sin(x)'); >> diff(y) ans =
cos(x) - 4*x + 3*x^2
(3)
>> f = x*y*log(x+y); >> fx = diff(f,x) fx =
log(x + sin(x) - 2*x^2 + x^3)*(sin(x) - 2*x^2 + x^3) + x*log(x + sin(x) - 2*x^2 + x^3)*(cos(x) - 4*x + 3*x^2) + (x*(sin(x) - 2*x^2 + x^3)*(cos(x) - 4*x + 3*x^2 + 1))/(x + sin(x) - 2*x^2 + x^3)
(4)
>> syms t >> y = log(1+t); >> int(y) ans =
(log(t + 1) - 1)*(t + 1) >> int(y,0,27) ans = 28*log(28) - 27
8.计算sin x 在 0 附近的Taylor 展开。
:
>> taylor(sym('sin(x)')) ans =
x^5/120 - x^3/6 + x
9.求解线性方程组231
321
x y x y +=⎧⎨+=-⎩
:
>> [x,y] = solve(sym('2*x+3*y=1'),sym('3*x+2*y=-1')) x = -1 y = 1
10.对符号表达式2
2
x
y z xe --=,进行如下变换
(1) 关于x 的傅立叶变换 (2) 关于y 的拉普拉斯变换 (3) 分别关于x 和y 的 Z 变换 : (1)
>> syms x y
>> z = x*exp(-(x^2+y^2)); >> syms u v >> fourier(z,x,u) ans =
-(pi^(1/2)*u*exp(- u^2/4 - y^2)*i)/2
(2)
>> laplace(z,y,v) ans =
(pi^(1/2)*x*erfc(v/2)*exp(v^2/4)*exp(-x^2))/2
(3)
>> ztrans(z,x,u) ans =
-u*diff(ztrans(exp(- x^2 - y^2), x, u), u) >> ztrans(z,y,v) ans =
x*ztrans(exp(- x^2 - y^2), y, v)
11.绘制函数()()()
221
exp 2f x x y π
=
-+在33x -<<,33y -<<上的表面图。
:
>> syms x y
>> z = 1/(2*pi)*exp(-(x^2+y^2)); >> ezsurf(x,y,z,[-3,3,-3,3]);
第8章习题
1. 编写程序,该程序在同一窗口中绘制函数在 []0,2π之间的正弦曲线和余弦曲线,步长为 /10π,线宽为 4 个像素,正弦曲线设置为蓝色实线,余弦曲线颜色设置为红色虚线,两条曲线交点处,用红色星号标记。
:
>> x=0:pi/10:2*pi; >> sinx = sin(x); >> cosx = cos(x);
>> figure,plot(x,sinx,'LineWidth',4) >> hold on,plot(x,cosx,'r:','LineWidth',4)
>> hold on,plot(x(find(cosx==sinx)),cosx(find(cosx==sinx)),'r*','LineWidth',4)
2. 绘制下列图像
(1) sin y x x =,010x π<<
(2) 三维曲线:22
6621z x xy y x y =++++-,1010x -<<,1010y -<<
(3) 双曲抛物面:22
164
x y z =
-,1616x -<<,44y -<< :
(1)
>> x = 0:pi/10:10*pi;
>> plot(x,sin(x))
(2)
>> [X,Y] = meshgrid(-10:0.5:10);
>> Z = X.^2 + 6*X*Y + Y.^2 + 6*X + 2*Y -1;
>> plot3(X,Y,Z)
(3)
>> [X,Y] = meshgrid(-16:0.4:16,-4:0.1:4);
>> Z = X.^2/16 - Y.^2/4;
>> plot3(X,Y,Z)
3. 绘制下列图像
(1) 绘制电脑磁盘使用情况的饼状图
(2) 生成100 个从0 到10 之间的随机整数,绘制其直方图
(3) 生成10个从0 到10 之间的随机整数,绘制其阶跃图:
(1) 利用pie 函数(略)。
(2)
>> hist(round(rand(100,1)*10))
(3)
>> X = round(rand(10,1)*10);
>> stairs(X)
4. 分别通过界面交互方式和函数方式在第1 题生成的图形中添加注释,至少应包括:标题,文本注释,图例。
:
界面交互方式略。
函数方式的如下(绘制图形略)。
>> title('正弦曲线和余弦曲线');
>> gtext('sin(x)')
>> gtext('cos(x)')
>> legend('sin(x)','cos(x)')
5.对第2 题中绘制的双曲抛物面尝试进行视点控制和颜色控制。
:使用view 函数和colormap 函数。
第9章习题
1. 新建图形窗口,设置其标题为“对数函数的图像”,在该窗口中绘制对数函数
ln
f x =
在
010
x
<<的图像。
:
>> figure,title('对数函数的图像'),plot(0:0.2:10, log(0:0.2:10))
2. 编写程序,实现功能为:创建图形窗口,并且设置其默认背景为黄色,默认线宽为4 个
像素,在该窗口中绘制椭圆 22
221x y a b
+=的图像,其中的a 和b 任选 :
figure('Color','y');
set(gca,'DefaultLineLineWidth',4);
a = 4;
b = 3;
x = linspace(-a,a,100);
y1 = sqrt((1-x.^2/(a^2))*b^2);
y2 = -sqrt((1-x.^2/(a^2))*b^2);
plot(x,y1);
hold on;
plot(x,y2);
3. 编写 MATLAB 程序,绘制下面的函数:
()()cos 2sin 2t x t t y t ππ⎧⎛⎫= ⎪⎪⎪⎝⎭⎨⎛⎫⎪= ⎪⎪⎝⎭⎩,其中22t -≤≤
该程序在绘制图形之后等待用户的鼠标输入,每单击其中一条曲线,就随机修改该曲线的颜色,包括红色、绿色、蓝色、黑色和黄色。
:
t = -2:0.1:2;
x = cos(t/pi);
y = 2*sin(t/(2*pi));
figure,plot(t,x);
hold on; plot(t,y);
%if waitforbuttonpress
while(1)
if waitforbuttonpress==0
get(gcf,'CurrentObject');
c = ceil((rand) * 5);
switch c
case 1
set(get(gcf,'CurrentObject'),'Color', 'r');
case 2
set(get(gcf,'CurrentObject'),'Color', 'g');
case 3
set(get(gcf,'CurrentObject'),'Color', 'b');
case 4
set(get(gcf,'CurrentObject'),'Color', 'k');
case 5
set(get(gcf,'CurrentObject'),'Color', 'y');
end
end
end。