- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C 编程新手,正在编写一个程序来求解简单的微分方程,该方程将输出作为 x 的值。但我没有得到正确的结果。
我得到了正确的方程值,但微分方程的值是错误的。代码编译时没有任何警告或错误。
#include <stdio.h>
#include <conio.h>
#include <math.h>
float poly(float a[], int, float);
float deriv(float a[], int, float);
int main()
{
float x, a[10], y1, dy1;
int deg, i;
printf("Enter the degree of polynomial equation: ");
scanf("%d", °);
printf("Ehter the value of x for which the equation is to be evaluated: ");
scanf("%f", &x);
for(i=0;i<=deg;i++)
{
printf("Enter the coefficient of x to the power %d: ",i);
scanf("%f",&a[i]);
}
y1 = poly(a, deg, x);
dy1 = deriv(a, deg, x);
printf("The value of polynomial equation for the value of x = %.2f is: %.2f",x,y1);
printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f",x,dy1);
return 0;
}
/* function for finding the value of polynomial at some value of x */
float poly(float a[], int deg, float x)
{
float p;
int i;
p = a[deg];
for(i=deg;i>=1;i--)
{
p = (a[i-1] + x*p);
}
return p;
}
/* function for finding the derivative at some value of x */
float deriv(float a[], int deg, float x)
{
float d[10], pd = 0, ps;
int i;
for(i=0;i<=deg;i++)
{
ps = pow(x, deg-(i+1));
d[i] = (deg-1)*a[deg-1]*ps;
pd = pd + d[i];
}
return pd;
}
最佳答案
你犯了一个简单的逻辑错误。函数中 float deriv(float a[], int deg, float x)
应该是 d[i] = (deg-i)*a[deg-i]*ps;
。所以你的函数看起来像这样
/* function for finding the derivative at some value of x */
float deriv(float a[], int deg, float x)
{
float d[10], pd = 0, ps;
int i;
for(i=0;i<=deg;i++)
{
ps = pow(x, deg-(i+1));
d[i] = (deg-i)*a[deg-i]*ps;
pd = pd + d[i];
}
return pd;
}
祝你 future 一切顺利。
关于c - 用C语言求解微分方程时错误答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56919237/
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
Julia的新手,试图测试ODE求解器的速度。我在本教程中使用了Lorenz方程 using DifferentialEquations using Plots function lorenz(t,u
我来这里是因为我一直在尝试使用 sympy 求解微分方程,不幸的是到目前为止我还没有成功。到目前为止我所做的是: 1)插入微分方程,赋值并求解: import sympy as sp from IPy
我不知道问这个地方是否合适,因为我的问题是关于如何使用微分方程增长和衰减方法计算计算机科学算法的复杂性。 我想证明的算法是二分查找排序数组,其复杂度为log2(n) 算法说:如果要搜索的目标值等于中间
我想知道是否有人可以帮助我使用 MatLab 求解 Lotka-Volterra 方程。我的代码似乎不起作用。我执行以下操作: 第 1 步 - 我创建了一个名为 pred_prey_odes.m 的文
我是一名优秀的程序员,十分优秀!