【问题标题】:Trapezoid Integration in Scilab - Polygon Color Fill StopsScilab 中的梯形积分 - 多边形颜色填充停止
【发布时间】:2017-06-20 11:29:31
【问题描述】:

我一直在研究 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

【问题讨论】:

  • 对于初学者来说,在 xfpoly 中关闭 3 不是合法值
  • @awiebe 不,这是合法的:必须是整数,用于填充多边形的颜色(请参阅 Scilab 帮助了解更多详细信息)。

标签: plot polygon scilab numerical-integration


【解决方案1】:

您使用的是哪个版本的 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

【讨论】:

  • 抱歉回复晚了(期末考试)。我终于开始下载最新版本,正如你所说,它看起来不错。我之前用的是5.4.1,现在用5.5.2没有任何问题!我真的很感谢你的帮助。谢谢!
猜你喜欢
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-20
相关资源
最近更新 更多