Scilab 中的梯形积分 - 多边形颜色填充停止

Trapezoid Integration in Scilab - Polygon Color Fill Stops

我一直在 Scilab 中编写一个程序,该程序通过梯形法则对函数进行数值积分(不使用内置函数)。我对函数的积分或绘图没有问题,但我想将真实函数叠加在梯形图上,并涂上颜色。

出于某种原因,当我将边界 a = 0 设置为 b = 3 时,没问题,我得到了我想要的。但是,当我将边界设置为 3 以上时,梯形仍会绘制(按线),但不会着色。在下面的代码中,颜色停在 3。例如,如果我绘制 0 到 6,颜色中途停止。 3到6,一点颜色都没有

以下是代码的相关部分:

deff('[y] = f(x)','y = e^(x^2)');            // Definition of function
a = 0;                                       // Lower bound
b = 4;                                       // Upper bound
n = 20;                                      // Number of intervals
h = ((b - a)/n);                             // Interval spacing
x = a:h:b;                                   // Array of positions for division

for i = 1:n+1
    y(i) = f(x(i));
end

for i = 1:n                                         // Plot colored trapezoids
    x_start = a+(h*(i-1));
    x_end = a+(h*(i));
    y_start = y(i);
    y_end = y(i+1);
    xpts = [x_start, x_end, x_end, x_start];
    ypts = [y_start, y_end, 0, 0];
    xfpoly(xpts,ypts,3);
end

This is the plot output for a = 0, b = 3

您使用的是什么版本的 Scilab? 我用 Scilab 5.4.1(64 位)尝试了你的代码,我得到了无色梯形,但在 5.5.2(64 位)中,所有形状都是漂亮的绿色。 所以也许这些版本之间存在一些错误修正。 我还将您的函数定义从 'y = e^(x^2)' 更改为 'y = %e^(x^2)',因为欧拉数是一个预定义变量(至少在 5.5.2 中是这样)。

clc;
clear;

deff('[y] = f(x)','y = %e^(x^2)');            // Definition of function
a = 0;                                       // Lower bound
b = 6;                                       // Upper bound
n = 100;                                      // Number of intervals
h = ((b - a)/n);                             // Interval spacing
x = a:h:b;                                   // Array of positions for division


for i = 1:n+1
    y(i) = f(x(i));
end

scf(0);
clf(0);
plot2d(x,y);

for i = 1:n                                         // Plot colored trapezoids
    x_start = a+(h*(i-1));
    x_end = a+(h*(i));
    y_start = y(i);
    y_end = y(i+1);
    xpts = [x_start, x_end, x_end, x_start];
    ypts = [y_start, y_end, 0, 0];
    xfpoly(xpts,ypts,3);
end