## 模块一：基础练习

### 1.1

``````clear
syms x
m=628;
f=(log(1+x-m*x^2)-x)/(1-cos(x));
limit(f)
f=((sqrt(m*x^2+2)-atan(m*x))/x);
limit(f,x,inf)
pretty(limit(f,x,inf))``````

### 1.2

``````clear
syms x
m=628;
f=exp(m*x)*sin(x);
diff(f,2)
subs(f,x,0);
diff(f,6)``````

### 1.3

``````clear
syms x
m=628;
f=(x+sin(x))/(1+cos(x));
int(f,x)
pretty(int(f,x))
f=log(1+m*x)-m*x;
int(f,x,0,1)
pretty(int(f,x,0,1))``````

### 1.4

``````clear
syms x
m=628;
f=cos(x*(m/200+sin(x)));
taylor(f,x,'Order',5,'ExpansionPoint',0)
pretty(taylor(f,x,'Order',5,'ExpansionPoint',0))``````

### 1.5

``````clear
x=[];
x(1)=rand(1);
m=628;
for k=2:10
x(k)=sqrt(m/100+x(k-1));
end
x``````

rand函数见书P5页

### 1.6

``````clear
m=628;
A=[4,2,m-2;-3,0,5;1,5,2*m]
B=[3,4,0;2,0,-3;-2,1,1]
det(A)
inv(A)
eig(A)
[P,D]=eig(A);
X=P(1:3,1:1)
Y=P(1:3,2:2)
Z=P(1:3,3:3)
A\B
A/B
C=[A B];
Q=rref(C)``````

### 1.7

#### 1.7.1

f.m

``````function y=f(x)
if 0<=x&&x<=1/2
y=2*x;
elseif 1/2<x&&x<=1
y=2*(1-x);
end``````

g.m

``````function y=g(x,f)
n=length(x);
for i=1:n
y(i)=f(x(i));
end
end``````

main

``fplot(@(x)g(x,@f),[0,1])``

#### 1.7.2

f.m

``````function y=f(x)
if -pi<=x&&x<=0
y=x-1;
elseif 0<x&&x<=pi
y=x+1;
end``````

g.m

``````function y=g(x,f)
n=length(x);
for i=1:n
y(i)=f(x(i));
end
end``````

main

``fplot(@(x)g(x,@f),[-pi,pi])``

### 1.8

#### 1.8.1

``````clear
m=628;
t=-m/100:0.1:m/100;
x=(m/20)*cos(t);
y=(m/20)*sin(t);
z=t;
plot3(x,y,z)
grid on``````

#### 1.8.2

``````clear
m=628;
t=-m/100:0.1:m/100;
x=cos(t)+t.*sin(t);
y=sin(t)-t.*cos(t);
z=-t;
plot3(x,y,z)
grid on``````

### 1.9

``````clear
m=628;
x=-30:0.1:30;
t=[1000/m,500/m,100/m];
for i=1:3
j=t(i);
y=1/(sqrt(2*pi*j))*exp(-(x.^2)/2*(j^2));
plot(x,y)
hold on
end``````

### 1.10

``````clear
ezplot('log(x^2+y*628)-(x^3)*y-sin(x)')``````

### 1.11

``````clear
m=628;
x=-5:0.1:5;
y=-5:0.1:5;
[x,y]=meshgrid(x,y);
z=m.*x.*exp(-(x.^2+y.^2));
mesh(x,y,z)``````

### 1.12

``````clear
syms x
m=628;
y=x^3+sqrt(m)*x^2+(m/3-3)*x-sqrt(m)*(1-m/27);
fplot(x,y,[-sqrt(m)/3-2,-sqrt(m)/3+2])
axis([-10.5,-6,-2.5,2.5])
grid on
fzero('x^3+sqrt(628)*x^2+(628/3-3)*x-sqrt(628)*(1-628/27)',-10)
fzero('x^3+sqrt(628)*x^2+(628/3-3)*x-sqrt(628)*(1-628/27)',-8)
fzero('x^3+sqrt(628)*x^2+(628/3-3)*x-sqrt(628)*(1-628/27)',-6)
diff(y)
figure
fplot(x,diff(y),[-sqrt(m)/3-2,-sqrt(m)/3+2])
axis([-10,-7,-3,1])
grid on
fzero('3*x^2 + 4*157^(1/2)*x + 619/3',-10)
fzero('3*x^2 + 4*157^(1/2)*x + 619/3',-7)``````

## 模块二：函数的迭代

### 2.1

``````clear
syms x
m=628;
y=(2*x+1)/(x-m)-x;
solve(y,x)``````

dd.m

``````function p=dd(f,x0,n)
p=x0;
for i=2:n
p(i)=f(p(i-1));
end
end``````

main

``dd(@(x)(2*x+1)/(x-m),___,20)``

### 2.2

``````clear
m=628;
f=@(x)(1-2*abs(x-1/2));
t=[1/4,1/10,1/100,1/m];
for j=1:4
subplot(2,2,j)
x0=t(j);
for i=1:1:100
plot(i,f(x0),'+');
x0=f(x0);
hold on
end
end``````

