2023 Nanjing University of Posts and Telecommunications Tongda College "Mathematical Experiment" MATLAB experiment answers

Weixia is in April, and summer is in June.
Diligence will encourage, don't look forward to the morning.

——Gift nmy

a statement

Nanjing University of Posts and Telecommunications Tongda College "Mathematics Experiment" MATLAB experiment answers Answer
update time: 2023.04.28, revised the questionable part of 4.2. The update has been completed. If there is no error, it will not be updated . For the convenience of calculation, I define m

as an independent variable operation in the code or directly substitute m=117. It can be directly substituted in the homework, that is, m does not appear in the code . The version of this machine is MATLAB R2020b. Due to the author's limited answering ability, it is inevitable that there are flaws and mistakes. Please bear with me! This answer is for learning reference only, please do not plagiarize directly. Please correct me if there are mistakes or omissions. Contact QQ: 1415520898, if you have any questions, you can communicate through QQ or leave a message in the comment area.insert image description here



Two MATLAB download

The latest MATLAB R2020b ultra-detailed installation tutorial (complete installation file attached) is referenced here to the relevant articles of the blogger @dew_142857. The actual test is effective, and you can follow the steps step by step. The disk link is placed below. In addition, the installed MATLAB is about 96.6 GB . Please plan the disk space in advance.

Link: https://pan.baidu.com/s/1NExZ_v-QN4Xbu4Jk1C0dEA
Extraction code: 7won

You can also register an account at https://matlab.mathworks.com/ and use it online directly
insert image description here

"Mathematics Experiment" Exercise 1

1.1

insert image description here
insert image description hereinsert image description here

log(x)——>lnx; inf——>infinity

1.2

Please add a picture description
insert image description here

exp(x)——>eˣ; diff(y,x,n)——>n derivative of y to x

1.3

insert image description here
insert image description here
insert image description here

Don’t forget +C for the answer to the first small question; int ——> deal with definite integrals and indefinite integrals

1.4

insert image description here
2020 version
insert image description here

The full text should be taylor((117/200+sin(x))*cos(x),x,'Order',5,'ExpansionPoint',0), which can be omitted at x=0.

2010 edition
insert image description here

1.5

insert image description here

insert image description here

The random intermediate data this time is:

