在 C 中集成复合函数
Integrating composite function in C
我正在尝试编辑使用梯形规则来集成函数的代码。
我的问题是该程序似乎不喜欢我有不止一个涉及 x 的术语这一事实。
$f(x)=\frac{\beta\gamma}{\delta}e^{-\gamma a}\lr{1-e^{-\delta a}}e^{- \frac{\alpha\beta \mathbf{S}}{\delta}\lr{1-e^{-\delta a}}}$
这是我想近似计算的积分图片,因为乳胶似乎不起作用:
我从使用梯形法则的 C(第 2 版)数值食谱中获取了代码:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double d = 0.7, b = 2.5, g = 1.1, a=0.3; /*One species case: delta, beta, gamma, alpha */
float s1=0.564029; /*s value*/
double exp(double x); /*defining e^x*/
/* Define function here */
double f (double x) { double myresult = b*g/d*exp(-g*x)*(1-exp(-d*x))*exp(-a*b*s1/d*(1-exp(-d*x))); return myresult; }
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
system("clear");
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
/* Finding Integration Value */
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
integration = integration + 2 * f(k);
}
integration = integration * stepSize/2;
printf("\nIntegral of given function for given values is: %.3f", integration);
getchar();
return 0;
}
这段代码给我错误,我相信是因为我的#Define f(x) 有不止一项涉及 'x'。
错误:
constant.c:33:18: error: expected ')'
integration = f(lower) + f(upper);
^
constant.c:33:16: note: to match this '('
integration = f(lower) + f(upper);
^
constant.c:13:37: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
constant.c:33:18: error: expected ')'
integration = f(lower) + f(upper);
^
constant.c:33:16: note: to match this '('
integration = f(lower) + f(upper);
^
constant.c:13:65: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
constant.c:33:29: error: expected ')'
integration = f(lower) + f(upper);
^
constant.c:33:27: note: to match this '('
integration = f(lower) + f(upper);
^
constant.c:13:37: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
constant.c:33:29: error: expected ')'
integration = f(lower) + f(upper);
^
constant.c:33:27: note: to match this '('
integration = f(lower) + f(upper);
^
constant.c:13:65: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
constant.c:37:37: error: expected ')'
integration = integration + 2 * f(k);
^
constant.c:37:35: note: to match this '('
integration = integration + 2 * f(k);
^
constant.c:13:37: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
constant.c:37:37: error: expected ')'
integration = integration + 2 * f(k);
^
constant.c:37:35: note: to match this '('
integration = integration + 2 * f(k);
^
constant.c:13:65: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
6 errors generated.
如果我只有
#define f(x) b*g/d*exp(-g*x)
然后它确实给出了该项的正确近似值。
我试过分别定义这三个部分,即
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double d = 0.7, b = 2.5, g = 1.1, a=0.3; /*One species case: delta, beta, gamma, alpha */
float s1=0.564029; /*s value*/
double exp(double x); /*defining e^x*/
/* Define function here */
#define f(x) b*g/d*exp(-g*x)
#define g(x) (1-exp(-d x))
#define h(x) exp(-a*b*s1/d*(1-exp(-d x)))
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
system("clear");
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
/* Finding Integration Value */
integration = f(lower)*g(lower)*h(lower) + f(upper)*g(upper)*h(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
integration = integration + 2 * f(k)*g(k)*h(k);
}
integration = integration * stepSize/2;
printf("\nIntegral of given function for given values is: %.3f", integration);
getchar();
return 0;
}
但这也给我一个错误。
只需将所有 exp(-d x)
替换为 exp(-d * x)
。
我正在尝试编辑使用梯形规则来集成函数的代码。 我的问题是该程序似乎不喜欢我有不止一个涉及 x 的术语这一事实。
$f(x)=\frac{\beta\gamma}{\delta}e^{-\gamma a}\lr{1-e^{-\delta a}}e^{- \frac{\alpha\beta \mathbf{S}}{\delta}\lr{1-e^{-\delta a}}}$
这是我想近似计算的积分图片,因为乳胶似乎不起作用:
我从使用梯形法则的 C(第 2 版)数值食谱中获取了代码:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double d = 0.7, b = 2.5, g = 1.1, a=0.3; /*One species case: delta, beta, gamma, alpha */
float s1=0.564029; /*s value*/
double exp(double x); /*defining e^x*/
/* Define function here */
double f (double x) { double myresult = b*g/d*exp(-g*x)*(1-exp(-d*x))*exp(-a*b*s1/d*(1-exp(-d*x))); return myresult; }
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
system("clear");
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
/* Finding Integration Value */
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
integration = integration + 2 * f(k);
}
integration = integration * stepSize/2;
printf("\nIntegral of given function for given values is: %.3f", integration);
getchar();
return 0;
}
这段代码给我错误,我相信是因为我的#Define f(x) 有不止一项涉及 'x'。
错误:
constant.c:33:18: error: expected ')'
integration = f(lower) + f(upper);
^
constant.c:33:16: note: to match this '('
integration = f(lower) + f(upper);
^
constant.c:13:37: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
constant.c:33:18: error: expected ')'
integration = f(lower) + f(upper);
^
constant.c:33:16: note: to match this '('
integration = f(lower) + f(upper);
^
constant.c:13:65: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
constant.c:33:29: error: expected ')'
integration = f(lower) + f(upper);
^
constant.c:33:27: note: to match this '('
integration = f(lower) + f(upper);
^
constant.c:13:37: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
constant.c:33:29: error: expected ')'
integration = f(lower) + f(upper);
^
constant.c:33:27: note: to match this '('
integration = f(lower) + f(upper);
^
constant.c:13:65: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
constant.c:37:37: error: expected ')'
integration = integration + 2 * f(k);
^
constant.c:37:35: note: to match this '('
integration = integration + 2 * f(k);
^
constant.c:13:37: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
constant.c:37:37: error: expected ')'
integration = integration + 2 * f(k);
^
constant.c:37:35: note: to match this '('
integration = integration + 2 * f(k);
^
constant.c:13:65: note: expanded from macro 'f'
#define f(x) b*g/d*exp(-g*x)*(1-exp(-d x))*exp(-a*b*s1/d*(1-exp(-d x)))
^
6 errors generated.
如果我只有
#define f(x) b*g/d*exp(-g*x)
然后它确实给出了该项的正确近似值。
我试过分别定义这三个部分,即
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double d = 0.7, b = 2.5, g = 1.1, a=0.3; /*One species case: delta, beta, gamma, alpha */
float s1=0.564029; /*s value*/
double exp(double x); /*defining e^x*/
/* Define function here */
#define f(x) b*g/d*exp(-g*x)
#define g(x) (1-exp(-d x))
#define h(x) exp(-a*b*s1/d*(1-exp(-d x)))
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
system("clear");
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
/* Finding Integration Value */
integration = f(lower)*g(lower)*h(lower) + f(upper)*g(upper)*h(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
integration = integration + 2 * f(k)*g(k)*h(k);
}
integration = integration * stepSize/2;
printf("\nIntegral of given function for given values is: %.3f", integration);
getchar();
return 0;
}
但这也给我一个错误。
只需将所有 exp(-d x)
替换为 exp(-d * x)
。