MATLAB first experiment: MATLAB basics

Preface

In order to help students complete the painful experimental course design, the author posted the experimental results and codes he made in CSDN for students to learn and refer to. If there are any shortcomings or imperfect descriptions, please point them out and welcome your corrections!
This experiment was completed between the graduation project and graduation thesis writing, so the writing of this series of experimental reports was slow. At the same time, students are also welcome to submit contributions!

1. Experimental purpose

1. Understand the characteristics of Matlab data objects;
2. Master the creation method of arrays in MATLAB, and be familiar with array access and assignment;
3 , Be familiar with basic Matlab operation rules;
4. Master how to use Matlab help.

2. Experimental environment

One computer (with MATLAB7.0 or above software environment)

3. Experimental content and steps

Preview requirements before the experiment class:
1. Before the experiment class, please be familiar with the usage of related functions that need to be used in the experiment.
2. Pay attention to the relevant functions or variables that appear in the experiment. Please use the doc function name or help function name to view the corresponding matlab help document and syntax, understand its meaning, and correctly solve the problems in the experiment. .
1. Establish the following scalars and perform calculations:.
1) a = 10 a=10 a=10
2) b = 2.5 ∗ 1 0 23 b=2.5*10^{23} b=2.51023
3) c = 2 + 3 i c=2+3i c=2+3i(i imaginary number unit)
4) d = 2 s i n 85 ° 1 + e 2 d=\frac{2sin85°}{1+e^2} d=1+e22sin85°(e为natural number, this is the same as below)
5) x = 1 1 + e − ( a − 15 ) / 6 x=\frac{1}{1+e^{-(a-15)/6}} x=1+e(a15)/61
6) y = ( a + b 21 ) π y=(\sqrt{a}+\sqrt[21]{b})^{\pi} and=(a +21b )πAlso refer to sqrt, pi.
2. According to the characteristics of the vector, choose a suitable method to establish the following vector and perform calculation:
1) aVec=[3.1 1 9 2.6]
2) bVec=Insert image description here
3) cVec=[5 4.5 … -4.5 -5] (the values ​​in the vector range from 5 to -5, with a step size of -0.5).
4) d V e c = [ 1 0 0 1 0 0.01 … 1 0 0.99 1 0 1 ] dVec=[10^0 10^{0.01} … 10^{0.99} 10^1] dVec=[100100.01100.99101] (Produces an equilogarithmically spaced vector between 1 and 10, refer to logspace, and pay attention to the length of the vector).
5) x V e c = e − c V e c 2 / 2. 5 2 xVec=e^{-cVec^2/2.5^2} < /span>xVec=It iscVec2/2.52,e是自然数。
6) y V e c = ( a V e c T ) 2 + b V e c 2 yVec=\sqrt{(aVec^T)^2+bVec^2} yVec=(aVecT)2+bVec2 , a V e c T aVec^T aVecT表示 a V e c aVec aVec的转置
7) z V e c = log ⁡ 10 ( 1 / d V e c ) zVec=\log_{10}(1/dVec) zVec=log10(1/dVec) log ⁡ 10 \log_{10} log10Represents the logarithm with base 10, refer to log10

3. Create the following matrix:
1)Insert image description here

aMat is a 9×9 matrix whose elements are all 2; (refer to ones or zeros).
2)Insert image description here

bMat is a 9×9 matrix. Except for the elements on the main diagonal which are [1 2 3 4 5 4 3 2 1], the other elements are all 0. (Refer to diag).
3)Insert image description here

cMat is a 10×10 matrix, which can be generated from a 1:100 vector (refer to reshape)
4)Insert image description here
5) Generate a A 5×3 matrix of random integers fMat with values ​​ranging from -3 to 3. (Refer to rand and floor or ceil or fix)

