Table of contents
2.polynomial differentiation (polynomial differentiation)
5.polynomial integration (polynomial integration)
8. Use diff to calculate numerical differentiation
10. Calculating Interval Derivatives
11. Different h derivation exercises
12. Quadratic cubic differential
17. Use matlab built-in functions to do numerical integration
1.symbolic root finding approach
3. Find equations and practice with sin and cos
5. Solving equations expressed in symbols
6. Solving Equation Matrix Exercises
8. Exercises in symbolic differentiation
13. Exercises for Solving Linear Equations in Two Variables
20. Bisection method vs Newton's method
Trilinear Equations and Linear Systems
3. The linear part is skipped directly!
2. mean median mode quartile average median mode quartile value
3. range and interquartile range
4. Variance Standard Deviation
6.figures are always more powerful (chart description)
12. statistical hypothesis testing
Five Regression and Interpolation
1.simple linear regression simple linear regression
3. Polyfit practice voltage and temperature relationship
4. Is there a linear relationship between x and y?
7. Multiple variable forecasting
9. Interpolation vs Regression
10. Linear interpolation interp1
11. Linear and spline exercises
12.cubic spline vs hermite polynomial
A Numerical Calculus
1. Differentiation
2.polynomial differentiation (polynomial differentiation)
A polynomial is represented by a row vector, with the last term corresponding to the constant term and the other vectors corresponding to the coefficients of the powers.
a represents a polynomial, and x represents the value of these x.
gca is used to adjust the parameters of the image
>> a = [9. -5, 3, 7]; x = -2:0.01:5;
f = polyval(a,x);
plot(x,f,'LineWidth', 2);
xlabel('x'); ylabel('f(x)');
set(gca, 'FontSize',14)
>>
3. Polyder differential
The calculation and derivation are as shown in the figure below
If you want to know the derivative at a given value of x, use polyval
4. Derivation exercises
Hint conv() is multiplying two polynomials. The coefficient of the cube of x in the formula in the teacher's picture here is 5, and I also changed it to 5 during the practice.
Note: ① After getting the derivative, you need to use polyval combined with the x value to get the corresponding value (I was stuck here for a long time before), ② Add hold on and hold off statements so that the two lines do not overlap. ③ Derivatives need to be represented in the diagram, and are represented by \prime.
a = [5, -7, 5, 10]; b = [4, 12, -3]; x= [-2:0.01:1];
p = conv(a,b);
f = polyval(p,x);
y = polyval(polyder(p),x);
hold on;
plot(x,f, '--b');
plot(x,y,'-r');
hold off;
xlabel('x');
legend('f(x)','f\prime(x)');
5.polynomial integration (polynomial integration)
Use polyint to represent the integral, and write the constant k when integrating.
The score calculated by hand is as follows:
>> p = [5 0 -2 0 1];
polyint(p,3)
polyval(polyint(p,3),7)
6.numerical differentiation
That's the definition.
diff() returns the difference, that is, diff(x) will return, 1 3 -3 -1
7. Calculate the slope
8. Use diff to calculate numerical differentiation
>> x0 = pi/2; h = 0.1;
x = [x0 x0+h];
y = [sin(x0) sin(x0+h)];
m = diff(y)./diff(x)
m =
-0.0500
9. sin derivative error
When the fourth one is counted here, the error is too small, so it is 0.
>> x0 = pi/2;
h = 1;
i = 0.1;
for n = 1:10
h = h*i;
x = [x0 x0+h];
y = [sin(x0) sin(x0+h)];
m(n) = diff(y)./diff(x);
end
m
10. Calculating Interval Derivatives
This code has been changed for a long time, and there are a few points to note: ①The ppt code does not assign a value to h, but I have assigned a value to h. ② You need to change the symbol to Times New Roman, otherwise the coordinate axis will display squares. ③Change p to π.
g = colormap(lines); hold on;
for i = 1:4
h = power(10,-i);
x = 0:h:pi;
y = sin(x); m = diff(y)./diff(x);
plot(x(1:end-1), m, 'Color',g(i,:));
end
hold off;
set(gca, 'XLim', [0, pi/2]); set(gca, 'YLim', [0, 1.2]);
set(gca, 'FontSize', 18); set(gca, 'FontName', 'Times New Roman');
set(gca, 'XTick', 0:pi/4:pi/2);
set(gca, 'XTickLabel', {'0', 'π/4', 'π/2'});
h = legend('h=0.1','h=0.01','h=0.001', 'h=0.0001');
set(h, 'FontName', 'Times New Roman'); box on;
At this time, it seems that there are only 2 lines, let's zoom it in~
In fact, it is four lines.
11. Different h derivation exercises
g = colormap(lines);
hold on;
for i =1:3
h = power(10,-i);
x = 0:h:2*pi;
y = exp(-x).*(sin(x.^2./2));
m = diff(y)./diff(x);
plot(x(1:end-1), m, 'Color',g(i,:));
end
hold off;
legend('h=0.1','h=0.01','h=0.001');
set(gca, 'XLim', [0, 2*pi]); set(gca, 'YLim', [-0.25, 0.25]);
set(gca, 'FontSize', 18); set(gca, 'FontName', 'Times New Roman');
set(gca, 'YTickLabel', {'-0.2', '-0.1', '0','0.1', '0.2'});
set(gca, 'XTick', 0:pi/2:2*pi);
set(gca, 'XTickLabel', {'0', 'π/2', 'π','3π/2', '2π'});
12. Quadratic cubic differential
Note: ① Every time there is one more derivative, the x value will be reduced by -1. ② In the legend, you need to write two ''.
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);
xlabel('x', 'FontSize', 18);
ylabel('Y', 'FontSize', 18);
legend('f(x)=x^3','f''(x)','f''''(x)');
set(gca,'FontSize',18);
13. Numerical integration
Midpoint calculates the size of the rectangle, first calculates the height with f(x0+x1/2) and then multiplies it by the length.
14.midpoint
>> h = 0.05; x= 0:h:2;
midpoint = (x(1:end-1)+x(2:end))./2;
y = 4*midpoint.^3;
s = sum(h*y)
The correct answer is 16, which is very accurate.
15 trapezoid
>> h = 0.05; x = 0:h:2; y = 4*x.^3;
s = h*trapz(y)
or so
16. 1/3Simpson’s
Calculate the area of two squares at once, the formula is shown in the figure below
This method has to be calculated by yourself, the coefficient is the complex Simpson formula, and the coefficient is 12421 (the last 2 is omitted)
It can be seen that the Simpson formula is very accurate.
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))
17. Use matlab built-in functions to do numerical integration
If the x range is given, use @
function [y] = xy_plot(input,x)
y = input(x); plot(x,y,'r--');
xlabel('x'); ylabel('function(x)');
end
18.numerical integration
root of quadratic equation
1.symbolic root finding approach
y will also become symbolic
2.solve()
>> syms x
>> y = x*sin(x)-x;
>> solve(y,x)
ans =
0
pi/2
And ans is also of type sym
3. Find equations and practice with sin and cos
>> syms x;
y = power(cos(x),2)-power(sin(x),2);
solve(y,x)
ans =
pi/4
>>
>> syms x;
y = power(cos(x),2)+power(sin(x),2);
solve(y,x)
ans =
Empty sym: 0-by-1
4. Solving Multiple Equations
>> syms x y
>> eq1 = x -2*y - 5;
eq2 = x + y -6;
A = solve(eq1,eq2,x,y)
A =
包含以下字段的 struct:
x: 17/3
y: 1/3
5. Solving equations expressed in symbols
If b is regarded as an unknown number, a and x are known, and one more step is required.
6. Solving Equation Matrix Exercises
① Solve the equation
>> syms x y a b r
>> solve(power(x-a,2)+power(y-b,2)-r^2)
ans =
a + (b + r - y)^(1/2)*(r - b + y)^(1/2)
a - (b + r - y)^(1/2)*(r - b + y)^(1/2)
② Solve the matrix
A = [a b; c d]
A =
[a, b]
[c, d]
>> B = inv(A);
disp(B)
[ d/(a*d - b*c), -b/(a*d - b*c)]
[-c/(a*d - b*c), a/(a*d - b*c)]
7. Symbolic Differentiation
>> syms x
>> y = 4*x^5;
yprime = diff(y)
yprime =
20*x^4
8. Exercises in symbolic differentiation
f(x):
>> syms x;
f = exp(x^2)./(x^3-x+3)
fprime = diff(f)
f =
exp(x^2)/(x^3 - x + 3)
fprime =
(2*x*exp(x^2))/(x^3 - x + 3) - (exp(x^2)*(3*x^2 - 1))/(x^3 - x + 3)^2
g(x):
syms x y
g = (x^2+x*y-1)./(y^3+x+3);
diff(g)
ans =
(2*x + y)/(y^3 + x + 3) - (x^2 + y*x - 1)/(y^3 + x + 3)^2
9. Symbolic integrals
>> syms x;
y = x^2*exp(x);
z = int(y);
z = z-subs(z,x,0)
z =
exp(x)*(x^2 - 2*x + 2) - 2
10. Integral exercises
>> y = @(x) (x.^2-x+1)./(x+3); integral(y,0,10)
ans =
29.0624
11. Symbol vs Numeric
12.fsolve()
fsolve is to equalize a formula to a value and solve for the value of x
13. Exercises for Solving Linear Equations in Two Variables
Note: ①solve can only have one variable, xy can be regarded as x1x2, ②the initial value of fsolve is expressed in matrix form.
14.zero
When solving with zero, there must be a zero point
15.root
root can only solve numerical values
16. Root practice
17. solvers find roots
18. Bisection Method
Gradually narrow the interval, the multiplication of two points is less than 0
19.Newton-Raphson Method
First find the slope of the initial point, then find the intersection point with the x-axis, and iterate repeatedly. The function is continuous and its differential is known.
20. Bisection method vs Newton's method
The dichotomy must be in an interval, but Newton's method may not converge and requires differentiation.
21. Recursive function
function output = face(n)
if n==1
output = 1;
else
output = n* fact(n-1);
end
end
Trilinear Equations and Linear Systems
Since the content of the first 34 minutes is mainly used for understanding, start taking notes from 35 minutes.
1. Matrix left division
>> A = [1 2 1;2 6 1;1 1 4];
b = [2; 7; 3];
x = A\b
x =
-3.0000
2.0000
1.0000
>>
2.cramer's method
3. The linear part is skipped directly!
Four statistics
1. Summary method
2. mean median mode quartile average median mode quartile value
3. range and interquartile range
4. Variance Standard Deviation
5. Stock exercise
:4 is to extract the fourth column
6.figures are always more powerful (chart description)
7. Drawing practice
The difference from above is that the practice is given which points have the ball, not the number of each penalty.
x = 1:14;
freqy = zeros(1,14);
value = [1 3 5 5 5 5 7 9 9 9 10 13 14];
a = length(value);
for j = 1:14;
for i=1:a;
if value(i)==j;
freqy(j)=freqy(j)+1;
end
end
end
>> subplot(1,3,1); bar(x,freqy); xlim([0 15]);
subplot(1,3,2); area(x,freqy); xlim([0 15]);
subplot(1,3,3); stem(x,freqy); xlim([0 15]);
8.Boxplot
It mainly indicates the maximum value, the minimum value, the quartile value, and the median.
Boxplot returns the maximum and minimum values, and prctile returns the values of 25%, 50%, and 75%.
9. Stock boxplot exercise
Just boxplot directly, because it is output by column.
10.skewness skewed
X = randn([10 3])*3;
X(X(:,1)<0,1)=0; X(X(:,3)>0, 3) = 0;
boxplot(X,{'Right-skewed', 'Symmetric','Left-skewed'});
y = skewness(X)
y =
1.1755 -0.0420 -1.5425
11. KURTOSIS kurtosis
12. statistical hypothesis testing
How to judge whether you will get an A? You need to make statistics on the past students' grades, and then compare them with the current grades.
13.t-test example
Are the averages of the two stocks the same?
If h=0, the averages are equal, and h=1 is the opposite. p is the probability, and the smaller p is, the less likely h is to be established. Obviously not established
14. Common assumptions
Five Regression and Interpolation
1.simple linear regression simple linear regression
Human height and weight relationship
residual sum of squares
partial derivative solution
2. polyfit
Apply polyfit to fit, and then use the defined xy to draw the line. The order is 1, which requires a one-time equation, that is, ax+b
>> x = [-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
>> y = [-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
>> fit = polyfit(x,y,1);
>> xfit = [x(1):0.1:x(end)]; yfit = fit(1)*xfit + fit(2);
>> plot(x,y,'ro',xfit,yfit); set(gca, 'FontSize',14);
>> legend('data points','beat-fit');
3. Polyfit practice voltage and temperature relationship
>> x = [20 30 40 50 60];
y = [0.025 0.035 0.050 0.060 0.080];
fit = polyfit(x,y,1);
xfit = [x(1):1:x(end)]; yfit = fit(1)*xfit + fit(2);
plot(x,y,'ro',xfit,yfit); set(gca, 'FontSize',14)
4. Is there a linear relationship between x and y?
Symmetric matrix, just look at 0.9202.
5. High-level profit
>> x = [-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y = [-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
figure('Position', [50 50 1500 400]);
for i=1:3
subplot(1,3,i); p = polyfit(x,y,i);
xfit = x(1):0.1:x(end); yfit = polyval(p,xfit);
plot(x,y,'ro',xfit,yfit); set(gca,'FontSize',14);
ylim([-17,11]); legend('Data points','Fitted curve');
end
6. 4 5 6 exercises
It's not that the higher the order, the better, it will be overfitting.
x = [-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y = [-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
figure('Position', [50 50 1500 400]);
for i=4:6
subplot(1,3,i-3); p = polyfit(x,y,i);
xfit = x(1):0.1:x(end); yfit = polyval(p,xfit);
plot(x,y,'ro',xfit,yfit); set(gca,'FontSize',14);
ylim([-17,11]); legend('Data points','Fitted curve','Location','southeast');
end
7. Multiple variable forecasting
8. Is the formula linear?
The cftool part is not used because there is no input data.
9. Interpolation vs Regression
Regression is to find a trend line, and interpolation is to find many lines and connect them one by one.
10. Linear interpolation interp1
m_i = ~isnan(x_m);
y_i = interp1(x_m(m_i), y_m(m_i), x);
hold on;
plot(x,y_i,'-b', 'LineWidth', 2);
hold off;
>> x = linspace(0, 2*pi, 40); x_m = x;
x_m([11:13, 28:30]) = NaN; y_m = sin(x_m);
plot(x_m,y_m,'ro','MarkerFaceColor', 'r');
xlim([0, 2*pi]); ylim([-1.2, 1.2]); box on;
set(gca, 'FontSize', 16);
set(gca, 'XTick', 0:pi/2:2*pi);
set(gca, 'XTickLabel', {'0','π/2', 'π', '3π/2','2π'});
m_i = ~isnan(x_m);
y_i = interp1(x_m(m_i), y_m(m_i), x);
hold on;
plot(x,y_i,'-b', 'LineWidth', 2);
hold off;
spline can appear smoother
x = linspace(0, 2*pi, 40); x_m = x;
x_m([11:13, 28:30]) = NaN; y_m = sin(x_m);
plot(x_m,y_m,'ro','MarkerFaceColor', 'r');
xlim([0, 2*pi]); ylim([-1.2, 1.2]); box on;
set(gca, 'FontSize', 16);
set(gca, 'XTick', 0:pi/2:2*pi);
set(gca, 'XTickLabel', {'0','π/2', 'π', '3π/2','2π'});
m_i = ~isnan(x_m);
y_i = interp1(x_m(m_i), y_m(m_i), x);
hold on;
plot(x,y_i,'-b', 'LineWidth', 2);
hold off;
m_i = ~isnan(x_m);
y_i = spline(x_m(m_i), y_m(m_i), x);
hold on;
plot(x,y_i,'-g', 'LineWidth', 2);
hold off;
h = legend('Original', 'Linear', 'Spline');
set(h,'FontName', 'Times New Roman');
11. Linear and spline exercises
x = [0 0.25 0.75 1.25 1.5 1.75 1.875 2 2.125 2.25];
y = [1.2 1.18 1.1 1 0.92 0.8 0.7 0.55 0.35 0];
plot(x,y,'ro','MarkerFaceColor', 'b');
xlim([0, 2.5]); ylim([0, 1.4]); box on;
set(gca, 'FontSize', 16);
set(gca, 'XTick', 0:0.5:2.5);
set(gca, 'YTick', 0:0.2:1.4);
y1 = interp1(x, y, x);
hold on;
plot(x,y1,'-r', 'LineWidth', 2);
hold off;
y2 = spline(x, y, x);
hold on;
plot(x,y2,'-g', 'LineWidth', 2);
hold off;
h = legend('Data', 'Linear', 'Spline');
set(h,'FontName', 'Times New Roman');
12.cubic spline vs hermite polynomial
x = -3:3; y = [-1 -1 -1 0 1 1 1]; t = -3:.01:3;
s = spline(x,y,t); p = pchip(x,y,t);
hold on; plot(t,s,':g', 'LineWidth', 2);
plot(t,p,'--b', 'LineWidth',2);
plot(x,y,'ro','MarkerFaceColor','r');
hold off; box on; set(gca,'FontSize', 16);
h = legend('Original', 'Spline','Hermite');
>> x = -3:3; y = [-1 -1 -1 0 1 1 1]; t = -3:.01:3;
s = spline(x,y,t); p = pchip(x,y,t);
hold on; plot(t,s,':g', 'LineWidth', 2);
plot(t,p,'--b', 'LineWidth',2);
plot(x,y,'ro','MarkerFaceColor','r');
hold off; box on; set(gca,'FontSize', 16);
h = legend('Original', 'Spline','Hermite','Location','Northwest');
13.interp2
Two x, one y values. Just typed according to the code.
xx = -2:.4:2; yy = -2:.5:3;
[X,Y] = meshgrid(xx,yy);
Z = X.*exp(-X.^2-Y.^2);
surf(X,Y,Z); hold on;
plot3(X,Y,Z+0.01,'ok','MarkerFaceColor','r')
xx_i = -2:.1:2; yy_i = -2:.1:3;
[X_i,Y_i] = meshgrid(xx_i,yy_i);
Z_i = interp2(xx,yy,Z,X_i,Y_i);
surf(X_i,Y_i,Z_i); hold on;
plot3(X,Y,Z+0.01,'ok','MarkerFaceColor','r')