简体   繁体   English

在 1 C 中积分 2 个数学方程

[英]Integrating 2 Mathematical Equations in 1 C

I have written a program to calculate y values according to given x values for the function y = (sqrt(3+x^2))/(20x^2+sqrt(x)).我编写了一个程序来根据函数 y = (sqrt(3+x^2))/(20x^2+sqrt(x)) 的给定x值计算y值。 Using two counters, one for x values [i], and one for y values [n].使用两个计数器,一个用于 x 值 [i],一个用于 y 值 [n]。 My x values show up fine, however, the y values return zeroes.我的 x 值显示正常,但是,y 值返回零。 What would be the mistake here?这里会有什么错误? Very appreciated.非常感激。

    for (i = 0; i < 30; i++)
    {
        x[i] = 20 i * 2 + 3;
    }   

    for (n = 0; i < 30 && n < 50; i++, n++)
    {
        y[n] = (sqrt(3 + (pow(x[i], 2))))) / (20 * pow(x[i], 2) + sqrt(x[i]));
    }

    for (i = 0, n = 0; i < 30 && n < 50; i++, n++)
    printf("x %lf, y %lf", x[i], y[n]);

return 0;

} }

You are continuing to use i , without re-initializing it to 0 after the first for loop.您将继续使用i ,而无需在第一个for循环后将其重新初始化为0 Because the value of i stays at the value of times , the second for loop never gets to run.因为i的值保持在times的值,所以第二个for循环永远不会运行。 But you rightly initialized it while printing the values of x , y in the final loop.但是您在最终循环中打印xy的值时正确地初始化了它。

Change your second for loop to将您的第二个for循环更改为

for (i =0, n = 0; i < times && n < Ymax; i++, n++)
//   ^^^^^
{
    y[n] = 1 - (1 - (sqrt(4 - (pow(x[i], 2))))) / (40 * pow(x[i], 2) + sqrt(x[i]));
}

Add a "i=0" line the initializer part of your second for loop.在第二个 for 循环的初始值设定项部分添加“i=0”行。

You should use C99 style for loop like this :您应该像这样使用 C99 样式的循环:

for (int i = 0; ...)

to avoid such errors.以避免此类错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM