The most dry goods of MATLAB (4)

The dry goods of MATLAB (four)-built-in functions

Up to this point, the content part I want to share is over half. Today, I brought the built-in function part in MATLAB , elbow~~

1. Polynomial

1. Create

p = [1,2,3,4];  % 系数向量,按x降幂排列,最右边是常数
f1 = poly2str(p,'x');  % 生成好看的字符串 f1 = x^3 + 2 x^2 + 3 x + 4,不被认可的运算式
f2 = poly2sym(p);  % 生成可用的符号函数 f2 = x^3 + 2*x^2 + 3*x + 4

2. Evaluation

x = 4;
y1 = poly2val(p,x);  % 代入求值;若x1为矩阵,则对每个值单独求值

3. Find the root

r = roots(p);
p0 = ploy(r);  % 由根求系数,结果为系数矩阵

2. Data interpolation

1. One-dimensional interpolation — %yi = interp1(X, Y, xi,'method')

X = [-3,-1,0,1,3];
Y = [9,1,0,1,9];   % XY为已知横纵坐标向量
y2 = interp1(X,Y,2);  % 差值预估在x=2的y的值,x不能超过已知范围(此处x<3)
y2m = interp1(X,Y,2,'spline');  %用spline方法(三次样条)差值预估在x = 2 的 y 的值

2. Two-dimensional interpolation— %zi = interp1(X, Y, Z, xi, yi,'method')

3. Statistics

1. Prepare the data

> X = [2, 3, 9, 15, 6, 7, 4];
> A = [1, 7, 2; 9, 5, 3; 8, 4 ,6];
> B = [1, 7, 3; 9, 5, 3; 8, 4 ,6];

2. Statistics

(1) The maximum and minimum values ​​of the matrix

y = max(X);  % 求矩阵X的最大值  min同理
[y,k] = max (X);   % 求最大值, k为该值的角标
[y,k] = max (A,[],2);  % A是矩阵,'2'时返回y每一行最大元素构成的列向量,k元素所在列;  '1'时同理

(2) Mean and median

y = mean (X);   % 均值
y = median (X);  % 中值
y = mean (A,2);  % '2'时返回y每一行均值构成的列向量;'1'时同理
y = median(A,2);  % '2'时返回y每一行中值构成的列向量;'1'时同理

(3) Sort

y = sort(A,1,'ascend');   % sort(矩阵, dim, 'method')dim为1按列排序,2按行排序;ascend升序,descend降序

Insert picture description here

[Y, I] = sort(A, 1, 'ascend');    % I保留了元素之前在A的位置

Insert picture description here
(4) Sum, multiply, accumulate, accumulate

y = sum (X);  %求和
y = prod(X);  %求积
y = cumsum(X);  %累加
y = cumprod(X);  %累乘

4. Numerical calculation

1. The most (extreme) value

Multivariate function finds the minimum point near the given initial value

 x = fminsearch(fun, x0);

Function zero

  x = fzero(fun, x0);  % 在给定初值x0附近找零点

This is the end of this section. You must go to matlab for verification if you don't understand it. Some things are really abstract, but the results of the execution will be very intuitive and can help you understand better.
Bye~~~

Guess you like

Origin blog.csdn.net/weixin_49005845/article/details/109773796