指数泰勒级数(Exponential Taylor Series)
这是我到目前为止的代码,因为我仍在试图弄清楚如何设置它,所以有点乱,但我无法弄清楚如何获得输出。 该代码应该采用指数的泰勒级数多项式,并检查获得近似值所需的迭代次数。
#include <stdio.h> #include <stdlib.h> #include <math.h> /*Prototype for functions used*/ double factorial (int); int main() { double input = 0; double exp_val; double delta = 1; int f =0; int n = 0; double taylor; int total; printf("Plese enter the exponent to check for convergence:\n"); scanf("%lf", &input); exp_val = exp(input); printf(" # Iter e^X Sum Diff\n"); printf("---- ------ ------- ----- --------"); while(delta > 0.00001) { f = factorial(n); taylor = ((pow(input,n))/ f); delta = (exp_val - taylor); printf("%d %f %f %f/n", (n+1), exp_val, taylor, delta); n++; } system("pause"); } double factorial (int n) { int r = 0; int sum = 1; int total = 0; if (n == 0) return total =1; else { for(r; r<n; r++) { sum = sum * r; total = sum + 1; } return total; } }
This is the code I have so far, which is a little messy since I am still trying to figure out how to set it up, but I cannot figure out how to get the output. This code is supposed to take a Taylor Series polynomial of an exponential, and check the amount of iterations it takes to get the approximation.
#include <stdio.h> #include <stdlib.h> #include <math.h> /*Prototype for functions used*/ double factorial (int); int main() { double input = 0; double exp_val; double delta = 1; int f =0; int n = 0; double taylor; int total; printf("Plese enter the exponent to check for convergence:\n"); scanf("%lf", &input); exp_val = exp(input); printf(" # Iter e^X Sum Diff\n"); printf("---- ------ ------- ----- --------"); while(delta > 0.00001) { f = factorial(n); taylor = ((pow(input,n))/ f); delta = (exp_val - taylor); printf("%d %f %f %f/n", (n+1), exp_val, taylor, delta); n++; } system("pause"); } double factorial (int n) { int r = 0; int sum = 1; int total = 0; if (n == 0) return total =1; else { for(r; r<n; r++) { sum = sum * r; total = sum + 1; } return total; } }
原文:https://stackoverflow.com/questions/22122846
最满意答案
感谢@Phorgz&@GabeRogan指引我朝着正确的方向前进。 Delaunay Triangulation绝对是最佳选择,即使将画布更新为动画,结果也非常快速。
我最终使用了npm包更快delaunay ,它使用分而治之算法对随机生成的点进行三角测量。
以下是我在画布上绘制的结果,点随着飞机移动而更新:
Thanks to @Phorgz & @GabeRogan for pointing me in the right direction. Delaunay Triangulation was definitely the way to go and it ended up being very fast, even when updating the canvas as an animation.
I did end up using the npm package faster-delaunay which uses the divide and conquer algorithm to triangulate the randomly generated points.
Here is a result of what I have drawn on the canvas that updates as the points move around the plane:
相关问答
更多-
一个好的解决方案是从随机点开始(使用您的首选分布)并应用一些三角测量算法。 其中,Delaunay三角剖分是一个很好的候选者,因为它的计算复杂度和代码可用性都很低。 A good solution is to start with random points (with your preferred distribution) and apply some triangulation algorithm. Among these, the Delaunay triangulation is a good c ...
-
问题1:为什么这些点是随机顺序的? 如果是,则必须对它们进行排序,以便用直线连接连续点会产生凸多边形。 如何订购它们 - 例如,通过运行凸包算法(尽管可能还有更简单的方法)。 订购后,按照此处的说明计算区域。 - 问题2更简单。 半平面由具有隐式方程a*x+b*y+c=0的单个线定义; a*x+b*y+c <= 0 (注意不等式)的所有点(x,y)都在半平面的“后面”。 现在,您需要至少三个平面,以便它们的负半空间的交点关闭(这是必要的,但不是充分的条件)。 如果交叉点已关闭,则它将是凸多边形。 我建议你维 ...
-
如果您确定几何体是否始终是凸的,那么您真正需要的是凸壳算法。 凸壳:此算法通过仅连接3d空间中给定点集的最外侧顶点来创建网格。 显然,您将松开凸结构内部的顶点,不会在任何三角形中使用。 这仅适用于凸形。 下面是凸壳结构的一个很好的例子。 有许多库实现了Convex hulling算法。 例如:CGAL,BulletPhysics和OpenMesh。 如果您正在寻找快速建造凸壳的方法,QuickHull是最好的地方。 http://www.qhull.org What you exactly need is ...
-
从给定的角度来看,施一条直线。 测试线与每个三角形的交点,并计算在点的同一侧找到的所有交点。 如果这个计数是奇数,你就在里面。 为了简化计算,对线使用x=x0 , y=y0并在XY平面上投影所有内容。 使用如何确定点是否在2D三角形中? 最后检查交叉点的z值。 From the given point, cast a straight line. Test intersection of the line with every triangle and count all intersections fou ...
-
如何遵循一个简单的GrahamScan Algo ...这是应该做的伎俩。 How About Following A simple GrahamScan Algo... That's should Do the Trick.
-
感谢@Phorgz&@GabeRogan指引我朝着正确的方向前进。 Delaunay Triangulation绝对是最佳选择,即使将画布更新为动画,结果也非常快速。 我最终使用了npm包更快delaunay ,它使用分而治之算法对随机生成的点进行三角测量。 以下是我在画布上绘制的结果,点随着飞机移动而更新: Thanks to @Phorgz & @GabeRogan for pointing me in the right direction. Delaunay Triangulation was de ...
-
连接点做一个三角形(Connecting points to make a triangle)[2024-01-13]
Delaunay Triangulation是你的朋友! 如果您使用Google这个术语,那么有很多可用的资源,其背后的数学/逻辑并不太难。 快速制作(但完全可行)有点困难,但这完全取决于您的要求。 Delaunay Triangulation is your friend! There are lots of resources available about it if you Google the term, and the math/logic behind it isn't too tough. ... -
建议: 不要使用getGraphics()来获取Graphics对象,因为如果执行任何重绘(不受控制的话getGraphics() ,获得的Graphics对象将不会持久存在。 而是绘制到BufferedImage并让JPanel或JComponent在其paintComponent覆盖中绘制BufferedImage,或者将您的图像数据添加到某种Collection的集合中,并让paintComponent覆盖方法使用绘制图像的信息迭代Collection。 不要直接在顶级窗口(如JFrame或JAppl ...
-
点和三角形之间的链接(Link between points and triangles)[2023-01-02]
计算机图形中的这种集合交互通常使用索引来实现。 class Point { float x,y; } class Triangle { unsigned int indices[3]; // indices in std::vector} std::vector points; std::vector triangles; 与指针解决方案相比,主要优点是您可以在着色器程序中使用此类索引引用。 编辑:通过已知索引删除顶点的代码示例(未测试) ... -
假设您的三角形是A:A1A2A3和B:B1B2B3 1)计算N1,A1A2和A1A3的归一化叉积。 2)测试B由A“看”,即N1•A1B1> 0。 3)计算N2,B1B2和B1B3的归一化交叉积。 如果你想测试'严格面对',那么测试N1•N2 == -1(其中•是点积)。 对于不太严格的面对,你可以测试N1•N2 - ( - 1)<一些小值。 正如@BenAaronson指出的那样,你可能想通过测试(A1A2,A1A3)和(B1B2,B1B3)来共同测试rectn是否在同一平面上。 如果O是原点,则测试N1 ...