鱼C论坛

 找回密码
 立即注册
查看: 2417|回复: 5

[已解决]求方程的近似解,第18行报错,expected primary-expression before float

[复制链接]
发表于 2020-10-24 14:02:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 猪猪虾 于 2020-10-24 14:03 编辑

用弦截法求方程
f(x)=x3-5x2+16x-80=0 的根

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. int main()
  5. {
  6.         float x1,x2,x,y1,y2,y;
  7.        
  8.         float function(float x);
  9.         float xpoint(float x1,float x2);
  10.         float root(float x1,float x2);
  11.        
  12.         printf("enter the x1:\n") ;
  13.         scanf("%f",&x1);
  14.         printf("enter the x2:\n") ;
  15.         scanf("%f",&x2);
  16.         do
  17.         {
  18.                 x = xpoint(float x1,float x2);
  19.                 y = abs(float function(float x))
  20.                 x1,x2 = float root(float x1,float x2);
  21.         }while(y < (e-6 ));
  22.        
  23.         return 0;
  24. }

  25. //定义直线方程
  26. float function(float x)
  27. {
  28.         float y;
  29.         y = pow(x,3) - 5 * pow(x,2) + 16 * x - 80.0;
  30.         return y;
  31. }

  32. //求两点形成的直线与x轴的交点的横坐标
  33. float xpoint(float x1,float x2)
  34. {
  35.         float function(float x);
  36.         float y1 y2,x,y = 0;
  37.         y1 = float function(float x1);
  38.         y2 = float function(float x2);
  39.         x = ((x1 -x2)/ (y1 -y2)) * (y - y2)  + x2;
  40.         return x;
  41. }

  42. //跟新x1,x2,y1,y2
  43. float root(float x1,float x2)
  44. {
  45.         float xpoint(float x1,float x2);
  46.         float function(float x);
  47.        
  48.         float y,y1,t;
  49.         x = float xpoint(float x1,float x2);
  50.         y1 = float function(float x1);
  51.         y = float function(float x);
  52.         if ((y1 > 0 & y > 0) || (y1 < 0 & y < 0)
  53.         {
  54.                 x1 = x;
  55.                 y1 = y;
  56.         }
  57.         else
  58.         {
  59.                 x2 = x;
  60.                 y2 = y;
  61.         }
  62.         return x1,x2;
  63. }
复制代码
最佳答案
2020-10-26 04:08:29
猪猪虾 发表于 2020-10-24 14:45
题目和解题思路在最下面,程序我修改过了,输入2,6,输出的结果应该是5

为了你这个程序,将我忘得干干净净的数学又复习了一遍


  1. #include <stdio.h>
  2. double x,x1,x2;           // 定义全局变量

  3. double f(double x)
  4. {
  5.     double c;
  6.     c = x * x * x - 5 * x * x + 16 * x - 80;
  7.     return(c);
  8. }

  9. double root(double x1,double x2)
  10. {
  11.     do
  12.     {
  13.         double k,b;
  14.         k = (f(x1) - f(x2)) / (x1 - x2);  // 过点 (x1,f(x1)), (x2,f(x2)) 的弦
  15.         b = f(x1) - k * x1;
  16.         x = - b / k;
  17.         if(f(x) * f(x1) >= 0)  // 若f(x)与f(x1)同符号,则根必在(x,x2)区间内
  18.         {
  19.             x1 = x;            // 将x作为新的x1
  20.         }
  21.         if(f(x) * f(x2) >= 0)  // 若f(x)与f(x2)同符号,则根必在(x1,x)区间内
  22.         {
  23.             x2 = x;            // 将x作为新的x2
  24.         }
  25.     }while(f(x) < -0.00001 || f(x) > 0.00001);  // 精度要求判断
  26.     return f(x);
  27. }

  28. int main()
  29. {
  30.     while(f(x1) * f(x2) >= 0)  // 当输入两个数大于0为真时,继续重新输入
  31.     {
  32.         printf("输入预估的实根区间的两个数值 x1,x2: ");
  33.         scanf("%lf%lf",&x1,&x2);
  34.     }
  35.     root(x1,x2);
  36.     printf("%f\n",x);
  37.     return 0;
  38. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-10-24 14:25:35 | 显示全部楼层
很多错误,主要是调用函数时无须带参数类型

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. int main()
  5. {
  6.         float x1,x2,x,y1,y2,y;
  7.         
  8.         float function(float x);
  9.         float xpoint(float x1,float x2);
  10.         float root(float x1,float x2);
  11.         
  12.         printf("enter the x1:\n") ;
  13.         scanf("%f",&x1);
  14.         printf("enter the x2:\n") ;
  15.         scanf("%f",&x2);
  16.         do
  17.         {
  18.                 x = xpoint(x1,x2);
  19.                 y = abs(function(x));
  20.                 x1,x2 = root(x1,x2);
  21.         }while(y < 0.000001);
  22.         
  23.         return 0;
  24. }

  25. //定义直线方程
  26. float function(float x)
  27. {
  28.         float y;
  29.         y = pow(x,3) - 5 * pow(x,2) + 16 * x - 80.0;
  30.         return y;
  31. }

  32. //求两点形成的直线与x轴的交点的横坐标
  33. float xpoint(float x1,float x2)
  34. {
  35.         float function(float x);
  36.         float y1,y2,x,y = 0;
  37.         y1 = function(x1);
  38.         y2 = function(x2);
  39.         x = ((x1 -x2)/ (y1 -y2)) * (y - y2)  + x2;
  40.         return x;
  41. }

  42. //跟新x1,x2,y1,y2
  43. float root(float x1,float x2)
  44. {
  45.         float xpoint(float x1,float x2);
  46.         float function(float x);
  47.         
  48.         float x,y,y1,y2,t;
  49.         x = xpoint(x1,x2);
  50.         y1 = function(x1);
  51.         y = function(x);
  52.         if ((y1 > 0 & y > 0) || (y1 < 0 & y < 0))
  53.         {
  54.                 x1 = x;
  55.                 y1 = y;
  56.         }
  57.         else
  58.         {
  59.                 x2 = x;
  60.                 y2 = y;
  61.         }
  62.         return x1,x2;
  63. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-24 14:28:09 | 显示全部楼层
用DEV_C++5.11 TMD-GCC4.9.2 32-bit release 编译通过了,具体的怎么解你的方程,我就不知道了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-24 14:44:57 | 显示全部楼层
风过无痕1989 发表于 2020-10-24 14:28
用DEV_C++5.11 TMD-GCC4.9.2 32-bit release 编译通过了,具体的怎么解你的方程,我就不知道了

你再看一眼
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. int main()
  5. {
  6.         float x1,x2,x,y1,y2,y;
  7.        
  8.         float function(float x);
  9.         float xpoint(float x1,float x2);
  10.         float root(float x1,float x2);
  11.         do
  12.         {
  13.                 printf("enter the x1:\n") ;
  14.             scanf("%f",&x1);
  15.                 printf("enter the x2:\n") ;
  16.                 scanf("%f",&x2);
  17.         }while(function(x1) * function(x2) >= 0);
  18.        
  19.         do
  20.         {
  21.                 x = xpoint(x1,x2);
  22.                 y = abs(function(x));
  23.                 x1,x2 = root(x1,x2);
  24.         }while(y > pow(10,-6));
  25.         printf("the root of function is %f",x);
  26.        
  27.         return 0;
  28. }

  29. //定义直线方程
  30. float function(float x)
  31. {
  32.         float y;
  33.         y = pow(x,3) - 5 * pow(x,2) + 16 * x - 80.0;
  34.         return y;
  35. }

  36. //求两点形成的直线与x轴的交点的横坐标
  37. float xpoint(float x1,float x2)
  38. {
  39.         float function(float x);
  40.        
  41.         float y1 ,y2,x,y = 0;
  42.         y1 = function(x1);
  43.         y2 = function(x2);
  44.         x = ((x1 -x2)/ (y1 -y2)) * (y - y2)  + x2;
  45.         return x;
  46. }

  47. //跟新x1,x2,y1,y2
  48. float root(float x1,float x2)
  49. {
  50.         float xpoint(float x1,float x2);
  51.         float function(float x);
  52.        
  53.         float x,y,y1,y2,t;
  54.         x =  xpoint(x1,x2);
  55.         y1 = function(x1);
  56.         y =  function(x);
  57.         if ((y1 > 0 & y > 0) || (y1 < 0 & y < 0) )
  58.         {
  59.                 x1 = x;
  60.                 y1 = y;
  61.         }
  62.         else
  63.         {
  64.                 x2 = x;
  65.                 y2 = y;
  66.         }
  67.         return x1,x2;
  68. }
复制代码

(1) 取两个不同点x1,x2,如果f(x1)和f(x2)符号相反,则(x1,x2)区间内必有一个根。如果f(x1)与f(x2)同符号,则应改变x1,x2,直到f(x1)、f(x2)异号为止。注意x1、x2的值不应差太大,以保证(x1,x2)区间内只有一个根。
(2) 连接(x1,f(x1))和(x2,f(x2))两点,此线(即弦)交x轴于x。
(3) 若f(x)与f(x1)同符号,则根必在(x,x2)区间内,此时将x作为新的x1。如果f(x)与f(x2)同符号,则表示根在(x1,x)区间内,将x作为新的x2
(4) 重复步骤 (2) 和 (3) , 直到 |f(x)|<ε 为止, ε为一个很小的数, 例如 10-6\. 此时认为 f(x)≈0

编程思路
(1) 用函数f(x)代表x的函数:x3-5x2+16x-80.
(2) 用函数调用xpoint (x1,x2)来求(x1,f(x1))和
     (x2,f(x2))的连线与x轴的交点x的坐标。
(3) 用函数调用root (x1,x2)来求(x1,x2)区间的
     那个实根。显然,执行root函数过程中要用
     到函数xpoint,而执行xpoint函数过程中要用
     到f函数。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-24 14:45:46 | 显示全部楼层
风过无痕1989 发表于 2020-10-24 14:28
用DEV_C++5.11 TMD-GCC4.9.2 32-bit release 编译通过了,具体的怎么解你的方程,我就不知道了

题目和解题思路在最下面,程序我修改过了,输入2,6,输出的结果应该是5
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-26 04:08:29 | 显示全部楼层    本楼为最佳答案   
猪猪虾 发表于 2020-10-24 14:45
题目和解题思路在最下面,程序我修改过了,输入2,6,输出的结果应该是5

为了你这个程序,将我忘得干干净净的数学又复习了一遍


  1. #include <stdio.h>
  2. double x,x1,x2;           // 定义全局变量

  3. double f(double x)
  4. {
  5.     double c;
  6.     c = x * x * x - 5 * x * x + 16 * x - 80;
  7.     return(c);
  8. }

  9. double root(double x1,double x2)
  10. {
  11.     do
  12.     {
  13.         double k,b;
  14.         k = (f(x1) - f(x2)) / (x1 - x2);  // 过点 (x1,f(x1)), (x2,f(x2)) 的弦
  15.         b = f(x1) - k * x1;
  16.         x = - b / k;
  17.         if(f(x) * f(x1) >= 0)  // 若f(x)与f(x1)同符号,则根必在(x,x2)区间内
  18.         {
  19.             x1 = x;            // 将x作为新的x1
  20.         }
  21.         if(f(x) * f(x2) >= 0)  // 若f(x)与f(x2)同符号,则根必在(x1,x)区间内
  22.         {
  23.             x2 = x;            // 将x作为新的x2
  24.         }
  25.     }while(f(x) < -0.00001 || f(x) > 0.00001);  // 精度要求判断
  26.     return f(x);
  27. }

  28. int main()
  29. {
  30.     while(f(x1) * f(x2) >= 0)  // 当输入两个数大于0为真时,继续重新输入
  31.     {
  32.         printf("输入预估的实根区间的两个数值 x1,x2: ");
  33.         scanf("%lf%lf",&x1,&x2);
  34.     }
  35.     root(x1,x2);
  36.     printf("%f\n",x);
  37.     return 0;
  38. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 12:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表