[8226958330713791/9007199254740992, (2^(1/2)*469134536469018791^(1/2))/671088640, ((2^(1/2)*469134536469018791^(1/2))/671088640 + 117/100)^(1/2), (((2^(1/2)*469134536469018791^(1/2))/671088640 + 117/100)^(1/2) + 117/100)^(1/2), ((((2^(1/2)*469134536469018791^(1/2))/671088640 + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2), (((((2^(1/2)*469134536469018791^(1/2))/671088640 + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2), ((((((2^(1/2)*469134536469018791^(1/2))/671088640 + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2), (((((((2^(1/2)*469134536469018791^(1/2))/671088640 + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2), ((((((((2^(1/2)*469134536469018791^(1/2))/671088640 + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2), (((((((((2^(1/2)*469134536469018791^(1/2))/671088640 + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2) + 117/100)^(1/2)]

1.6

insert image description here
insert image description here
insert image description here
insert image description here

There are many symbols used in this question, use clear to clear variables when proceeding to the next question

1.7

insert image description here
insert image description here
1.7.1
Please add a picture description
insert image description here
insert image description here
1.7.2
insert image description here

insert image description here
insert image description here

1.8

insert image description here
insert image description here
insert image description here

1.8.2
insert image description here
You can also use the following code, the effect is the same
insert image description here

insert image description here

1.9

insert image description here
insert image description here
insert image description here

1.10

Plot is to draw two-dimensional graphics, and the expressions of x and y are known or are exact expressions such as y=f(x). The basic calling format of the plot function is: plot(x,y) where x and y are vectors of the same length, which are used to store x-coordinate and y-coordinate data respectively.
ezplot is to draw implicit function graphics, which is shaped like f(x,y)=0, which cannot write a function like y=f(x) ezplot unary function drawing function ezplot(fun) ezplot(fun,[ min,max])
fplot(y,[a,b]) precise plotting

Please add a picture description

insert image description here
Please add a picture description

1.11

Please add a picture description
insert image description here
[X,Y] = meshgrid(-5:0.1:5); can be changed to the form in the book:
x=-5:0.1:5;y=x;
[XY]=meshgrid(x,y);
insert image description here

"Mathematics Experiment" Exercise 2

2.1

Please add a picture description
insert image description here

The first fixed point is -0.0084 and
the second fixed point is 119.0084

(2) Define a universal iterative method first, and use the M file to save
insert image description here
insert image description here
the function convergence. As long as the initial value does not take 14165^(1/2)/2+119/2, which is the second fixed point, the convergence value is the same as the initial value The selection of the value has little to do with it, and it always converges to -0.0084. Only when the initial value is 14165 ^(1/2)/2+119/2, the iterative function takes it as the limit; the convergence value must be one of the fixed points
;

2.2

Please add a picture description

m=117;
syms x;
f=inline('1-2*abs(x-1/2)');%设定函数
x0=1/4;%设定初值
for i=1:1:10
plot(i,f(x0),'*');%用*作图,可以在括号内添加'MarkerSize',20放大点
x0=f(x0); %更新x0的值,x0类似于C语言的static类型变量
hold on %将各个点划在一张图上
end
hold off

insert image description here

Several images tend to be 0 in the end. If not, the final value of i should be increased. The i=1:1:100 of my last three images;

insert image description here
insert image description here
insert image description here

2.3

This question is the second example on page P76
insert image description here
insert image description here

%MARTIN函数代码
function Martin(a,b,c,N) %N为迭代次数
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))];%表示矩阵m的第n+1列。冒号表示选择所有行
end
plot(m(1,:),m(2,:),'kx');
axis equal %横纵坐标采用相等单位长度
%循环迭代N次,N是预定义的数字。在循环内部,代码更新矩阵m中的值。 具体来说,该代码通过将其第一个元素设置为f(m(1,n),m(2,n)),将其第二个元素设置为g(m(1,n))来更新m的第n列。 第一行0后面的分号表示矩阵m初始化为两行N列的列向量。

m=117;
Martin(m,m,m,5000)
Martin(-m,-m,m,10000)
Martin(-m,m/1000,-m,15000)
Martin(m/1000,m/1000,0.5,20000)

insert image description here
insert image description here
insert image description here
insert image description here

2.4

insert image description here
insert image description here
(1)

%此小问无需在卷面作答,且每人选的数不一样,仔细看题目!!!
m=117;
syms x;
diff(subs((100*x+117)/(x^2+100),x,117^(1/3))) %对默认的变量进行一次的求导
%我取的是a=100,c=1;最后结果的绝对值应小于1才可以,否则另取
ans =
 
0

(2)

syms x;
m=117;
f=inline('(100*x+117)/(x^2+100)');
x0=10;% 任取一个初值
for i=1:20;
x0=f(x0);
fprintf('%g,%g\n',i,x0);
end

%我的运行结果
1,5.585
2,5.14893
3,4.99475
4,4.93387
5,4.90889
6,4.89849
7,4.89413
8,4.8923
9,4.89153
10,4.89121
11,4.89107
12,4.89101
13,4.89099
14,4.89098
15,4.89098
16,4.89097
17,4.89097
18,4.89097
19,4.89097
20,4.89097

(3)
Answer based on personal experience

The convergence speed of the function iteration has little to do with the selection of the initial value;
the initial value of the iteration has an influence on the convergence of the iteration, but there is uncertainty in this influence, and there is no law to follow;

Just change it in your own words

2.5

insert image description here

syms x;
y=sin(x);
y1=taylor(sin(x),x,'Order',2);
y2=taylor(sin(x),x,'Order',4);
y3=taylor(sin(x),x,'Order',6);
fplot([y y1 y2 y3])
xlim([-3/2*pi 3/2*pi])
grid on
legend('sin(x)','approximation of sin(x) up to O(x^1)','approximation of sin(x) up to O(x^3)','approximation of sin(x) up to O(x^5)')

insert image description here

(2)

syms x;
y=sin(x);
y1=taylor(sin(x),x,'Order',8);
y2=taylor(sin(x),x,'Order',10);
y3=taylor(sin(x),x,'Order',12);
fplot([y y1 y2 y3])
xlim([-3/2*pi 3/2*pi])
grid on
legend('sin(x)','approximation of sin(x) up to O(x^7)','approximation of sin(x) up to O(x^9)','approximation of sin(x) up to O(x^(11))')

insert image description here

(3)

insert image description here

"Mathematics Experiment" Exercise 3

3.1

insert image description here

A=str2sym('[117,117-4;6-117,10-117]');%表示符号表达式
[P,D]=eig(A);
Q=inv(P);
syms n;
x=[1;2];
xn=P*(D.^n)*Q*x 


xn =
 
(339*6^n)/2 - (337*4^n)/2 - (559*0^n)/111
        2*0^n + (337*4^n)/2 - (333*6^n)/2

3.2

insert image description here

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



xn =
 
(339*(3/5)^n)/2 - (337*(2/5)^n)/2 - (559*0^n)/111
        2*0^n + (337*(2/5)^n)/2 - (333*(3/5)^n)/2

3.3

insert image description here

%教材P136页原题
A=[9,5;2,6];
t=[];
for i=1:20
    x=2*rand(2,1)-1;
    t(length(t)+1,1:2)=x;
    for j=1:40
        x=A*x;
        t(length(t)+1,1:2)=x;
    end
end
plot(t(:,1),t(:,2),'*')
grid('on') 

insert image description here

(2) It can be seen that the iteration array seems to be on a straight line passing through the origin.
(3)

A=[9,5;2,6]; a=[];
x=2*rand(2,1)-1; 
for i=1:20
a(i,1:2)=x;
x=A*x;
end
for i=1:20
if a(i,1)==0
else t=a(i,2)/a(i,1);
fprintf('%g,%g\n',i,t);
end
end
%结果
1,0.911983
2,0.551028
3,0.451391
4,0.418261
5,0.406586
6,0.402388
7,0.400867
8,0.400315
9,0.400115
10,0.400042
11,0.400015
12,0.400006
13,0.400002
14,0.400001
15,0.4
16,0.4
17,0.4
18,0.4
19,0.4
20,0.4

(4)
The limit value is the slope of the straight line of the image

Organize any of the following in your own language

  1. The final stable value is one of the eigenvalues ​​of the iteration matrix.
  2. If the iteration matrix has multiple linearly independent eigenvectors corresponding to the same eigenvalue, then the final stable value will be the result of the linear combination of these eigenvectors.
  3. The stable value is the eigenvector of the iterative matrix, and the corresponding eigenvalue is 1. The eigenvalues ​​and eigenvectors of the iteration matrix can be obtained through the characteristic equation.

3.4

insert image description here
Book P141 similar questions

m=117;
A=[m-1,m;1-m,-m];
p=[0.4;0.6];%选择合适初始向量,要求和为1
[P,D]=eig(A)%P每列是特征向量,D主对角线元素是特征值
for i=1:20
    p(:,i+1)=A*p(:,i);
end
fprintf('%2f,%2f\n',p)

You can also use the following method to find the stable value

m=117;
A=[m,1/4-m;m-3/4,1-m];
x0=[0.4;0.6];
n=10000;
y = A^n * x0

result

%A=[m,6-m;m-2,8-m]
%A=[m,1/4-m;m-3/4,1-m]
%A=[m-1,m;1-m,-m]

(4) ps: This question is difficult, you can give it up appropriately

In linear map iteration, the stability of the iterated matrix depends on the size and distribution of its eigenvalues. The eigenvalue is an important property of the matrix, which describes the change of the matrix under the linear transformation.

If the absolute values ​​of all eigenvalues ​​of the iteration matrix are less than 1, then the iteration matrix is ​​stable, and the element values ​​of the matrix will approach a stable value after each iteration.

However, if the absolute value of the eigenvalues ​​of the iteration matrix is ​​greater than or equal to 1, then the iteration matrix is ​​unstable. In this case, the element values ​​of the matrix will approach infinity or infinitesimal after each iteration, resulting in invalid iteration results.

In addition, if the iteration matrix has multiple eigenvalues ​​that are the same, the iteration matrix may also be unstable. In this case, the eigenvectors of the iterated matrix may fluctuate by a very large magnitude, making the iterated results unreliable.

Therefore, for the linear map iteration of a two-dimensional matrix, it is necessary to analyze the eigenvalues ​​of the iterated matrix to determine its stability. If the iteration matrix is ​​unstable, some measures need to be taken, such as adjusting the iteration step size or using a more stable iteration algorithm, to ensure the reliability of the iteration results.

3.5

insert image description here

%如果默认b>a
>> I=0;
>> m=[];
>> n=1000;
>> for a=1:n
for c=a+1:n
b=sqrt(c^2-a^2);
if(b==floor(b))&(b>a)&(c==b+2)
I=I+1;m(:,I)=[a,b,c];
end
end
end
>> m

m =

  117

     6     8    10    12    14    16    18    20    22    24    26    28    30    32    34    36    38
     8    15    24    35    48    63    80    99   120   143   168   195   224   255   288   323   360
    10    17    26    37    50    65    82   101   122   145   170   197   226   257   290   325   362

  1829

    40    42    44    46    48    50    52    54    56    58    60    62
   399   440   483   528   575   624   675   728   783   840   899   960
   401   442   485   530   577   626   677   730   785   842   901   962

>>

公式:a=2m b=m^2-1 c=m^2+1(m>2,m为整数);
即:
{
    
    a,b,c}={
    
    (2u)^2,(u^2-1)^2,(u^2+1)^2}

The default is b>a in class, and the code for when the relationship between a and b is uncertain is given below, which does not need to be written on the test paper

abc0=zeros(1000,3);
k=0;
for c=3:1000
b=c-2;
a=sqrt(c^2-b^2);
if(mod(a,1)==0)
k=k+1;
abc0(k,:)=[a b c];
end
end
abc=abc0(1:k,:);
fprintf('所有勾股数 a b c=\n')
disp(abc)

3.6

insert image description here

for k=1:200 
	for b=1:999
		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

1,2,8,9,18,25,32,49,50,72,81,98,121,128,162,169,200

k is a perfect square number or double the perfect square number.
It is predicted that k is 200,225,242,288,289 between [200,300].

"Mathematics Experiment" Exercise 4

4.1

insert image description here

% 方法一:通过法方程组求解
d0=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];
d1=sum(x);d2=sum(x.^2);b1=sum(y);b2=sum(y.*x);
A=[d0,d1;d1,d2];B=[b1;b2];
u=A\B;
a0=u(1)
a1=u(2)
error=sum((y-(a0+a1.*x)).^2)

a0 =

    2.8304


a1 =

    4.0244


error =

    0.2409
%方法二:直接求解
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];
P=polyfit(x,y,1)


P =

    4.0244    2.8304


error=sum((y-(2.8304+4.0244.*x)).^2)%误差


error =

    0.2409

insert image description here

4.2

insert image description here
(1)

%我的学号尾数是7;故数据是到1920年,对应人口是106.5,第14,
%则t2=t(14),x2=x(14)
%这段代码是用来进行数据拟合的,其中变量t和x分别代表时间和数据点。代码用log函数将数据点转换成线性形式,然后使用线性回归来拟合两个数据点的斜率和截距,最后用指数函数求出x0和k,从而得到新的函数曲线。代码中的error表示新的函数曲线与原数据点的误差平方和
t=1790:10:1980;
x=[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];
t1=t(1);x1=x(1);
t2=t(14);x2=x(14); %此步根据学号不同而不同
A=[1,t1;1,t2];
b=[log(x1);log(x2)];
u=A\b;
x0=exp(u(1))
k=u(2)
error=sum((x0*exp(k*t)-x).^2)


x0 =

   6.5242e-20


k =

    0.0254


error =

   1.2278e+05

(2)

t=1790:10:1920;
x=[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];
%我的数据是到1920,所以上面的数据是截到1920年对应的106.5
y=log(x); 
m=length(t);
A=[m,sum(t);sum(t),sum(t.^2)]; 
b=[sum(y);y*t'];
u=A\b;
x0=exp(u(1))
k=u(2)
error=sum((x0*exp(k*t)-x).^2)


x0 =

   2.7207e-20


k =

    0.0260


error =

  681.9588

4.3

insert image description here

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.01];
f=@(a,x)a(1)./(1+a(2)*exp(-a(3)*x));
[A,resnorm]=lsqcurvefit(f,a,x,y)
f(A,20)



A =

   1.0e+03 *

    5.7882    0.0025    0.0001


resnorm =

   3.3995e+04


ans =

   4.7438e+03

4.4

insert image description here

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=27;
while    f(A,t+1)-f(A,t)>=10
 t=t+1;
end
f(A,t)




A =

   1.0e+03 *

    5.3860    0.0021    0.0001    0.0000


resnorm =

   9.1025e+03


ans =

   5.3409e+03

4.5

insert image description here

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=[2,0.1,0.1];%r、k、a
f=@(a,x)a(1)*exp(a(2)*x+a(3));
[A,resnorm]=lsqcurvefit(f,a,x,y)
 t=27;
while    f(A,t+1)-f(A,t)>=10
 t=t+1;
end
f(A,t)

A =

    2.4511    0.3152   -0.0841


resnorm =

   2.3628e+08


ans =

   Inf

Guess you like

Origin blog.csdn.net/yanqiu12138/article/details/129806029