MATLAB 数值微积分

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaohaibo_/article/details/82154279

学习笔记:郭彥甫 (Yan-Fu Kuo), 台大生機系 , MATLAB教學 - 10數值微積分

Representing Polynomials in MATLAB

  • Polynomials were represented as row vectors
  • for example,consider the equation f ( x ) = x 3 2 x 5
  • To enter this polynomial into MATLAB, usep = [1 0 -2 -5]

Values of Polynomials:polyval()

  • Plot the polynomial f ( x ) = 9 x 3 5 x 2 + 3 x + 7
  • for 2 x 5 .
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)

Polynomial Differentiation(多项式微分):polyder()

  • Given f ( x ) = 5 x 4 2 x 2 + 1
    1. What is the derivative(导数) of the function f ( x ) ?
    2. What is the derivative of the function value of f ( 7 ) ?
>> p = [5 0 -2 0 1];
>> polyder(p) 
ans =     20     0    -4     0
>> polyval(polyder(p),7)
ans = 6832

Exercise

  • Plot the polynomial f ( x ) = ( 20 x 3 7 x 2 + 5 x + 10 ) ( 4 x 2 + 12 x 3 ) and its derivative for 2 x 1 .
  • Hint:conv()

    p1 = [20 -7 5 10];
    p2 = [4 12 -3];
    a = conv(p1,p2);
    x=-2:0.01:1;
    hold on
    plot(x,f,'b--','linewidth',2);
    plot(x,polyval(polyder(a),x),'r','linewidth',2);
    hold off
    % plot(x,f,'b--',x,polyval(polyder(a),x),'r','linewidth',2);
    xlabel('x')
    ylabel('y')
    legend('f(x)',"f'(x)");

Polynomial Intergration(积分):polyint()

  • for a polynomial

    f ( x ) = a n x n + a n 1 x n 1 + . . . + a 1 x + a 0
    the integration is
    f ( x ) = 1 n + 1 a n x n + 1 + 1 n a n 1 x n + . . . + a 0 x + k

  • Given f ( x ) = 5 x 4 2 x 2 + 1

    1. What is the integral of the function f ( x ) with a constant of 3?

      >> p = [5 0 -2 0 1];
      >> polyint(p,3)
      ans =    1.0000         0   -0.6667         0    1.0000    3.0000
    2. What is the derivative of the function value of f ( 7 ) ?

      >> polyval(polyint(p,3),7))
      ans = 1.6588e+04

Numerical Differentiation (数值微分):diff()

  • The simplest method: finite difference approximation(有限差分近似)

  • Calculating a secant line(割线) in the vicinty(附近) of x 0

    f ( x 0 ) = lim h 0 f ( x 0 + h ) f ( x 0 ) h
    where h represents a small change in x .

  • diff() : calculates the differences between adjacent(附近的) elements of a vector

    >> x = [1 2 5 2 1];
    >> diff(x)
    ans =  1     3    -3    -1
  • Exercise : obtain the slope(斜率) of a line between 2 points (1,5) and (2,7)

    x = [1 2];y=[5 7]
    slope = diff(y)./diff(x)
  • Given f ( x ) = s i n ( x ) , find f ( x 0 ) at x 0 = π / 2 using h = 0.1

    f ( x 0 ) = lim h 0 f ( x 0 + h ) f ( x 0 ) h

    >> x0 = pi/2
    x0 = 1.5708
    >> h=0.1
    h = 0.1000
    >> x = [x0 x0+h]
    x = 1.5708    1.6708
    >> y = [sin(x0) sin(x0+h)]
    y = 1.0000    0.9950
    >> m = diff(y)./diff(x)
    m = -0.0500

Exercise

  • Given f ( x ) = s i n ( x ) , write a script to find the error of f ( x ) at x 0 = π / 2 using various h

  • We have already known f ( π / 2 ) = 0

    x0 = pi/2;
    for h=[0.1 0.01 0.001 0.0001 0.00001 0.000001]
      x=[x0,x0+h];
      y=[sin(x0),sin(x0+h)];
      slop=diff(y)./diff(x);
      W=['h=',num2str(h),' error=',num2str(slop)];
      disp(W)
    end

How to Find the f over An Interval(区间) [ 0 , 2 π ] ?

  • In the previous example, x 0 = π / 2 .
  • Strategy:

    1. Create an array in the interval [ 0 , 2 π ]
    2. The step is the h .
    3. Calculate the f at these points
  • For example:

    h = 0.5;
    x = 0:h:2*pi;
  • Find s i n ( x ) over x = [ 0 , 2 π ] .

    h = 0.5;
    x = 0:h:2*pi;
    y = sin(x);
    m = diff(y)./diff(x);  % m = f'(x)

