首页 > 编程语言 >C#凹凸曲线求拐点算法

C#凹凸曲线求拐点算法

时间:2023-04-24 19:36:03浏览次数:29  
标签:f2 return xm C# double 凹凸 算法 points PointType

凹凸曲线求拐点算法实现:

代码:

  1         public static double diff(Func<double, double> f, double x, double h)
  2         {
  3             return (f(x+h)-f(x-h))/(2*h);
  4         }
  5 
  6         public static double diff2(Func<double, double> f, double x, double h)
  7         {
  8             return (f(x+h)-2*f(x)+f(x-h))/(h*h);
  9         }
 10 
 11 
 12         enum PointType { None, Minimum, Maximum, Inflection };
 13 
 14         private static PointType getPointType(Func<double, double> f, double x, double h)
 15         {
 16             double f1 = diff(f, x, h);
 17             double f2 = diff2(f, x, h);
 18 
 19             if (Math.Abs(f2) < 1e-6)
 20             {
 21                 return PointType.None;
 22             }
 23             else if (f2 > 0)
 24             {
 25                 return PointType.Minimum;
 26             }
 27             else if (f2 < 0)
 28             {
 29                 return PointType.Maximum;
 30             }
 31             else if (f1 != 0)
 32             {
 33                 return PointType.Inflection;
 34             }
 35             else
 36             {
 37                 return PointType.None;
 38             }
 39         }
 40 
 41         // List<double> findTurningPoints = null;
 42         //Func<double, double> t = x => x * x * x - 3 * x * x + 2 * x + 1;
 43 
 44         //List<double> points = findTurningPoints(x => (x * x * x - 3 * x * x + 2 * x + 1),-10,10,0.01);
 45 
 46 
 47 
 48 
 49         public static List<double> findTurningPoints(Func<double, double> f, double a, double b, double h)
 50         {
 51             List<double> points = new List<double>();
 52 
 53             double fa = f(a);
 54             double fb = f(b);
 55 
 56             if (fa == fb)
 57             {
 58                 return points;
 59             }
 60 
 61             if (fa < fb)
 62             {
 63                 for (double x = a; x < b; x += h)
 64                 {
 65                     double fx = f(x);
 66                     double fxh = f(x + h);
 67 
 68                     if (fx < fxh)
 69                     {
 70                         continue;
 71                     }
 72 
 73                     double x1 = x - h;
 74                     double x2 = x + h;
 75 
 76                     while (x2 - x1 > 1e-6)
 77                     {
 78                         double xm = (x1 + x2) / 2;
 79                         double f1 = f(xm - h);
 80                         double f2 = f(xm);
 81                         double f3 = f(xm + h);
 82 
 83                         if (f2 < f1 && f2 < f3)
 84                         {
 85                             points.Add(xm);
 86                             break;
 87                         }
 88                         else if (f1 < f3)
 89                         {
 90                             x2 = xm;
 91                         }
 92                         else
 93                         {
 94                             x1 = xm;
 95                         }
 96                     }
 97                 }
 98             }
 99             else
100             {
101                 for (double x = a; x < b; x += h)
102                 {
103                     double fx = f(x);
104                     double fxh = f(x + h);
105 
106                     if (fx > fxh)
107                     {
108                         continue;
109                     }
110 
111                     double x1 = x - h;
112                     double x2 = x + h;
113 
114                     while (x2 - x1 > 1e-6)
115                     {
116                         double xm = (x1 + x2) / 2;
117                         double f1 = f(xm - h);
118                         double f2 = f(xm);
119                         double f3 = f(xm + h);
120 
121                         if (f2 > f1 && f2 > f3)
122                         {
123                             points.Add(xm);
124                             break;
125                         }
126                         else if (f1 > f3)
127                         {
128                             x2 = xm;
129                         }
130                         else
131                         {
132                             x1 = xm;
133                         }
134                     }
135                 }
136             }
137 
138             for (int i = 0; i < points.Count; i++)
139             {
140                 PointType type = getPointType(f, points[i], h);
141 
142                 if (type == PointType.None)
143                 {
144                     points.RemoveAt(i);
145                     i--;
146                 }
147             }
148 
149             return points;
150         }
凹凸曲线求拐点算法实现

 



