资源描述
目录
第一章 1
第二章 5
第三章 12
第四章 32
第五章 47
第六章 54
补充题 欧拉法,龙格库塔法解方程,黑板上的题 57
1.创建表达式
%可以用syms先符号运算再带入值
x=1;
y=2;
z=(sqrt(4*x^2+1)+0.5457*exp(-0.75*x^2-3.75*y^2-1.5*x))/(2*sin(3*y)-1)
z =
-1.4345
2.计算复数
x=(-1+sqrt(-5))/4;
y=x+8+10j
y =
7.7500 +10.5590i
3.help命令学三维曲线
x=-5:0.1:5;
y=x;
[X,Y]=meshgrid(x,y);
Z=(sin(sqrt(X.^2+Y.^2)))./(sqrt(X.^2+Y.^2));
subplot(221);
surf(X,Y,Z);
colormap(cool);
subplot(222);
plot3(X,Y,Z,'linewidth',4); %绘制三维曲线,也可以随意给定一个三维曲线的函数。如果画这个曲面,那么将绘出一族三维曲线
grid on;
subplot(223);
meshz(X,Y,Z); %地毯绘图
subplot(224);
meshc(X,Y,Z); %等高线绘图
4.peaks等高线(更改原函数)
subplot(221);
contour(peaks1,20);
subplot(222);
contour3(peaks1,10); %可以定义等高线条数
subplot(223);
contourf(peaks1,10);
subplot(224);
peaks1;
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
- 1/3*exp(-(x+1).^2 - y.^2)
5. LOGO绘制
membrane
logo
第一章书后习题
1.合法性
不合法合法不合法不合法合法
2.运行命令及探讨
a=sqrt(2)
a =
1.4142
答:不是精确的。是一个近似。可通过改变format进行位数显示调整。
例如:
format long;
a=sqrt(2)
format short;
a =
1.414213562373095
或可使用digits任意指定输出位数。
例如:
digits(50);
a=sqrt(2);
vpa(a)
ans =
1.4142135623730950488016887242096980785696718753769
常见情况下毋需太高精度。
3.运行结果讨论
format long;
w1=a^(2/3)
w2=a^2^(1/3)
w3=(a^(1/3))^2
w1 =
1.259921049894873
w2 =
1.259921049894873
w3 =
1.259921049894873
测试结果为相同,说明MATLAB程序执行时经过的过程相同。
4.clearclfclc
clear 为从内存中清除变量和函数
clf为清除figure中的已绘图形以及子图形
clc为清除命令行窗口
5.产生二维数组
显然第一第二个方法可以实现。
例如:
s=[1 2 3;4 5 6;7 8 9]
s =
1 2 3
4 5 6
7 8 9
即是一个简便的键入矩阵的方法。
第二章
1 数据类型
class(3/7+0.1)
class(sym(3/7+0.1))
class(vpa(sym(3/7+0.1),4))
class(vpa(sym(3/7+0.1)))
ans =
double
ans =
sym
ans =
sym
ans =
sym
2 哪些精准?
a1=sin(sym(pi/4)+exp(sym(0.7)+sym(pi/3)));
a2=sin(sym(pi/4)+exp(sym(0.7))*exp(sym(pi/3)));
a3=sin(sym('pi/4')+exp(sym('0.7'))*exp(sym('pi/3')));
a4=sin(sym('pi/4')+exp(sym('0.7+pi/3')));
a5=sin(sym(pi/4)+exp(sym(0.7+pi/3)));
a6=sin(sym(pi/4)+sym(exp(0.7+pi/3)));
a7=sin(sym(pi/4+exp(0.7+pi/3)));
a8=sym(sin(pi/4+exp(0.7+pi/3)));
digits(64);
vpa(a2-a1)
vpa(a3-a1)
vpa(a4-a1) %为精确值
vpa(a5-a1)
vpa(a6-a1)
vpa(a7-a1)
vpa(a8-a1)
ans =
8.772689107613377606024459313047548287536202098197290121158158175e-72
ans =
8.772689107613377606024459313047548287536202098197290121158158175e-72
ans =
0.0
ans =
-0.0000000000000008874822716959584619522637254014249128254875650208152937300697045
ans =
-0.000000000000001489122128176563341755713716272780778030227615022223735634526288
ans =
-0.000000000000001518855593927822635897082947744411794950714383466168364259064934
ans =
-0.00000000000000151859755909122793880734918235619076228065004813152159311456667
可以看到,除了a4为精确,其余均存在很小的误差。其中a2与a3的误差较小,小于eps精度,故可认为为精确的。
3 独立自由变量
a1=sym('sin(w*t)') ;
a2=sym('a*exp(-X)' );
a3=sym('z*exp(j*th)');
symvar(a1,1)
symvar(a2,1)
symvar(a3,1)
ans =
w
ans =
a
ans =
z
6 符号解
syms x k;
f1=x.^k;
s1=symsum(f1,k,0,inf);
s2=subs(f1,x,(-1/3));
s3=subs(f1,x,(1/pi));
s4=subs(f1,x,3);
symsum(s2,k,0,inf)
double(symsum(s3,k,0,inf))
symsum(s4,k,0,inf)
ans =
3/4
ans =
1.4669
ans =
Inf
7 限定性假设
reset(symengine);
syms k;
syms x positive;
f1=(2/(2*k+1))*((x-1)/(x+1))^(2*k+1);
f1_s=symsum(f1,k,0,inf);
simplify(f1_s,'steps',27,'IgnoreAnalyticConstraints',true)
ans =
log(x)
8 符号计算
syms t;
yt=abs(sin(t));
dydt=diff(yt,t)
dydt0=limit(dydt,t,0,'left')
dydtpi=subs(dydt,t,(pi/2))
dydt =
sign(sin(t))*cos(t)
dydt0 =
-1
dydtpi =
0
9 积分值
syms x;
fx=exp(-abs(x))*abs(sin(x))
fxint=int(fx,-5*pi,1.7*pi);
vpa(fxint,64)
fx =
abs(sin(x))*exp(-x)
ans =
3617514.635647088707100018393465500554242735057835123431773680704
10二重积分
syms x y;
fxy=x^2+y^2;
int(int(fxy,y,1,x^2),x,1,2)
ans =
1006/105
11 绘出曲线
syms t x;
fx=int((sin(t)./t),t,0,x);
ezplot(fx)
fx4=subs(fx,x,4.5)
fx4 =
sinint(9/2)
12 积分表达式
syms x;
syms n positive;
yn=int((sin(x)).^n,x,0,pi/2)
yn3=subs(yn,n,1/3);
vpa(yn3,32)
yn =
beta(1/2, n/2 + 1/2)/2
ans =
1.2935547796148952674767575125656
13 序列卷积
syms a b n;
syms k positive;
xk=a.^k;
hk=b.^k;
kn=subs(xk,k,k-n)*subs(hk,k,n);
yk=symsum(kn,n,0,k)
yk =
piecewise([a == b and b ~= 0, b^k*(k + 1)], [a ~= b or b == 0, (a*a^k - b*b^k)/(a - b)])
所以答案为a*a^k - b*b^k)/(a - b)
20求解solve
reset(symengine)
syms x y;
s=solve('x^2+y^2-1','x*y-2','x','y')
s.x
s.y
s =
x: [4x1 sym]
y: [4x1 sym]
ans =
((15^(1/2)*i)/2 + 1/2)^(1/2)/2 - ((15^(1/2)*i)/2 + 1/2)^(3/2)/2
- ((15^(1/2)*i)/2 + 1/2)^(1/2)/2 + ((15^(1/2)*i)/2 + 1/2)^(3/2)/2
(1/2 - (15^(1/2)*i)/2)^(1/2)/2 - (1/2 - (15^(1/2)*i)/2)^(3/2)/2
- (1/2 - (15^(1/2)*i)/2)^(1/2)/2 + (1/2 - (15^(1/2)*i)/2)^(3/2)/2
ans =
((15^(1/2)*i)/2 + 1/2)^(1/2)
-((15^(1/2)*i)/2 + 1/2)^(1/2)
(1/2 - (15^(1/2)*i)/2)^(1/2)
-(1/2 - (15^(1/2)*i)/2)^(1/2)
23 求通解
clear all;
yso=simplify(dsolve('Dy*y*0.1+0.3*x=0','x'))
yso =
(- 3*x^2 + 2*C3)^(1/2)
-(- 3*x^2 + 2*C3)^(1/2)
%此题存疑
holdon;clear all;
reset(symengine);
syms x;
y1=(- 3*x^2 + 2*1)^(1/2);
y2=-(- 3*x^2 + 2*1)^(1/2);
h1=ezplot(y1,x,[-2 2 -2 2],1);
h2=ezplot(y2,x,[-2 2 -2 2],1);
gridon;title('');warning off;axis([-2 2 -2 2]);
set(h1,'color','r','linewidth',2);
set(h2,'color','r','linewidth',2);
xlabel('Y');ylabel('x');
%对于第二章存在问题的习题的探讨
2.23
clear all;
syms x;
yso=simplify(dsolve('Dy*y*0.1+0.3*x=0','x'))
%此题存疑
holdon;clear all;
reset(symengine);
syms x;
y1=(- 3*x^2 + 2*1)^(1/2);
y2=-(- 3*x^2 + 2*1)^(1/2);
h1=ezplot(y1,x,[-2 2 -2 2],1);
h2=ezplot(y2,x,[-2 2 -2 2],1);
gridon;title('');warning off;axis([-2 2 -2 2]);
set(h1,'color','r','linewidth',2);
set(h2,'color','r','linewidth',2);
xlabel('Y');ylabel('x');
yso =
(- 3*x^2 + 2*C3)^(1/2)
-(- 3*x^2 + 2*C3)^(1/2)
%以上方法可以绘出正常的横坐标为y纵坐标为x的图像,但发现在y=0处x延伸至正负无穷。
h1=ezplot(y1,[-2 2 -2 2],1);
h2=ezplot(y2,[-2 2 -2 2],1);
%以上方法绘出的图像存在一个空隙,且默认为y-x图像。
reset(symengine);
syms x y S;
S = dsolve('Dy*y/5+x/4=0','x');
ezplot(subs(y^2-(S(1))^2, 'C3', 1),[-2,2 -2,2],2);
grid on;
%用椭圆方程绘图不产生间隙
24 一阶微分方程
syms a b;
ys=dsolve('Dy-a*x^2-b*x=0','y(0)=2',x)
ys =
(x^2*(3*b + 2*a*x))/6 + 2
25 边值问题
fs=dsolve('Df-3*f=4*g,Dg+4*f=3*g','f(0)=0,g(0)=1')
fs =
g: [1x1 sym]
f: [1x1 sym]
fs.g
fs.f
ans =
cos(4*t)*exp(3*t)
ans =
sin(4*t)*exp(3*t)
第三章
3.行下标列下标
rng('default');
A=rand(3,5);
L=A>0.5
L =
1 1 0 1 1
1 1 1 0 0
0 0 1 1 1
[a,b]=find(L==1)
IND=sub2ind(size(A),a,b)
IND =
1
2
4
5
8
9
10
12
13
15
4.循环运算、数组运算
t=0:0.1:10;
N=length(t);
y1=zeros(size(t));
for k=1:N
y1(k)=1-exp(-0.5*t(k))*cos(2*t(k));
end
plot(t,y1);
xlabel('t');
ylabel('y1');
y2=1-exp(-0.5*t).*cos(2*t);
plot(t,y2);
xlabel('t');
ylabel('y2');
5.回答问题
clear all;
A=magic(3);B=rand(3);
A*B
B*A
ans =
5.4072 11.5771 3.0037
6.3884 10.3215 4.9680
2.7058 7.5337 4.8496
ans =
2.5916 3.8303 5.2097
3.4833 5.6313 3.6800
10.9646 9.0086 12.3554
相同,对于矩阵而言对位相乘无差异
不相同,点乘与矩阵乘法进行的不是同一种运算。
不相同,左乘右乘运算不同。
相同,A左点除B等同于B右点除A,均是对位计算。
不相同,左除右除运算亦不相同。
A*A\B-B
A*(A\B)-B
A*(A*inv(B))-B
ans =
-0.0562 -0.6902 -0.0436
-0.1051 -0.3282 -0.4311
-0.8011 -0.9350 -0.3763
ans =
1.0e-15 *
0 -0.1110 -0.0278
0 0 0
0 0.1110 0
ans =
-80.2971 65.0383 107.2212
-8.0299 91.2626 70.5679
-66.2535 153.4898 66.4342
不相同。第二个更接近0。具体原理需要参考线性代数书……有点忘了。
A\eye(3)
eye(3)/A
ans =
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028
ans =
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028
相同。因为对于对角阵,,二者均可化为同一形式。
6.结果不同
A=[1 2; 3 4];
B1=A.^(0.5)
B2=0.5.^A
B3=A^(0.5)
B4=0.5^A
B1 =
1.0000 1.4142
1.7321 2.0000
B2 =
0.5000 0.2500
0.1250 0.0625
B3 =
0.5537 + 0.4644i 0.8070 - 0.2124i
1.2104 - 0.3186i 1.7641 + 0.1458i
B4 =
0.9910 -0.4422
-0.6634 0.3276
A1=B1.*B1
A3=B3*B3
norm(A1-A3,'fro')
A1 =
1.0000 2.0000
3.0000 4.0000
A3 =
1.0000 + 0.0000i 2.0000 + 0.0000i
3.0000 - 0.0000i 4.0000 + 0.0000i
ans =
1.2831e-15
可见误差在eps量级,可以认为相等。
7.绘出图形
x=-3*pi:pi/15:3*pi;
y=x;
[X,Y]=meshgrid(x,y);
warning off;
Z=sin(X).*sin(Y)./X./Y;
共有10个非数数据。
surf(X,Y,Z)
shadinginterp
x=-3*pi:pi/15:3*pi;
Lx=(x==0);
xx=x+Lx*realmin;
y=xx;
[X,Y]=meshgrid(xx,y);
warning off;
Z=sin(X).*sin(Y)./X./Y;
surf(X,Y,Z)
shadinginterp
即消除零点处的断点即可
8.两种思路
%第二种思路
function z=zpoly_z(x,y)
ifx+y<=-1
z=0.546*exp(-0.75*y.^2-3.75*x.^2+1.5*x);
elseifx+y>-1 & x+y<=1
z=0.758*exp(-y.^2-6*x.^2);
else
z=0.546*exp(-0.75*y.^2-3.75*x.^2-1.5*x);
end
x=-1.5:0.1:1.5;
y=-3:0.1:3;
[X,Y]=meshgrid(x,y);
Z=zpoly_z(X,Y);
surf(X,Y,Z);
%第一种思路
x=-1.5:0.1:1.5;
y=-3:0.2:3;
LX=length(x);
LY=length(y);
for ii=1:LX
forjj=1:LY
if x(ii)+y(jj)<=-1
z=0.546*exp(-0.75*y.^2-3.75*x.^2+1.5*x);
elseif x(ii)+y(jj)>-1 & x(ii)+y(jj)<=1
z=0.758*exp(-y.^2-6*x.^2);
else
z=0.546*exp(-0.75*y.^2-3.75*x.^2-1.5*x);
end
end
end
[X,Y]=meshgrid(x,y);
Z=zpoly_z(X,Y);
surf(X,Y,Z);
%其实for循环完全无意义……
9.矩阵计算
%第一问老师取消
rng default
A=randn(50,70)+1i*randn(50,70);
B=randn(70,60)+1i*randn(70,60);
C=randn(50,60)+1i*randn(50,60);
D=randn(60,1)+1i*randn(60,1);
G=(A*B-C)*D
Gr=real(G),70,70
Gi=imag(G)
Gn=norm(G,2)
G =
1.0e+02 *
-0.1776 + 1.9914i
0.6088 + 0.3316i
-0.1340 - 0.8615i
0.0752 - 0.0759i
-0.1171 - 1.8169i
0.2005 - 1.4540i
-1.4501 + 0.1897i
0.6445 + 0.1657i
-1.0651 + 0.1191i
0.3301 - 0.0450i
-1.4338 + 0.8707i
-0.9491 + 1.4840i
1.1314 + 1.2751i
-0.5158 - 0.0725i
-0.2746 + 0.2518i
-1.0279 - 0.8409i
-1.1161 - 2.3362i
0.1346 + 1.3500i
0.4220 - 1.2839i
0.2650 - 0.2849i
-1.0212 + 0.5374i
0.0563 + 0.4151i
-1.9074 - 0.2448i
0.1645 + 1.2071i
1.1870 + 0.0085i
1.2304 + 0.6672i
0.3303 - 1.6027i
-0.5728 - 0.5519i
0.3738 + 0.2863i
-0.6682 - 0.7565i
1.6063 + 1.2886i
0.6994 - 1.3377i
0.6523 + 0.0318i
-0.2143 - 2.8209i
1.7026 - 0.1371i
0.9285 + 1.5852i
-0.7550 - 0.2427i
-1.3879 - 1.8978i
-0.5266 - 0.8334i
-0.0849 + 0.1680i
1.1590 + 0.2109i
-1.8938 + 0.6709i
0.3406 - 1.8211i
-1.0916 - 1.8076i
0.2062 - 1.4363i
1.3679 + 0.2061i
-0.4541 + 0.8056i
1.3574 + 0.8773i
-0.1071 + 0.0948i
0.1042 + 2.2812i
Gr =
-17.7553
60.8848
-13.4003
7.5175
-11.7073
20.0458
-145.0055
64.4517
-106.5069
33.0077
-143.3779
-94.9055
113.1368
-51.5804
-27.4560
-102.7914
-111.6150
13.4596
42.2009
26.5006
-102.1225
5.6295
-190.7388
16.4525
118.6963
123.0361
33.0336
-57.2817
37.3849
-66.8175
160.6261
69.9436
65.2278
-21.4319
170.2597
92.8549
-75.5045
-138.7923
-52.6574
-8.4902
115.9030
-189.3844
34.0593
-109.1584
20.6169
136.7896
-45.4089
135.7386
-10.7050
10.4240
Gi =
199.1404
33.1590
-86.1452
-7.5887
-181.6856
-145.4039
18.9686
16.5731
11.9053
-4.5021
87.0651
148.4022
127.5072
-7.2483
25.1791
-84.0887
-233.6194
135.0018
-128.3931
-28.4923
53.7385
41.5139
-24.4788
120.7113
0.8532
66.7238
-160.2738
-55.1871
28.6287
-75.6522
128.8596
-133.7671
3.1772
-282.0866
-13.7111
158.5203
-24.2673
-189.7767
-83.3384
16.7992
21.0869
67.0898
-182.1134
-180.7631
-143.6344
20.6149
80.5622
87.7339
9.4764
228.1237
Gn =
1.0253e+03
y2=1-exp(-0.5*t).*cos(2*t);
plot(t,y2);
xlabel('t');
ylabel('y2');
5.回答问题
clear all;
A=magic(3);B=rand(3);
A*B
B*A
ans =
5.4072 11.5771 3.0037
6.3884 10.3215 4.9680
2.7058 7.5337 4.8496
ans =
2.5916 3.8303 5.2097
3.4833 5.6313 3.6800
10.9646 9.0086 12.3554
相同,对于矩阵而言对位相乘无差异
不相同,点乘与矩阵乘法进行的不是同一种运算。
不相同,左乘右乘运算不同。
相同,A左点除B等同于B右点除A,均是对位计算。
不相同,左除右除运算亦不相同。
A*A\B-B
A*(A\B)-B
A*(A*inv(B))-B
ans =
-0.0562 -0.6902 -0.0436
-0.1051 -0.3282 -0.4311
-0.8011 -0.9350 -0.3763
ans =
1.0e-15 *
0 -0.1110 -0.0278
0 0 0
0 0.1110 0
ans =
-80.2971 65.0383 107.2212
-8.0299 91.2626 70.5679
-66.2535 153.4898 66.4342
不相同。第二个更接近0。具体原理需要参考线性代数书……有点忘了。
A\eye(3)
eye(3)/A
ans =
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028
ans =
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028
相同。因为对于对角阵,,二者均可化为同一形式。
6.结果不同
A=[1 2; 3 4];
B1=A.^(0.5)
B2=0.5.^A
B3=A^(0.5)
B4=0.5^A
B1 =
1.0000 1.4142
1.7321 2.0000
B2 =
0.5000 0.2500
0.1250 0.0625
B3 =
0.5537 + 0.4644i 0.8070 - 0.2124i
1.2104 - 0.3186i 1.7641 + 0.1458i
B4 =
0.9910 -0.4422
-0.6634 0.3276
A1=B1.*B1
A3=B3*B3
norm(A1-A3,'fro')
A1 =
1.0000 2.0000
3.0000 4.0000
A3 =
1.0000 + 0.0000i 2.0000 + 0.0000i
3.0000 - 0.0000i 4.0000 + 0.0000i
ans =
1.2831e-15
可见误差在eps量级,可以认为相等。
7.绘出图形
x=-3*pi:pi/15:3*pi;
y=x;
[X,Y]=meshgrid(x,y);
warning off;
Z=sin(X).*sin(Y)./X./Y;
共有10个非数数据。
surf(X,Y,Z)
shadinginterp
x=-3*pi:pi/15:3*pi;
Lx=(x==0);
xx=x+Lx*realmin;
y=xx;
[X,Y]=meshgrid(xx,y);
warning off;
Z=sin(X).*sin(Y)./X./Y;
surf(X,Y,Z)
shadinginterp
即消除零点处的断点即可
8.两种思路
%第二种思路
function z=zpoly_z(x,y)
ifx+y<=-1
z=0.546*exp(-0.75*y.^2-3.75*x.^2+1.5*x);
elseifx+y>-1 & x+y<=1
z=0.758*exp(-y.^2-6*x.^2);
else
z=0.546*exp(-0.75*y.^2-3.75*x.^2-1.5*x);
end
x=-1.5:0.1:1.5;
y=-3:0.1:3;
[X,Y]=meshgrid(x,y);
Z=zpoly_z(X,Y);
surf(X,Y,Z);
%第一种思路
x=-1.5:0.1:1.5;
y=-3:0.2:3;
LX=length(x);
LY=length(y);
for ii=1:LX
forjj=1:LY
if x(ii)+y(jj)<=-1
z=0.546*exp(-0.75*y.^2-3.75*x.^2+1.5*x);
elseif x(ii)+y(jj)>-1 & x(ii)+y(jj)<=1
z=0.758*exp(-y.^2-6*x.^2);
else
z=0.546*exp(-0.75*y.^2-3.75*x.^2-1.5*x);
end
end
end
[X,Y]=meshgrid(x,y);
Z=zpoly_z(X,Y);
surf(X,Y,Z);
%其实for循环完全无意义……
9.矩阵计算
%第一问老师取消
rng default
A=randn(50,70)+1i*randn(50,70);
B=randn(70,60)+1i*randn(70,60);
C=randn(50,60)+1i*randn(50,60);
D=randn(60,1)+1i*randn(60,1);
G=(A*B-C)*D
Gr=real(G),70,70
Gi=imag(G)
Gn=norm(G,2)
G =
1.0e+02 *
-0.1776 + 1.9914i
0.6088 + 0.3316i
-0.1340 - 0.8615i
0.0752 - 0.0759i
-0.1171 - 1.8169i
0.2005 - 1.4540i
-1.4501 + 0.1897i
0.6445 + 0.1657i
-1.0651 + 0.1191i
0.3301 - 0.0450i
-1.4338 + 0.8707i
-0.9491 + 1.4840i
1.1314 + 1.2751i
-0.5158 - 0.0725i
-0.2746 + 0.2518i
-1.0
展开阅读全文