我写了一个用矩形法求解积分X^2+2的程序
program fourrr;
var a, b : real; { borders }
h : real; { increment argument }
s : real; { approximate value of the integral }
n : integer; { number of intervals }
x : real; { argument }
y : real; { function value at the beginning of interval }
i : integer;
begin
write('lower border a: '); read(a);
write('upper border b: '); read(b);
write('increment argument h: '); read(h);
n := (b - a) / (h + 1);
x := a;
s := 0;
i := 1;
while i <= n do
begin
y := x * x + 2; // function value at the beginning of interval
s := s + (y * h);
x := x + h;
end;
writeln('Integral value: ', s);
end.
但是我运行时无法解决数据类型转换的问题。
所以请帮助我,这非常重要。谢谢
(从一开始,对不起我的英语不好)
一开始,需要输入输出:
program fourrr(input,output);
我看到你已经完成了
n := (b - a) / (h + 1);
那不是 /
,你应该使用 div
:
n := (b - a) div (h + 1);
我不知道你为什么使用循环,我告诉你我不知道如何求解该积分,但是,如果像你那样,你就不需要它。 你可以在循环中改变什么? 使用
IF
。
If i<=n THEN
Begin
y := x * x + 2; (* function value at the beginning of interval *)
s := s + (y * h);
x := x + h;
end (* without a semicolon *)
Else
writeln('ERROR'); (* The ELSE only read for him the writeln('error'),
if you want to use more things,
use the ELSE begin h:= ... ; alpha:=...
If ...(<-for example) end; *)