4. Use the vectors and matrices generated in Questions 2 and 3 to calculate the following equations. Note that the operations in this question all use matrix operations. Pay attention to the changes in the size of the matrix before and after the operation.
1) x M a t = ( a V e c ⋅ b V e c ) ⋅ a M a t 2 xMat=(aVec·bVec)·aMat^2 < /span>xMat=(aVecbVec)aMat2
2) y M a t = b V e c ⋅ a V e c yMat=bVec·aVec yMat=bVecaVec,注意这里 a V e c ⋅ b V e c aVec·bVec aVecbVec与是不一样的
3) z M a t = ∣ ( c M a t ) ∣ ( a M a t ⋅ b M a t ) T zMat=\left|(cMat)\right|(aMat·bMat)^T zMat=(cMat)(aMatbMat)T 这里 ∣ ( c M a t ) ∣ \left|(cMat)\right| (cMat) c M a t cMat The determinant corresponding to cMat Value, refer to det.
5. Use matrix left division operation to solve linear equations
Insert image description here
doc inv queries the function of inv function and try to use this function to solve the equation of this question
6. Complete the following operations (record the instructions and execution results)
(1) Find the number of numbers between [100,999] that can be divided by 21.
Tips: First use the colon expression, and then use the find and length functions
(2) Create a string vector and delete the uppercase letters.
Requirement: In question (2), the string vector required to be created is the full spelling of your class name, where the class is XinXiAnQuan18, and the full first letter of the name is in capital letters, such as "Schumacher" "—Schumacher.
Assume that a certain student is the sixth child in Information 181 class, with student number 181000345. The corresponding string is: X i n X i 181 L a o L i u 181000345 XinXi181LaoLiu181000345 XinXi 181La oLiu1 81000 345
7. Textbook P362 Experiment 3 Experiment Content 1
Enter a 4-digit integer from the keyboard, encrypt it according to the following rules and output it. Encryption rules: Add 7 to each digit, then replace the number with the remainder divided by 10; then swap the first digit with the third digit, and the second digit with the fourth digit.

4. Experimental code

%Knight_Von_Schumacher
%开始答题...
%第一题
a=10;
b=2.5*10^23;
c=2+3i;
d=2*sind(85)/(1+exp(2));
x=1/(1+exp(-(a-15)/6));
y=(sqrt(a)+b^(1/21))^pi;
%第二题
aVec=[3.1 1 9 2.6];
bVec=[2.7;8;2;12];
cVec=5:-0.5:-5;
dVec=logspace(0,1,101);
xVec=exp(-cVec.^2/2.5^2);
yVec=sqrt((aVec)'.^2+bVec.^2);
zVec=log10(1./dVec);
%第三题
aMat=2*ones(9);
bMat=diag([1 2 3 4 5 4 3 2 1]);
cMat=reshape(1:100,[10,10]);
eMat=[-3 -2 0;6 15 8];
fMat=round(-3+6*rand(5,3));
%第四题
xMat=(aVec*bVec)*aMat.^2;
yMat=bVec*aVec;
zMat=det(cMat)*(aMat*bMat)';
%第五题
left=[2 -1 3;2 0 2;4 2 5];right=[1 6 4]';
c=inv(left)*right;
%第六题
num=length(find(rem(100:999,21)==0));
name='Ich_Bin_Knight_Von_Schumacher18103721729';delete=name<'A'|name>'Z';name(delete)
%第七题
number=input('please input number ranging from 1000 to 9999:');
a=rem((number-rem(number,1000))/1000+7,10);
b=rem((rem(number,1000)-rem(number,100))/100+7,10);
c=rem((rem(number,100)-rem(number,10))/10+7,10);
d=rem(rem(number,10)+7,10);
ans=c*1000+d*100+a*10+b;

5. Experimental requirements

1. Textbook P46, question 4 of the application questions.
Write the command to complete the following operations
(1) Assign the elements in columns 1, 3, and 5 in rows 2 to 5 of matrix A to matrix B.
(2) Delete the 5th element of matrix A.
(3) Find the size and dimension of matrix A.
(4) Replace the 0 element of vector t with machine zero.
(5) Convert the vector x containing 12 elements into a 3*4 matrix.

(1)B=A(2:5,1:2:5);
(2)A[5]=[];
(3)size(A);ndims(A);
(4)x=[0 0 0 0 1;1 2 3 0 0];x(find(x==0))=eps;
(5)y=reshape([1 2 3 4 5 6 7 8 9 0 1 2],3,4)

2. What is the difference between the usage of sin function and sind function in trigonometric functions? For example, if you want the sine value of 60 degrees, how should you use these two functions.
sin(A): requires A to be in radian system sind(A): requires A to be in angle system
e.g.: Calculate the sine value of 60 degrees:

ans=sin(pi/3)或ans=sind(60)

3. e represents a natural number, and ln represents a natural logarithm. What is the corresponding Matlab function? If you require a base 10 logarithm or a base 2 logarithm, what function should you use?

exp()、log()、log10()、log2()

4. According to the experimental results, when using the whos or who command to query workspace variables, these two commands check the characteristics of the results.

  1. The list given by who can only be arranged in alphabetical order by variable name; the list given by whos can be arranged in alphabetical order by variable name, or by size, number of bytes occupied, etc.
  2. who is used to list all variable names in the current workspace; whos is used to list all variables in the current workspace, as well as their names, sizes (such as the row and column dimensions of a matrix or array), and the number of bytes they occupy. , attributes and other information.

5. Practice the usage of save and load functions by yourself.
Omitted

Guess you like

Origin blog.csdn.net/sjx3161/article/details/124841733