Table of contents
1.Polynomial differential and integral calculus
1.2 Polynomial Differentiation
1.3 How to use Matlab correctly?
1.3.1 Matlab expresses polynomials
1.3.2 polyval() polynomial evaluation
1.3.3 polyder() polynomial differential
1.4.1 How to express correctly
1.4.2 polyint() polynomial integration
2. Numerical differentiation and integration
2.2 diff() calculates the difference
2.4 Second and third order derivatives
2.5.3 Simpson’s integral method
2.5.4 Comparison of three integration methods
3.1.1 Handle functions with parameters
3.1.2 Parameterless handle function
3.1.3 No parameters and no parentheses
3.4 integral() numerical integration
3.4.2 integral2() second-level integral
3.4.3 integral3() third-level integral
1.Polynomial differential and integral calculus
1.1 Differential
- The derivative of function f(x) is written as: F'(x) or
- The rate of change of function f(x) with respect to x
f (x0) represents the coefficient of the straight line tangent to the curve at point x0.
1.2 Polynomial Differentiation
For a polynomial:, its differential polynomial is:
1.3 How to use Matlab correctly?
1.3.1 Matlab expresses polynomials
In the above equation, how should we express it in Matlab (polynomials are expressed as row vectors in Matlab)
>> p=[1 0 -2 -5]
p =
1 0 -2 -5
1.3.2 polyval() polynomial evaluation
Syntax : y=polyval(p,x)
y= polyval(p,x)
Computes the value of the polynomial p
at x
each point . The argument p
is a vector of length n+1
whose elements are n
the coefficients of a sub-degree polynomial (ordered by descending powers):
Example 1:
Calculate the values of the polynomial p(x)=3x^2+2x+1 at points x=5, 7, and 9.
>> p = [3 2 1];
x = [5 7 9];
y = polyval(p,x)
y =
86 162 262
Example 2:
Graph the polynomial to evaluate it
>> a = [9,-5,3,7]; x = -2:0.01:5;
f = polyval(a,x);
plot(x,f,'LineWidth', 2);%设置线条粗度
xlabel('x轴'); ylabel('f(x)');%设置xy轴名称
set(gca, 'FontSize', 14)%设置字体大小
1.3.3 polyder() polynomial differential
Syntax 1: k=polyder(p)
k=polyder(p) returns the derivative of the polynomial represented by the coefficients in p
Example:
Create a vector to represent the polynomial p(x)=3x^5−2x^3+x+5.
p = [3 0 -2 0 1 5];
Use polyder to differentiate the polynomial, and the result is:
q = polyder(p)
q = 1×5
15 0 -6 0 1
Syntax 2:k= polyder(a,b)
Return the derivative of the product of the sum of a
polynomials b
Example:
Create two vectors to represent the polynomials a(x)=x^4−2x^3+11 and b(x)=x^2−10x+15.
a = [1 -2 0 0 11];
b = [1 -10 15];
q = polyder(a,b)
q = 1×6
6 -60 140 -90 22 -110
The final result is:
Syntax 3:[q,d] = polyder(a,b)
Return the derivative of the quotient of the sum of a
polynomials b
Example:
Create two vectors to represent the polynomial in the quotient
p = [1 0 -3 0 -1];
v = [1 4];
[q,d] = polyder(p,v)
q = 1×5
3 16 -3 -24 1
d = 1×3
1 8 16
The result is:
1.4 Polynomial integration
For a polynomial:
The integrating polynomial is:
1.4.1 How to express correctly
1.4.2 polyint() polynomial integration
Syntax: q=polyint(p,k) If there is only one parameter p, the default k is 0 , and the integration constant k is used to return the polynomial integral represented by the coefficient in p
Example:
Integrating the product of two polynomials
- Create vectors to represent polynomials
p = [1 0 -1 0 0 1];
v = [1 0 1];
- Multiply the polynomials and integrate the resulting expression using the integration constant k=3
k = 3;
q = polyint(conv(p,v),k)
q = 1×9
0.1250 0 0 0 -0.2500 0.3333 0 1.0000 3.0000
- Solve for the value of I by evaluating q over the integration range
a = 0;
b = 2;
I = diff(polyval(q,[a b]))
I =
32.6667
2. Numerical differentiation and integration
2.1 Numerical Differentiation
- The simplest method: finite difference approximation
- Calculate secants near x
2.2 diff() calculates the difference
diff() calculates the difference between adjacent elements in a vector
x = [1 2 5 2 1];
diff(x)
>> x = [1 2 5 2 1];
diff(x)
ans =
1 3 -3 -1
Example:
Find the slope between two points
>> x = [1 2]; y = [5 7];
slope = diff(y)./diff(x) %y的变化量/x的变化量
slope =
2
2.3 Accuracy of errors
What happens to the error when h=0.1, 0.01, 0.001s?
When h=0.1 :
>> x0 = pi/2; h = 0.1;
x = [x0 x0+h];
y = [sin(x0) sin(x0+h)];
m = diff(y)./diff(x)
m =
-0.0500
When h=0.01:
>> x0 = pi/2; h = 0.01;
x = [x0 x0+h];
y = [sin(x0) sin(x0+h)];
m = diff(y)./diff(x)
m =
-0.0050
When h=0.001:
>>
x0 = pi/2; h = 0.001;
x = [x0 x0+h];
y = [sin(x0) sin(x0+h)];
m = diff(y)./diff(x)
m =
-5.0000e-04
From the analysis: So when h->0, the error is smaller
g = colormap(lines); hold on;
for i=1:4
x = 0:power(10, -i):pi;
y = sin(x); m = diff(y)./diff(x);
plot(x(1:end-1), m, 'Color', g(i,:));%每次微分,系数的个数少1
end
hold off;
set(gca, 'XLim', [0, pi/2]); set(gca, 'YLim', [0, 1.2]);
set(gca, 'FontSize', 18); set(gca, 'FontName', 'symbol');
set(gca, 'XTick', 0:pi/4:pi/2);
set(gca, 'XTickLabel', {'0', 'p/4', 'p/2'});
h = legend('h=0.1','h=0.01','h=0.001','h=0.0001');
set(h,'FontName', 'Times New Roman'); box on;
2.4 Second and third order derivatives
>> x = -2:0.005:2; y = x.^3;
m = diff(y)./diff(x);
m2 = diff(m)./diff(x(1:end-1));
plot(x,y,x(1:end-1),m,x(1:end-2),m2);%每次导数系数减1
xlabel('x', 'FontSize', 18);
ylabel('y', 'FontSize', 18);
legend('f(x) =x^3','f''(x)','f''''(x)');
set(gca, 'FontSize', 18);
2.5 Numerical integration
- Quadrature method - approximating the integral with a finite set of points
2.5.1 Midpoint Rule
Example:
We can find by integrating:
But we can get by midpoint integration:
>> h = 0.05; x = 0:h:2;
midpoint = (x(1:end-1)+x(2:end))./2;
y = 4*midpoint.^3;
s = sum(h*y)
s =
15.9950
explain:
midpoint = (x(1:end-1)+x(2:end))./2;
2.5.2 Trapezoid Rule (trapz)
>> h = 0.05; x = 0:h:2; y = 4*x.^3;
s = h*trapz(y)
s =
16.0100
We can also customize this function:
h = 0.05; x = 0:h:2; y = 4*x.^3;
trapezoid = (y(1:end-1)+y(2:end))/2;
s = h*sum(trapezoid)
s =
16.0100
2.5.3 Simpson’s integral method
>> h = 0.05; x = 0:h:2; y = 4*x.^3;
s = h/3*(y(1)+2*sum(y(3:2:end-2))+...
4*sum(y(2:2:end))+y(end))
s =
16
2.5.4 Comparison of three integration methods
It is easy to get from the analysis : Simpson’s integral method has the smallest error
3. Function handle
A function handle is a Matlab data type that stores an association pointing to a function . Indirectly calling a function allows you to call the function without considering the calling location. Typical uses of function handles include:
- Passing one function into another function (often called a composite function)
- Specify callback function
- Constructs a function handle that is defined inline rather than stored in a program file (anonymous function)
- Call local function from outside main function
Check if h is a function handle
isa(h,'function_handle')
3.1 Create function handle
@
Create a handle to a function by adding a symbol before the function name .
f = @myfunction;
3.1.1 Handle functions with parameters
Calling a function using a handle is the same as calling the function directly, for example:
function y = computeSquare(x)
y = x.^2;
end
Create a handle and call the function to calculate 4 squared.
f = @computeSquare;
a = 4;
b = f(a)
b =
16
3.1.2 Parameterless handle function
If the function does not require any input, you can call the function using empty brackets, for example:
h = @ones;
a = h()
a =
1
3.1.3 No parameters and no parentheses
If parentheses are not used, the assignment creates another function handle .
a = h
a =
@ones
3.2 Anonymous functions
You can create a handle to an anonymous function. Anonymous functions are MATLAB functions based on single-line expressions and do not require a program file. Constructs a handle to an anonymous function by defining anonymous_function
a function body and arglist
a comma-separated list of input arguments to the anonymous function. The syntax is:
h = @(arglist)anonymous_function
For example, create a handle to an anonymous function that computes square numbers sqr
and call the anonymous function using its handle.
sqr = @(n) n.^2;
x = sqr(3)
x =
9
3.3 Error demonstration
function [y] = xy_plot(input,x)
% xy_plot receives the handle of a function
% and plots that function of x
y = input(x); plot(x,y,'r--');
xlabel('x'); ylabel('function(x)');
end
>> xy_plot(sin,0:0.01:2*pi)
错误使用 sin
输入参数的数目不足。
When calling a parameter, if the parameter is also a function, it is inappropriate to directly input the function as a parameter at this time, and an error will be reported directly. At this time, we need a handle
>>
xy_plot(@sin,0:0.01:2*pi)
ans =
列 1 至 11
0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0699 0.0799 0.0899 0.0998
列 12 至 22
0.1098 0.1197 0.1296 0.1395 0.1494 0.1593 0.1692 0.1790 0.1889 0.1987 0.2085
列 23 至 33
0.2182 0.2280 0.2377 0.2474 0.2571 0.2667 0.2764 0.2860 0.2955 0.3051 0.3146
...
3.4 integral() numerical integration
3.4.1 Level 1 points
How should we solve it through handles?
>> y = @(x) 1./(x.^3-2*x-5);
integral(y,0,2)
ans =
-0.4605
3.4.2 integral2() second-level integral
>> f = @(x,y) y.*sin(x)+x.*cos(y);
integral2(f,pi,2*pi,0,pi)
ans =
-9.8696
3.4.3 integral3() third-level integral
>> f = @(x,y,z) y.*sin(x)+z.*cos(y);
integral3(f,0,pi,0,1,-1,1)
ans =
2.0000