Various Step Size

  • The derivatives of f ( x ) = s i n ( x ) caculated using various h values

    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,:));
    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'});  % xtick是刻度(小竖线);xticklabel 刻度值(竖线下面的数值)
    h = legend('h=0.1','h=0.01','h=0.001','h=0.0001');
    set(h,'FontName','Times New Roman'); box on; 

Exercise

  • Given f ( x ) = e x s i n ( x 2 / 2 ) ,plot the approximate derivatives f of h = 0.1 , 0.01 , and 0.001

    g = colormap(lines);hold on;
    for i=1:3
      x = 0:power(10,-i):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;
    set(gca,'Xlim',[0,pi*2]); set(gca,'YLim',[-0.3,0.3]);
    set(gca,'FontSize',18); set(gca,'FontName','symbol');
    set(gca,'XTick',0:pi/2:pi*2);
    set(gca,'XTickLabel',{'0','pi/2','pi','3/2pi','2pi'});
    set(gca,'YTick',-0.2:0.1:0.2);
    set(gca,'YTickLabel',{'-0.2','-0.1','0','0.1','0.2'});
    h = legend('h=0.1','h=0.01','h=0.001');
    set(h,'FontName','Times New Roman'); box on;

Second and Third Derivatives(二阶、三阶导)

  • The second derivative f and third derivative f can be obtained using similar approaches

  • Given f ( x ) = x 3 , plot f and f for 2 x 2 .

    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);

Numerical Integration(数值积分)

  • Calculating the numerical value of a definite integral

    s = a b f ( x ) d ( x ) i = 0 n f ( x i ) a b L i ( x ) d x

  • Quadrature method — approximating the integral by using a finite set of points(正交方法 - 使用有限的点集近似积分)

Numerical Quadrature Rules(数值求积分准则)

  • Basic quadrature rules:

    1. Midpoint rule(zeroth-order approximation)(中点准则(零阶近似),使用矩形预测面积大小)

    2. Trapezoid rule(first-order approximation)(梯形准则(一阶近似),使用梯形预测面积大小)

Midpoint Rule Using sum()
  • Example:

    A = 0 2 4 x 3 d x = x 4 | 0 2 = ( 2 ) 4 ( 0 ) 4 = 16

    >> h = 0.05;
    >> x = 0:h:2;  % x is a vector
    >> midpoint = (x(1:end-1)+x(2:end))./2;  % midpoint is a vector
    >> y = 4*midpoint.^3;  % y is a vector
    >> s = sum(h*y)
    s =15.9950
Trapezoid Rule Using trapz()
  • Example:

    A = 0 2 4 x 3 d x = x 4 | 0 2 = ( 2 ) 4 ( 0 ) 4 = 16

    >> h = 0.05;
    >> x = 0:h:2;
    >> y = 4*x.^3;
    >> s = h*trapz(y)
    16.0100
    
    % the forth step above equals
    trapezoid = (y(1:end-1)+y(2:end))/2;
    s = h*sum(trapezoid)
Second-order Rule:1/3 Simpson’s

  • Example:

    A = 0 2 4 x 3 d x = x 4 | 0 2 = ( 2 ) 4 ( 0 ) 4 = 16

>> 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
Comparison
  • Midpoint rule — zeroth-order
  • Trapezoid rule — first-order
  • Simpson’s rule — second-order
  • higher order of approximation results to a higher accuracy (逼近的次数越高越精准)

Review of Function Handles(@)

  • A handle is a pointer to a function
  • Can be used to pass function to other functions

  • For example, the input of the following function is another function:

    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
  • When we need to use the function xy_plot ,use xy_plot(@sin,0:0.01:2*pi);

Numerical Intergration:integral()

  • Numerical integration on a function from using global adaptive quadrature and default error tolerances.

  • Example:

    0 2 1 x 3 2 x 5

    >> y = @(x) 1./(x.^3-2*x-5);
    >> integral(y,0.2)
    ans = -0.4605

    0 2 s i n ( x )

    y = @(x) sin(x);
    integral(y,0,2)

Double and Triple Integrals(双重和三重积分)

  • Example:

    f ( x , y ) = 0 π π 2 π ( y s i n ( x ) + x c o s ( y ) ) d x d y

    f = @(x,y) y.*sin(x)+x.*cos(y);
    integral2(f,pi,2*pi,0,pi)
  • Example:

    f ( x , y ) = 1 1 0 1 0 π ( y s i n ( x ) + z c o s ( y ) ) d x d y d z

    f = @(x,y,z) y.*sin(x)+z.*cos(y)
    integral3(f,0,pi,0,1,-1,1)

猜你喜欢

转载自blog.csdn.net/zhaohaibo_/article/details/82154279