梯形求积法: public class d3r1F { double trapzd_s; void trapzd(double a, double b, double s, int n) { double del,x,sum; int j, it, tnm; if (n == 1) { s = 0.5 * (b - a) * (func(a) + func(b)); it = 1; } else { it = (int)Math.pow(2 , n - 2); tnm = it; del = (b - a) / tnm; x = a + 0.5 * del; sum = 0.0; for (j = 1; j <= it; j++) { sum = sum + func(x); x = x + del; } s = 0.5 * (s + (b - a) * sum / tnm); } trapzd_s = s; } double func(double x) { return (x * x) * (x * x - 2.0) * Math.sin(x); } double fint(double x) { double aaa = 4.0 * x * (x * x - 7.0) * Math.sin(x); return aaa - (x * x * x * x - 14.0 * x * x + 28.0) * Math.cos(x); } } // /**************** * * */ //例子 import java.text.*; public class d3r1 { public static void main (String[] args) { //program d3r1 //driver for routine trapzd int i; int nmax = 14; double pio2 = 1.5707963; double s,b,a = 0.0; s = 0; d3r1F g = new d3r1F(); DecimalFormat form = new DecimalFormat("0.000000"); b = pio2; System.out.println(); System.out.println("integral of func with 2^(n-1) points"); System.out.print("actual value of integral is: "); System.out.println(form.format((g.fint(b) - g.fint(a)))); System.out.println(); System.out.println("n approx.integral"); for (i = 1; i <= nmax; i++) { g.trapzd(a, b, s, i); s = g.trapzd_s; System.out.print(i + " "); System.out.println(form.format(s)); } } }
to tiger999(不吃肉的老虎)照你这么说,有了公式就有了思路,那还用你编什么?问题是哪种算法更接近于积分结果
>>从图片中读积分公式然后求解?难呵呵,好恐怖!!!
数学积分都要用java实现,关注……
好像没什么说的啊,求近似值啊!标准的积分啊!$a $b f(x)dx
public class d3r1F { double trapzd_s; void trapzd(double a, double b, double s, int n) { double del,x,sum; int j, it, tnm; if (n == 1) { s = 0.5 * (b - a) * (func(a) + func(b)); it = 1; } else { it = (int)Math.pow(2 , n - 2); tnm = it; del = (b - a) / tnm; x = a + 0.5 * del; sum = 0.0; for (j = 1; j <= it; j++) { sum = sum + func(x); x = x + del; } s = 0.5 * (s + (b - a) * sum / tnm); } trapzd_s = s; } double func(double x) { return (x * x) * (x * x - 2.0) * Math.sin(x); } double fint(double x) { double aaa = 4.0 * x * (x * x - 7.0) * Math.sin(x); return aaa - (x * x * x * x - 14.0 * x * x + 28.0) * Math.cos(x); } } // /**************** * * */ //例子 import java.text.*; public class d3r1 { public static void main (String[] args) { //program d3r1 //driver for routine trapzd int i; int nmax = 14; double pio2 = 1.5707963; double s,b,a = 0.0; s = 0; d3r1F g = new d3r1F(); DecimalFormat form = new DecimalFormat("0.000000"); b = pio2; System.out.println(); System.out.println("integral of func with 2^(n-1) points"); System.out.print("actual value of integral is: "); System.out.println(form.format((g.fint(b) - g.fint(a)))); System.out.println(); System.out.println("n approx.integral"); for (i = 1; i <= nmax; i++) { g.trapzd(a, b, s, i); s = g.trapzd_s; System.out.print(i + " "); System.out.println(form.format(s)); } } } 不能运行呀,请指点!谢谢
public class d3r1F
{
double trapzd_s;
void trapzd(double a, double b, double s, int n)
{
double del,x,sum;
int j, it, tnm;
if (n == 1)
{
s = 0.5 * (b - a) * (func(a) + func(b));
it = 1;
}
else
{
it = (int)Math.pow(2 , n - 2);
tnm = it;
del = (b - a) / tnm;
x = a + 0.5 * del;
sum = 0.0;
for (j = 1; j <= it; j++)
{
sum = sum + func(x);
x = x + del;
}
s = 0.5 * (s + (b - a) * sum / tnm);
}
trapzd_s = s;
} double func(double x)
{
return (x * x) * (x * x - 2.0) * Math.sin(x);
} double fint(double x)
{
double aaa = 4.0 * x * (x * x - 7.0) * Math.sin(x);
return aaa - (x * x * x * x - 14.0 * x * x + 28.0) * Math.cos(x);
}
}
//
/****************
*
*
*/
//例子
import java.text.*;
public class d3r1
{
public static void main (String[] args)
{
//program d3r1
//driver for routine trapzd
int i;
int nmax = 14;
double pio2 = 1.5707963;
double s,b,a = 0.0;
s = 0;
d3r1F g = new d3r1F();
DecimalFormat form = new DecimalFormat("0.000000");
b = pio2;
System.out.println();
System.out.println("integral of func with 2^(n-1) points");
System.out.print("actual value of integral is: ");
System.out.println(form.format((g.fint(b) - g.fint(a))));
System.out.println();
System.out.println("n approx.integral");
for (i = 1; i <= nmax; i++)
{
g.trapzd(a, b, s, i);
s = g.trapzd_s;
System.out.print(i + " ");
System.out.println(form.format(s));
}
}
}
{
double trapzd_s;
void trapzd(double a, double b, double s, int n)
{
double del,x,sum;
int j, it, tnm;
if (n == 1)
{
s = 0.5 * (b - a) * (func(a) + func(b));
it = 1;
}
else
{
it = (int)Math.pow(2 , n - 2);
tnm = it;
del = (b - a) / tnm;
x = a + 0.5 * del;
sum = 0.0;
for (j = 1; j <= it; j++)
{
sum = sum + func(x);
x = x + del;
}
s = 0.5 * (s + (b - a) * sum / tnm);
}
trapzd_s = s;
} double func(double x)
{
return (x * x) * (x * x - 2.0) * Math.sin(x);
} double fint(double x)
{
double aaa = 4.0 * x * (x * x - 7.0) * Math.sin(x);
return aaa - (x * x * x * x - 14.0 * x * x + 28.0) * Math.cos(x);
}
}
//
/****************
*
*
*/
//例子
import java.text.*;
public class d3r1
{
public static void main (String[] args)
{
//program d3r1
//driver for routine trapzd
int i;
int nmax = 14;
double pio2 = 1.5707963;
double s,b,a = 0.0;
s = 0;
d3r1F g = new d3r1F();
DecimalFormat form = new DecimalFormat("0.000000");
b = pio2;
System.out.println();
System.out.println("integral of func with 2^(n-1) points");
System.out.print("actual value of integral is: ");
System.out.println(form.format((g.fint(b) - g.fint(a))));
System.out.println();
System.out.println("n approx.integral");
for (i = 1; i <= nmax; i++)
{
g.trapzd(a, b, s, i);
s = g.trapzd_s;
System.out.print(i + " ");
System.out.println(form.format(s));
}
}
}
不能运行呀,请指点!谢谢
UP,楼上的,数学系计算科学的吧to 楼主:看你要怎么算了,要算到什么精度??具体算法请参阅:计算方法