zoukankan      html  css  js  c++  java
  • matlab实现复合梯形法则

    复合梯形法则:

    function int_f = CompoundEchelon( f, a, b, m )
    % input :   f : function handler
    %           a : the lower limit of integral
    %           b : the upper limit of integral
    %           m : cut integral area into m peace
    % output :  int_f : the answer of the integral
        h = (b - a) / m;
        int_f = 0;
        if m >= 2
            for i = 1 : m-1
               int_f = int_f + 2 * f(a + h * i); 
            end
        end
        int_f = int_f + f(a) + f(b);
        int_f = int_f * h / 2;
    end
    

    例子:

    clear all
    format long
    clc
    %% (a)
    fprintf(' (a) 
    ')
    f = @(x) x./((x.^2+9).^0.5);
    int1_16 = CompoundEchelon(f, 0, 4, 16);
    int1_32 = CompoundEchelon(f, 0, 4, 32);
    correct_int1 = quadgk(f, 0, 4);
    error1_16 = abs(correct_int1 - int1_16);
    error1_32 = abs(correct_int1 - int1_32);
    fprintf('int1_16 = %g
    ', int1_16);
    fprintf('int1_32 = %g
    ', int1_32);
    fprintf('correct_int1 = %g
    ', correct_int1);
    fprintf('error1_16 = %g
    ', error1_16);
    fprintf('error1_32 = %g
    ', error1_32);
    %% (b)
    fprintf(' (b) 
    ')
    f = @(x) (x.^3)./(x.^2+1);
    int2_16 = CompoundEchelon(f, 0, 1, 16);
    int2_32 = CompoundEchelon(f, 0, 1, 32);
    correct_int2 = quadgk(f, 0, 1);
    error2_16 = abs(correct_int2 - int2_16);
    error2_32 = abs(correct_int2 - int2_32);
    fprintf('int2_16 = %g
    ', int2_16);
    fprintf('int2_32 = %g
    ', int2_32);
    fprintf('correct_int2 = %g
    ', correct_int2);
    fprintf('error2_16 = %g
    ', error2_16);
    fprintf('error2_32 = %g
    ', error2_32);
    %% (c)
    fprintf(' (c) 
    ')
    f = @(x) x.*exp(x);
    int3_16 = CompoundEchelon(f, 0, 1, 16);
    int3_32 = CompoundEchelon(f, 0, 1, 32);
    correct_int3 = quadgk(f, 0, 1);
    error3_16 = abs(correct_int3 - int3_16);
    error3_32 = abs(correct_int3 - int3_32);
    fprintf('int3_16 = %g
    ', int3_16);
    fprintf('int3_32 = %g
    ', int3_32);
    fprintf('correct_int3 = %g
    ', correct_int3);
    fprintf('error3_16 = %g
    ', error3_16);
    fprintf('error3_32 = %g
    ', error3_32);
    %% (d)
    fprintf(' (d) 
    ')
    f = @(x) (x.^2).*(log(x));
    int4_16 = CompoundEchelon(f, 1, 3, 16);
    int4_32 = CompoundEchelon(f, 1, 3, 32);
    correct_int4 = quadgk(f, 1, 3);
    error4_16 = abs(correct_int4 - int4_16);
    error4_32 = abs(correct_int4 - int4_32);
    fprintf('int4_16 = %g
    ', int4_16);
    fprintf('int4_32 = %g
    ', int4_32);
    fprintf('correct_int4 = %g
    ', correct_int4);
    fprintf('error4_16 = %g
    ', error4_16);
    fprintf('error4_32 = %g
    ', error4_32);
    %% (e)
    fprintf(' (e) 
    ')
    f = @(x) (x.^2).*(sin(x));
    int5_16 = CompoundEchelon(f, 0, pi, 16);
    int5_32 = CompoundEchelon(f, 0, pi, 32);
    correct_int5 = quadgk(f, 0, pi);
    error5_16 = abs(correct_int5 - int5_16);
    error5_32 = abs(correct_int5 - int5_32);
    fprintf('int5_16 = %g
    ', int5_16);
    fprintf('int5_32 = %g
    ', int5_32);
    fprintf('correct_int5 = %g
    ', correct_int5);
    fprintf('error5_16 = %g
    ', error5_16);
    fprintf('error5_32 = %g
    ', error5_32);
    %% (f)
    fprintf(' (f) 
    ')
    f = @(x) (x.^3)./((x.^4-1).^0.5);
    int6_16 = CompoundEchelon(f, 2, 3, 16);
    int6_32 = CompoundEchelon(f, 2, 3, 32);
    correct_int6 = quadgk(f, 2, 3);
    error6_16 = abs(correct_int6 - int6_16);
    error6_32 = abs(correct_int6 - int6_32);
    fprintf('int6_16 = %g
    ', int6_16);
    fprintf('int6_32 = %g
    ', int6_32);
    fprintf('correct_int6 = %g
    ', correct_int6);
    fprintf('error6_16 = %g
    ', error6_16);
    fprintf('error6_32 = %g
    ', error6_32);
    %% (g)
    fprintf(' (g) 
    ')
    f = @(x) 1./((x.^2+4).^0.5);
    int7_16 = CompoundEchelon(f, 0, 2*3^0.5, 16);
    int7_32 = CompoundEchelon(f, 0, 2*3^0.5, 32);
    correct_int7 = quadgk(f, 0, 2*3^0.5);
    error7_16 = abs(correct_int7 - int7_16);
    error7_32 = abs(correct_int7 - int7_32);
    fprintf('int7_16 = %g
    ', int7_16);
    fprintf('int7_32 = %g
    ', int7_32);
    fprintf('correct_int7 = %g
    ', correct_int7);
    fprintf('error7_16 = %g
    ', error7_16);
    fprintf('error7_32 = %g
    ', error7_32);
    %% (h)
    fprintf(' (h) 
    ')
    f = @(x) x./((x.^4+1).^0.5);
    int8_16 = CompoundEchelon(f, 0, 1, 16);
    int8_32 = CompoundEchelon(f, 0, 1, 32);
    correct_int8 = quadgk(f, 0, 1);
    error8_16 = abs(correct_int8 - int8_16);
    error8_32 = abs(correct_int8 - int8_32);
    fprintf('int8_16 = %g
    ', int8_16);
    fprintf('int8_32 = %g
    ', int8_32);
    fprintf('correct_int8 = %g
    ', correct_int8);
    fprintf('error8_16 = %g
    ', error8_16);
    fprintf('error8_32 = %g
    ', error8_32);
    
  • 相关阅读:
    Java基础——clone()方法浅析
    Unity shader error: “Too many texture interpolators would be used for ForwardBase pass”
    ar 解压一个.a文件报错: xxx.a is a fat file (use libtool(1) or lipo(1) and ar(1) on it)
    How to set up "lldb_codesign" certificate!
    Unity-iPhone has Conflicting Provisioning Settings
    ETC1/DXT1 compressed textures are not supported when publishing to iPhone
    Xcode 提交APP时遇到 “has one iOS Distribution certificate but its private key is not installed”
    XCode iOS之应用程序标题本地化
    苹果电脑(Mac mini或Macbook或iMac)恢复出厂设置
    Unity 4.7 导出工程在XCode10.1上编译报错
  • 原文地址:https://www.cnblogs.com/wsine/p/4634587.html
Copyright © 2011-2022 走看看