List<double> points = findTurningPoints(x => x * x * x - 3 * x * x + 2 * x + 1, -10, 10, 0.01);
foreach (double x in points) {     Console.WriteLine("Turning point at x = {0}", x); } 欢迎反馈

标签:f2,return,xm,C#,double,凹凸,算法,points,PointType
From: https://www.cnblogs.com/CS-ComputerScience/p/17350594.html

相关文章

  • springboot mybatis 动态调用oracle存储过程,通过存储过程名称,就能动态调用存储过程、j
    由于在开发业务时,可能同时调用的存储过程不知道参数,但是参数从界面、或已经存储在数据库的获取,所以就不希望手动写存储过程的参数,通过简化的调用。能不能写个动态的业务,只输入存储过程名称,自动获取存储过程参数,并且参数的数据从前台传递过来,这个就通用了。只写一个通用方法,就可以......
  • 【Oracle】year must be between -4713 and +9999,and not be 0
    【Oracle】yearmustbebetween-4713and+9999,andnotbe0yearmustbebetween-4713and+9999,andnotbe0出现问题的时候一般是to_date的地方有问题,很有可能是有字符串或者空格在数据中过滤掉就行......
  • Function-advanced
    函数进阶1.函数防抖在频率触发的情况下只有等待一定的时间才会触发执行一次代码特点执行之前先清除之前的即将执行操作保证只有最后一次生效优化高频执行JS代码操作提高性能防抖代码实现//使用setTimeout实现函数防抖vart=null;document.onmous......
  • Acwing 3728-城市通电 / 最小生成树,建图,超级源点
    AcWing3728.城市通电做出来就凭之前的一句感悟:把每个动态选择变为与超级源点连的一条边,把这条边加入图里面跑最小生成树就相当于考虑了每个动态选择......
  • ctfshow-菜狗杯-wp
    MISCmisc1下载附件解压misc2下载附件解压发现解压不了用010打开发现是png图片后缀改为png文字识别一下就好了CRYPTO签到密码16进制转字符串caesar根据题目发现是凯撒直接离线工具一把梭可以发现在3时出现flag加上头部提交即可0x36d16进制的密码转为10进制......
  • 飞腾CPU FT-2000/4 uboot下PHY调试记录
    飞腾爱好者技术交流群码公众号“乌拉大喵喵” 一、环境说明板子是FT-2000/4的开发板:固件版本:ft-2004c_u-boot-v2-Ver0.3_202112231001.tar.gzft2004c_v2.06_image_fix.rar 二、调试命令说明调试PHY主要用到的命令是mii,先查看下可用的命令:miidevice,缩写miidev,查看......
  • Go: process synchronization
     packagemainimport("fmt""math""runtime""strconv""strings""time")funcproducer(intChanchanint){forb:=1000;b<=1099;b++{intChan<......
  • Docker容器日志中文乱码问题
    背景:在使用docker部署springboot项目的时候,查看项目日志的过程中,发现中文打印出来的都是乱码:??????楼主是通过dockerfile创建的镜像,解决方案:DcokerFile中增加环境变量ENVTZ'Asia/Shanghai'ENVLANGen_US.UTF-8ENVLANGUAGEen_US:enENVLC_ALLen_US.UTF-8其......
  • C/C++服务端客户端通讯程序[2023-04-24]
    C/C++服务端客户端通讯程序[2023-04-24]Socket通讯程序..服务器端).pptx任务:Socket通讯程序开发·基本要求(80分)∶完成一对一的Socket客户端与服务器程序·进阶要求(90分)∶在完成基本要求基础上,将服务器端程序改为多线程程序·高级要求(100分)︰将客户端和服务器端都改为多......
  • Educational Codeforces Round 147 (Rated for Div. 2)
    题目链接B核心思路真的需要加强了,看到这个最大值和最小值我们其实也需要往对应的最大值和最小值的相关操作去想,不如前缀最小值/前缀最大值或者是后缀最小值/后缀最大值。这里一个比较直接的想法就是想找到不同的地方,然后看不可以扩展。那么怎么看是否可以扩展呢,如果是左边的话......