Representing Polynomials in MATLAB
- Polynomials were represented as row vectors
- For example: f(x) = x^3 - 2*x - 5
- to enter this polynomial into MATLAB, use
p = [1 0 -1 -5];
values of polynomials : polyval()
- plot the polynomial
f(x) = 9x^3 - 5x^2 + 3x + 7 (for -2 <= x <= 5)
1 | a = [9, -5, 3, 7]; |
Polynomial Differentiation微分: polyder()
1 | p = [5 0 -2 0 1]; |
算某处微分值:
1 | p = [5 0 -2 0 1]; |
Polynomial Integration积分 : polyint()
需要多给matlab一个常熟项,积分后会出现一个常数项
1 | p = [5 0 -2 0 1]; |
在某处的值
1 | polyval(polyint(p, 3), 7) |
Numerical数值 Differentiation微分
Differences: diff()
- diff() calculates the differences between adjacent相邻 elements of a vector
1 | x = [1 2 5 2 1]; |
obtain the slope坡度 of a line between 2 points (1, 5) and (2, 7)
1 | x = [1 2]; y = [5 7]; |
Numerical Differentiation Using diff()
- Given f(x) = sin(x), find f`(x0) at x0 = pi/2 using h = 0.1
1 | x0 = pi/2; h = 0.1; |
How to Find the f` over an interval [0, 2Π]?
1 | h = 0.05; x = 0:h:2*pi; |
Second and Third Derivatives 二次/三次微分
1 | x = -2:0.005:2; y = x.^3; |
Numerical Integration
用矩形或梯形来估算面积大小来作为积分值
Midpoint矩形估算 Rule Using sum()
底乘高,底是h , 高是中等分点的函数值
1 | h = 0.05; x = 0:h:2; |
Trapezoid Rule 梯形预测
上底加下底乘高除二
上底是x1的函数值 下底是x0的函数值 高是h
1 | h = 0.05; x = 0:h:2; y = 4*x.^3; |
或者:
1 | h = 0.05; x = 0:h:2; y = 4*x.^3; |
Second-order Rule: 1/3 Simpson’s
计算面积的公式为:
h/3*(f0 + 4f1 + f2)
Review of Function Handles (@)
- A handle is a pointer to a function
- can be used to pass functions to other functions
1 | function [y] = xy_plot(input, x) |
Try: xy_plot(@sin, 0:0.01:2*pi);
不能将函数传给函数,只能传一个函数的handle给另一个函数
Numerical Integration积分: integral()
1 | y = @(x) 1./(x.^3-2*x-5); |