0

整个问题

这更多是为了澄清目的,我想仔细检查我的工作是否正确。我很确定案例 1(梯形)和 2(辛普森)是正确的,但对于案例 3(二等分),我不知道如何让它在卡住时正常运行。我已经完成了选择开关,所以我没有包含该代码。

data = input('1 为梯形选择 2 为 Simpson,选择 3 为 ROOT '); 切换数据案例1

    a = input(' enter lower limit a: ');
    b = input(' enter upper limit b: ');
    n = input(' # of subintervals: ');
    h = (b-a)/n ;
    sum = 0 ;
    for k = 1:1:n-1
        x(k) = a+k*h;
        y(k) = f(x(k));
        sum = sum + y(k);
    end
    answer = h/2*(f(a)+f(b)+2*sum);
    fprintf('\n The value of integration is %f' ,answer);
    
case 2 
    a = input(' enter lower limit a: ');
    b = input(' enter upper limit b: ');
    n = input(' # of subintervals: ');
    h=(b-a)/n;
    x=linspace(a,b,n);
    for i = 1:n-1
        I(i)= (h/3)*(f(x(i))+(4*f((x(i)+x(i+1))/2))+f(x(i+1)));
    end
    answer = sum(I);
    fprintf('\n The value of integration is %f' ,answer);
    
case 3
    a = input(' Enter the left boundary ');
    b = input(' Enter the right boundary ');
    e = input(' Enter epsilon ');
    h = (b - a)/2;
    k = h;
    while h > e
        h = (k - a)/2;
    end
   answer = sum(h);
   fprintf('\n The value of integration is %f' ,answer);
   

结尾

4

0 回答 0