Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.
Posted
Comments
OriginalGriff 19-Mar-14 15:44pm    
And what have you tried?
Where are you stuck?
What help do you need?
Syed Daniyal Ali 19-Mar-14 15:49pm    
i didn't try anything, because i m stuck to solve an equation stored as a string. As i know we can't perform any mathematical operations on string until we convert it in to double or int.. but i don't know what to do for equation, and how to put values in X using loops to find my equation value on which it is becoming equal to zero.

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.
 
Share this answer
 
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!
 
Share this answer
 
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:
C#
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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900