帮忙把下面c语言程序改为c++语言,程序为用矩形法求fx=1/(1+x^2)在0到1的定积分近似值,求修改成能运行啊format longn=100;a=0;b=1;inum1=0;inum2=0;inum3=0;syms x fxfx=1/(1+x^2);for i=1:nxj=a+(i-1)*(b-a)/n; %左点xi=a+i*(b
来源:学生作业帮助网 编辑:作业帮 时间:2024/12/01 13:23:52
帮忙把下面c语言程序改为c++语言,程序为用矩形法求fx=1/(1+x^2)在0到1的定积分近似值,求修改成能运行啊format longn=100;a=0;b=1;inum1=0;inum2=0;inum3=0;syms x fxfx=1/(1+x^2);for i=1:nxj=a+(i-1)*(b-a)/n; %左点xi=a+i*(b
帮忙把下面c语言程序改为c++语言,程序为用矩形法求fx=1/(1+x^2)在0到1的定积分近似值,求修改成能运行啊
format long
n=100;a=0;b=1;
inum1=0;inum2=0;inum3=0;
syms x fx
fx=1/(1+x^2);
for i=1:n
xj=a+(i-1)*(b-a)/n; %左点
xi=a+i*(b-a)/n; %右点
fxj=subs(fx,'x',xj); %左点值
fxi=subs(fx,'x',xi); %右点值
fxij=subs(fx,'x',(xi+xj)/2); %中点值
inum1=inum1+fxj*(b-a)/n;
inum2=inum2+fxi*(b-a)/n;
inum3=inum3+fxij*(b-a)/n;
end
inum1
inum2
inum3
integrate=int(fx,0,1)
integrate=double(integrate)
fprintf('The relative error between inum1 and real-value is about:%d\n\n',...
abs((inum1-integrate)/integrate))
fprintf('The relative error between inum2 and real-value is about:%d\n\n',...
abs((inum2-integrate)/integrate))
fprintf('The relative error between inum3 and real-value is about:%d\n\n',...
abs((inum3-integrate)/integrate))
附录2:梯形法(fulu2.m)
format long
n=100;a=0;b=1;inum=0;
syms x fx
fx=1/(1+x^2);
for i=1:n
xj=a+(i-1)*(b-a)/n;
xi=a+i*(b-a)/n;
fxj=subs(fx,'x',xj);
fxi=subs(fx,'x',xi);
inum=inum+(fxj+fxi)*(b-a)/(2*n);
end
inum
integrate=int(fx,0,1)
integrate=double(integrate)
fprintf('The relative error between inum and real-value is about:%d\n\n',...
abs((inum-integrate)/integrate))
梯形法不用理他,只需改矩形法的
帮忙把下面c语言程序改为c++语言,程序为用矩形法求fx=1/(1+x^2)在0到1的定积分近似值,求修改成能运行啊format longn=100;a=0;b=1;inum1=0;inum2=0;inum3=0;syms x fxfx=1/(1+x^2);for i=1:nxj=a+(i-1)*(b-a)/n; %左点xi=a+i*(b
#include
#include
class tang
{
double a,b,n,x1,x2,x3;
public:
tang(int a,int b,int k);
int calc();
void show();
double f(double x);
};
tang::tang(int i,int j,int k):a(i),b(j),n(k)
{
}
double tang::f(double x)
{
return (1/(1+x*x));
}
int tang::calc()
{
if(a>b)
return -1;
double h=(b-a)/n;
int i;
x1=x2=x3=0;
for(i=0;i