第二章
%分段函数的写法
syms y x
z=int(x*y,x,0,1);
g = evalin(symengine,[' piecewise([y > 1/2,' char(z) '], [y <= 1/2, 1])']) G=int(g,y)
g =
piecewise([1/2 < y, y/2], [y <= 1/2, 1])
G =
piecewise([1/2 < y, y^2/4], [y <= 1/2, y])
%习题2-1
class(vpa(sym(3/7+0.1),4))
%习题2-2
findsym(sym('sin(w*t)'),1)
findsym(sym('a*exp(-X)'),1)
findsym(sym('z*exp(j*th)'),1)
%习题2-3
syms x a positive
x_1=solve('x^3-44.5')
syms x unreal
x_2=solve('x^2-a*x+a^2')
syms a unreal
x_2=solve('x^2-a*x+a^2')
x_3=solve('x^2-a*x-a^2')
syms x real
evalin(symengine,'anames(Properties)')
evalin(symengine,'getprop(x)')
%习题2-4
7/3, pi/3 , pi*3^(1/3)
a = pi*3^(1/3) ,
b = sym( pi*3^(1/3) ),
c = sym( pi*3^(1/3),'d'),
d = sym( 'pi*3^(1/3) ' )
vpa(abs(a-d)) , vpa(abs(b-d)) , vpa(abs(c-d))
%习题2-5
syms a11 a12 a13 a21 a22 a23 a31 a32 a33
A = [a11 a12 a13;a21 a22 a23;a31 a32 a33]
a=det(A)
B=inv(A)
C=subexpr(B)
[RS,w]=subexpr(B,'w')
%习题2-6
syms k
syms a positive
% fk =a^k * heaviside(k)
fk =a^k
s=symsum(fk,k,0,inf)
%习题2-7
clear all
syms k
syms x positive
fk =2/(2*k+1)*(((x-1)/(x+1))^(2*k+1))
s=symsum(fk,k,0,inf)
s1=simple(s)
%习题2-8
clear all, syms t
y=abs(sin(t))
df=diff(y),class(df)
df1=limit(df,t,0,'left')
df2=subs(df,'t',sym(pi/2))
%习题2-9
clear all, syms x;
f=exp(-abs(x)).*abs(sin(x));
fint=int(f,x,-5*pi,1.7*pi), digits(64), vpa(fint)
class(fint)
%习题2-10
clear all,syms x y,f=x.^2+y.^2,
fint=(int(int(f,y,1,x.^2),x,1,2)), double(fint)
%习题2-11
clear all, syms t x; f=sin(t)/t, yx=int(f,t,0,x), ezplot(yx,[0 2*pi])
fint=subs(yx,x,4.5),%或yxd=int(f,t,0,4.5),fint=double(yxd)
hold on, plot(4.5,fint,'*r')
%习题2-12
% clear all, syms x n; f=(sin(x))^n; yn=int(f,'x',0,pi/2), class(yn)
clear all, syms x n; syms n positive ; f=(sin(x))^n; yn=int(f,'x',0,pi/2), class(yn) % y(1/3)=?
yn1=subs(yn,'n',sym(1/3)),vpa(yn1)
%或yn=limit(yn,n,1/3),vpa(yn)
%或yy=int(sin(x).^(1/3),x,0,pi/2) ,vpa(yy)
%习题2-23
clear, syms x y S
S = dsolve('Dy*y/5+x/4=0','x')
ezplot(subs(S(1),'C3',1),[-2,2 -2,2],1), hold on
ezplot(subs(S(2),'C3',1),[-2,2 -2,2],1)
%解为S =
% 2^(1/2)*(C3 - (5*x^2)/8)^(1/2)
% -2^(1/2)*(C3 - (5*x^2)/8)^(1/2)
ezplot(subs(S(1),'C3',1),[-2,2 -2,2],1), hold on
ezplot(subs(S(2),'C3',1),[-2,2 -2,2],1) % 用此两条指令绘圆,在y=0处有间隙
ezplot(subs(y^2-(S(1))^2, 'C3', 1),[-2,2 -2,2],2) %用椭圆方程绘图不产生间隙
colormap([0 0 1]) %用ezplot(fun)绘图时,如果fun中只有一个参数,绘图的颜是蓝;如果fun中有两个参数,绘图的颜是绿,此指令设置图形颜为蓝。
grid on
S1=subs(S(1),'C3',1)
subs(S1,{x},{1.6^(1/2)})
y=double(solve(S1))
t=linspace(y(2),y(1),100)
S2=subs(S(2),'C3',1)
figure
plot(t,subs(S1,x,t)), hold on
plot(t,subs(S2,x,t))
axis([-2,2 -2,2])
%习题2-24
x=dsolve('Dx=a*t^2+b*t','x(0)=2','t')
%习题2-25
[f,g]=dsolve('Df=3*f+4*g','Dg=-4*f+3*g','f(0)=0','g(0)=1','x')
第三章
%习题3-1
a=0:2*pi/9:2*pi
b=linspace(0,2*pi,10)
%习题3-2
rand( 'twister',0),
A=rand(3,5)
[I1,J1]=find(A>0.5)
subindex_A=sub2ind(size(A),I1,J1)
subindex_A=find(A>0.5)
[I,J]=ind2sub(size(A),subindex_A)
index_A=[I J]
%122_2
rand('twister',0),A=rand(3,5),B=A>0.5,C=find(B)
[ci,cj]=ind2sub(size(A),C) %此法太繁
[Ci,Cj]=find(B)
%122_5
t=linspace(1,10,100)
(1) y=1-exp(-0.5.*t).*cos(2.*t),plot(t,y)
(2) L=length(t)
for k=1:L, yy(k)=1-exp(-0.5*t(k))*cos(2*t(k)), end, plot(t,yy)
%122_6
clear,format long, rand('twister',1),A=rand(3,3),
B=diag(diag(A)),C=A.*(~B)%或C=A-B%或C=triu(A,1)+tril(A,-1)
%习题3-3
% s=sign(randint(1,1000,[],123)-.5);
% n=sum(s==-1)
rand('state',123),
s=sign(rand(1,1000)-.5),
n=sum(s==-1)
%习题3-4
clear all
A=[1 2;3 4]
B1=A.^(0.5), B2=A^(0.5),
A1=B1.^2
A2=B2^2
A1-A
norm(A1-A)
A1-A2
norm(A1-A2)
clear all
A=sym('[1 2;3 4]')
B1=A.^(0.5), B2=A^(0.5),
A1=simple(B1.^2)
A2=simple(B2^2)
A1-A
vpa(A1-A)
A1-A2
vpa(A1-A2)
%习题3-5
t=linspace(1,10,100)
%(1)
L=length(t)
for k=1:L
yy(k)=1-exp(-0.5*t(k))*cos(2*t(k)) end
figure(1),plot(t,yy)
%(2)
y=1-exp(-0.5.*t).*cos(2.*t),
figure(2),plot(t,y)
figure(3),subplot(1,2,1),plot(t,y) subplot(1,2,2),plot(t,yy)
%习题3-6
clear,format long,
rand('twister',1),A=rand(3,3),
B=diag(diag(A)),
C=A.*(~B)
%或C=A-B%或C=triu(A,1)+tril(A,-1) %习题3-7
clear,
x=-3*pi:pi/15:3*pi; y=x;
[X,Y]=meshgrid(x,y); % X=ones(size(y))*x;Y=y*ones(size(x)); warning off;
Z=sin(X).*sin(Y)./X./Y;
% (1)“非数”数目
m=sum(sum(isnan(Z))); %k=Z(isnan(Z));m=length(k)
% (2)绘图
surf(X,Y,Z); shading interp
% (3)“无裂缝”图形的全部指令:
x=-3*pi:pi/15:3*pi;y=x';
[X,Y]=meshgrid(x,y); % X=ones(size(y))*x;Y=y*ones(size(x));
X=X+(X==0)*eps;
Y=Y+(Y==0)*eps;
Z=sin(X).*sin(Y)./X./Y;
surf(X,Y,Z);shading interp
%习题3-8
clear
for k=10:-1:1;
A=reshape(1:10*k,k,10);
Sa(k,:)=sum(A,1);
end
Sa
clearmatlab速成教程
for k=10:-1:1;
A=reshape(1:10*k,k,10);
if k==1
Sa(k,:)=A;
else
Sa(k,:)=sum(A);
end
end
Sa
第四章
%习题4-1
clear all
load prob_data401;
diff_y=diff(y)./diff(t);
gradient_y=gradient(y)./gradient(t);
% plot(t(1:end-1),diff_y,t,gradient_y)
figure(1)
subplot(1,2,1),plot(t,y,t(1:end-1),diff_y)
subplot(1,2,2),plot(t,y,t,gradient_y)
%上面结果不好因自变量采样间距太小,将间距增大后结果较好
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论