### 2.3

Martin.m

``````function Martin(a,b,c,N)
m=628;
f=@(x,y)(y-sign(x)*sqrt(abs(b*x-c)));
g=@(x)(a-x);
m=[0;0];
for n=1:N
m(:,n+1)=[f(m(1,n),m(2,n)),g(m(1,n))];
end
plot(m(1,:),m(2,:),'kx')
axis equal``````

main

``````m=628
subplot(2,2,1)
Martin(m,m,m,5000)
subplot(2,2,2)
Martin(m/1000,m/1000,0.5,10000)
subplot(2,2,3)
Martin(m/1000,m,-m,15000)
subplot(2,2,4)
Martin(-m/10,17,4,20000)``````

### 2.4

``````clear
m=628;
syms x a b c d e;
diff((a*x+m*c)/(c*x^2+a))
pretty(diff((a*x+m*c)/(c*x^2+a)))
x=m^(1/3)
vpa(diff(subs((100*x+m)/(x^2+100),x,m^(1/3))),8)
f=@(x)(100*x+628)/(x^2+100);
for j=1:5
x0=(100*rand-50)
for i=1:20
x0=f(x0);
fprintf('%g,%g\n',i,x0)
end
end``````

### 2.5

``````clear
syms x
y=sin(x);
y1=taylor(y,x,'Order',2,'ExpansionPoint',0);
y2=taylor(y,x,'Order',4,'ExpansionPoint',0);
y3=taylor(y,x,'Order',6,'ExpansionPoint',0);
y4=taylor(y,x,'Order',8,'ExpansionPoint',0);
y5=taylor(y,x,'Order',10,'ExpansionPoint',0);
y6=taylor(y,x,'Order',12,'ExpansionPoint',0);
subplot(1,2,1)
fplot([y,y1,y2,y3],[-3*pi/2,3*pi/2])
grid on
subplot(1,2,2)
fplot([y,y4,y5,y6],[-3*pi/2,3*pi/2])
grid on``````

## 模块三:线性映射的迭代

### 3.1、3.2

``````clear
A=str2sym('[628,628-4;6-628,10-628]');
[P,D]=eig(A);
Q=inv(P);
syms n;
x=[1;2];
xn=P*(D.^n)*Q*x
B=1/10.*A
[P,D]=eig(B);
Q=inv(P);
xn=P*(D.^n)*Q*x``````

### 3.3

``````clear
A=[9,5;2,6]
for k=1:20
x=[2*rand-1;2*rand-1]
t=[];
for i=1:40
x=A*x;
t(i,1:2)=x;
end
for j=1:40
if t(j,1)==0
else a=t(j,2)/t(j,1);
fprintf('%g,%g\n',j,a);
end
end
plot(t(1:40,1),t(1:40,2),'+')
grid on
hold on
end``````

### 3.4

``````clear
m=628;
A=[3/4,7/18;1/4,11/18];
A(:,:,2)=[m,6-m;m-2,8-m];
A(:,:,3)=[m,1/4-m;m-3/4,1-m];
A(:,:,4)=[m-1,m;1-m,-m];
for i=1:4
eig(A(:,:,i))
[P,D]=eig(A(:,:,i))
X=P(1:2,1:1)
Y=P(1:2,2:2)
for j=1:20
x=[2*rand-1;2*rand-1];
t=[];
for k=1:40
x=A(:,:,i)*x;
t(k,1:2)=x;
end
t
subplot(2,2,i)
plot(t(1:40,1),t(1:40,2),'+')
grid on
hold on
end
end``````

## 模块四：种群增长模型与综合实验

### 4.1、4.2

``````clear
f=[1,2,4,5]
for i=1:4
for b=1:1000
a=sqrt((b+f(i))^2-b^2);
if (a==floor(a))
fprintf(['a=%4i,b=%4i,c=%4i|'],a,b,b+f(i))
end
end
fprintf('\n')
end``````

### 4.3

``````clear
for k=1:200
for b=1:1000
a=sqrt((b+k)^2-b^2);
if((a==floor(a))&&gcd(gcd(a,b),(b+k))==1)
fprintf('%i,',k);
break;
end
end
end``````

### 4.4

``````clear
syms a0 a1
m=9;
x=[1.5,1.8,2.4,2.8,3.4,3.7,4.2,4.7,5.3];
y=[8.9,10.1,12.4,14.3,16.2,17.8,19.6,22.0,24.1];
polyfit(x,y,1)
x1=sum(x);
x2=sum(x.^2);
y1=sum(y);
xy=sum(y.*x);
eql1=m*a0+x1*a1==y1
eql2=x1*a0+x2*a1==xy
[a0,a1]=solve([eql1,eql2],[a0,a1])
error=sum((y-(a0+a1.*x)).^2)
f=a0+a1*x;
plot(x,y,'+',x,f);``````

