如何用c#代码求解多项式方程 [英] how to solve polynomial equation using c# code

查看:562
本文介绍了如何用c#代码求解多项式方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



我正在用c#开发桌面应用程序,打算解决用户存储在字符串中的三角函数方程。



string s =cos(x)+ ln(x)-e ^ x;



我希望我的应用程序执行某些操作,将随机值放在x的位置,因为它是一个未知变量并解决这个等式使其等于零。



Plzz尽快帮助我。

解决方案

我看了谷歌,第一次点击是关于Codeproject的文章 [ ^ ]。虽然本文中的代码可能不完全符合您的要求,但它是一个很好的起点。在您编写了自己的代码后,如果遇到问题,您可以随时在这里提出具体问题。一定要在遇到问题的地方加入代码行,并提供问题的详细说明。


不要把它放得太精细,我会放弃目前的想法。对Wolfram Alpha的快速检查超出了他们的计算限制,试图通过理性方式解决这个问题,所以你的随机数想法可能会有持续时间问题......就像太阳老去世一样。 :笑:



如果你需要解决的多项式表达式比你需要的要多得多,那就复杂得多了。

快点 Google [ ^ ]提供近5,000,000次点击,其中一些看起来很有趣:多项式方程求解器 [ ^ ]但我认为不允许使用trig和log函数。你可以看一下,你可能会有足够的想法开始使用它作为基础 - 或者看看其他一些Googler的点击,它们也可能非常相关。



但是......我怀疑你已经咬过的东西比用这个可以咀嚼的多了......这不是一个我想在不经过深思熟虑的情况下跳进去的项目!

这是一个非常大的问题,在快速解答中无法充分解决。问题是:这不是数字问题。这是创建或使用计算机代数系统的问题:

http://en.wikipedia。 org / wiki / Computer_algebra_system [ ^ ]。



此类系统执行分析(符号),而非数字解决方案。 符号并不意味着它们与表示表达式的字符串有关。相反,它们在表达式树上运行。我开发了这样的解决方案,但没有准备好给你任何细节,原因有很多。其中一个原因是:我不相信你现在有足够的知识/经验。如果你认为你做了,那就太棒了:你可以做一些好的研究并使用一些可用的系统(根本不是很容易,原因很多),或者你自己开发。



此外,你在字符串中显示的内容不是等式。充其量只是左边的右边部分。也就是说,您的解决方案根本未定义。 (这就是为什么我认为你解决这些大问题的技巧是不够的;抱歉,如果我错了。)此外,很可能,解决方案可能在分析上变得无法解决。如果你给我们遗漏了等式,我们可能会帮助解决它或者告诉你它是否无法解决。



此外,你可以用数字方式解决它。而不是无意义的字符串s =cos(x)+ ln(x)-e ^ x; 写下函数:

 使用系统; 
// ...

(x)= < span class =code-keyword>> { return Math.Cos(x)+ Math.Log(x) - Math.Exp(x ); }

//

double LeftPart(dounle x){ return Math.Cos(x)+ Math.Log(x) - Math。 EXP(X); }

并尝试使用它来找到解决方案(再次,它将取决于您在等式的右/左部分有什么)。



有许多数值方法可以解决已经实现的方程,你可以学习它们。这是一个很大的话题。在所有情况下,你可以自己创建一些解决方案(可以吗?:-))...



-SA


Hi !

I am developing a desktop application in c# , intending to solve a trigonometric equation that is stored in a string by a user.

string s ="cos(x)+ln(x)-e^x";

I want my application to perform certain operations for putting random values in a place of "x" as it is an unknown variable and solve this equation to bring it equal to zero.

Plzz help me as soon as possible.

解决方案

I looked on Google and the very first hit was an article on Codeproject[^]. While the code in this article may not do exactly what you want it is a good place to start. After you have written some code of your own, if you get stuck, you should feel free to ask specific questions here. Be sure to include the lines of code where you are having the problem as well as providing a detailed description of your issue.


Not to put too fine a point on it, I'd drop the current idea. A quick check on Wolfram Alpha exceeded their computation limit trying to solver that one by rational means, so your "random number" idea it likely to have duration problems...like the Sun dying of old age. :laugh:

If you need to solve polynomial expressions than you need to be a lot, lot more sophisticated than that.
A quick Google[^] gives nearly 5,000,000 hits, and several of them look interesting: Polynomial Equation Solver[^] for example, but I don't think that allows for trig and log functions. You could look though, and you may get enough ideas to start using it as a basis - or look at some of the other Googler hits, they could also be very relevant.

But...I suspect you have bitten off rather more than you can chew with this one...it's not a project I would want to leap into without a heck of a lot of thought!


This is a really big problem which cannot be adequately solved in a Quick Answer. The problem is: this is not a numeric problem. This is a problem of creation or using of a Computer Algebra System:
http://en.wikipedia.org/wiki/Computer_algebra_system[^].

Such systems perform analytic (symbolic), not numeric solutions. "Symbolic" does not mean they have anything to do with strings representing expression. Instead, they operate on expression trees. I developed such solution but not ready to give you any detail, by many reasons. One of such reasons is: I don't believe you have enough knowledge/experience at this moment. If you think you do, it would be great: you could do some good research and use some available system (which is not easy at all, by, again, many reasons), or develop you own.

Besides, what you show in your string, is not an equation. At best, this is only its left of right part. That said, your solution is simply undefined. (That's why I though that you skills of solving such big problems are way insufficient; sorry if I'm wrong.) Besides, chances are, the solution may become unresolvable analytically. If you give us missing part of equation, we may help to solve it or tell you if it seems unsolvable.

Besides, you can solve it digitally. Instead of pointless string s ="cos(x)+ln(x)-e^x"; write the function:

using System;
//...

(x) => { return Math.Cos(x) + Math.Log(x) - Math.Exp(x); }

// or

double LeftPart(dounle x) { return Math.Cos(x) + Math.Log(x) - Math.Exp(x); }

and try to use it to find the solution (again, it will depend on what do you have on right/left part of your equation).

There are many numeric methods for solving equations which are already implemented, you can learn them. This is a whole big topic. In all cases, you can create some solution by yourself (can you? :-))…

—SA


这篇关于如何用c#代码求解多项式方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