### 4.5

``````clear
syms k x0
X=1790:10:1980;
x=1790:10:1930;
Y=[3.9,5.3,7.2,9.6,12.9,17.1,23.2,31.4,38.6,50.2,62.9,76,92,106.5,123.2,131.7,150.7,179.3,204.0,226.5];
y=[3.9,5.3,7.2,9.6,12.9,17.1,23.2,31.4,38.6,50.2,62.9,76,92,106.5,123.2];
Z=log(Y);
z=log(y);
A=[1,x(1);1,x(15)];
B=[z(1);z(15)];
u=A\B;
x0=u(1)
k=u(2)
error=sum((x0*exp(k*x)-y).^2)
f=exp(x0+k*X);
plot(X,f,X,Y,'+');
hold on
t=polyfit(X,Z,1);
X0=t(2)
K=t(1)
f=exp(X0+K*X);
error=sum((X0*exp(K*X)-Y).^2)
plot(X,f);``````

### 4.6

``````clear
x=1:26;
y=[1807,2001,2158,2305,2422,2601,2753,2914,3106,3303,3460,3638,3799,3971,4125,4280,4409,4560,4698,4805,4884,4948,5013,5086,5124,5163];
a=[6000,2,0.1];
f=@(a,x)a(1)./(1+a(2)*exp(-a(3)*x));
[A,resnorm]=lsqcurvefit(f,a,x,y);
t=26;
while abs(f(A,t)-f(A,t+1))>1
t=t+1;
end
f(A,t)
t
t=1:t;
plot(x,y,'+',t,f(A,t))``````

### 4.7

``````clear
x=1:26;
y=[1807,2001,2158,2305,2422,2601,2753,2914,3106,3303,3460,3638,3799,3971,4125,4280,4409,4560,4698,4805,4884,4948,5013,5086,5124,5163];
a=[6000,2,0.1,0.1];
f=@(a,x)a(1)./(1+a(2)*exp(-a(3)*x-a(4)*x.^2));
[A,resnorm]=lsqcurvefit(f,a,x,y);
t=26;
while abs(f(A,t)-f(A,t+1))>1
t=t+1;
end
f(A,t)
t
t=1:t;
plot(x,y,'+',t,f(A,t));``````

### 4.8

``````clear
x=1:27;
y=[21,65,127,281,558,923,1321,1801,2420,3125,3886,4638,5306,6028,6916,7646,8353,9049,9503,10098,10540,10910,11287,11598,11865,12086,12251];
a=[13000,2,0.1];
f=@(a,x)a(1)./(1+a(2)*exp(-a(3)*x));
[A,resnorm]=lsqcurvefit(f,a,x,y);
t=27;
while abs(f(A,t)-f(A,t+1))>1
t=t+1;
end
f(A,t)
t
t=1:t;
plot(x,y,'+',t,f(A,t));
hold on
a=[13000,2,0.1,0.1];
f=@(a,x)a(1)./(1+a(2)*exp(-a(3)*x-a(4)*x.^2));
[A,resnorm]=lsqcurvefit(f,a,x,y);
t=27;
while abs(f(A,t)-f(A,t+1))>10
t=t+1;
end
f(A,t)
t
t=1:t;
plot(t,f(A,t));``````

### 4.9

``````clear
x=1:27;
y=[21,65,127,281,558,923,1321,1801,2420,3125,3886,4638,5306,6028,6916,7646,8353,9049,9503,10098,10540,10910,11287,11598,11865,12086,12251];
a=[2,0.1,0.1];
f=@(a,x)a(1)*exp(exp(a(2)*x+a(3)));
[A,resnorm]=lsqcurvefit(f,a,x,y);
t=27;
while abs(f(A,t)-f(A,t+1))<1
t=t+1;
end
f(A,t)
t
t=1:t;
plot(x,y,'+',t,f(A,t));``````

## 综合实验

``````clear
for j=1:25
p=rand;
AA=p*p;
Aa=2*p*(1-p);
aa=(1-p)*(1-p);
K=AA+Aa+aa;
for i=1:2
x(i)=AA(i)*AA(i)+AA(i)*Aa(i)*1/2+Aa(i)*AA(i)*1/2+Aa(i)*Aa(i)*1/4;
y(i)=AA(i)*Aa(i)*1/2+AA(i)*aa(i)+Aa(i)*AA(i)*1/2+Aa(i)*Aa(i)*1/2+Aa(i)*aa(i)*1/2+aa(i)*AA(i)+aa(i)*Aa(i)*1/2;
z(i)=aa(i)*aa(i)+Aa(i)*Aa(i)*1/4+Aa(i)*aa(i)*1/2+aa(i)*Aa(i)*1/2;
k=x+y+z;
AA(i+1)=x(i);
Aa(i+1)=y(i);
aa(i+1)=z(i);
end
tu=[AA,;Aa,;aa,];
subplot(5,5,j);
plot(1:3,tu');
grid on
title(j);
legend('AA','Aa','aa');
end``````