You are invited to Log in or Register a free Frihost Account!

# [Visual Basic] Divide X equation

Trickster
Hello I would like to know if it is possible to calculate equations with unknown variables like:
20 = 2*x^3 + 4

It's because I want to make a little program to solve divide equations with multiple x's like:
(3*x^7 + 5*x - 6) / (x - 4)

Instead of calculating them on a paper with a pen
Please show some examples if it's possible
Indi
 Trickster wrote: Hello I would like to know if it is possible to calculate equations with unknown variables like: 20 = 2*x^3 + 4 It's because I want to make a little program to solve divide equations with multiple x's like: (3*x^7 + 5*x - 6) / (x - 4) Instead of calculating them on a paper with a pen Please show some examples if it's possible

There are two ways to do it - symbolic manipulation and root finding. Symbolic manipulation is the most reliable, but it would take months to program something to do it - better would be to use something like Maple that already has symbolic manipulation.

The other option is root finding. What you do is first rewrite the equation to equal zero, so "20 = 2*x^3 + 4" becomes "0 = 2*x^3 + 4 - 20". Then you guess at an upper and lower limit for x, say x=3 and x=-3, and the middle of those two values, which is x=0 in this case. Then you substitute both values into the equation and you should get one positive answer and one negative answer, in this case, 2 for x=3, -34 for x=-3 and -16 for x=0.

Then you drop one of the upper or lower values, whichever one is the same sign as the middle value. So you drop x=-3 in this case. Your new middle between x=3 and x=0 is x=1.5. So you do the same thing again, over and over, until you get the result as 0.

There are other methods, but this is the simplest to code. The only tricky part is guessing what to use for the upper and lower bounds.
ThePolemistis
Indi wrote:
 Trickster wrote: Hello I would like to know if it is possible to calculate equations with unknown variables like: 20 = 2*x^3 + 4 It's because I want to make a little program to solve divide equations with multiple x's like: (3*x^7 + 5*x - 6) / (x - 4) Instead of calculating them on a paper with a pen Please show some examples if it's possible

There are two ways to do it - symbolic manipulation and root finding. Symbolic manipulation is the most reliable, but it would take months to program something to do it - better would be to use something like Maple that already has symbolic manipulation.

The other option is root finding. What you do is first rewrite the equation to equal zero, so "20 = 2*x^3 + 4" becomes "0 = 2*x^3 + 4 - 20". Then you guess at an upper and lower limit for x, say x=3 and x=-3, and the middle of those two values, which is x=0 in this case. Then you substitute both values into the equation and you should get one positive answer and one negative answer, in this case, 2 for x=3, -34 for x=-3 and -16 for x=0.

Then you drop one of the upper or lower values, whichever one is the same sign as the middle value. So you drop x=-3 in this case. Your new middle between x=3 and x=0 is x=1.5. So you do the same thing again, over and over, until you get the result as 0.

There are other methods, but this is the simplest to code. The only tricky part is guessing what to use for the upper and lower bounds.

There is a much easier and simple way..

1) First you type your expression:
e.g a * b

2) ask for numbers for values:
e.g. 3 * 4

2) You convert the expression to infix notation:
e.g. 3 4 *

3) You convert the expression to postfix notation:
e.g. 12

Indeed, if you program it correctly, will accept brackets, and all the other operators such as /, +,-, ^ etc etc.
The simplest way to program all this would be to use stack (FILO - first in last out) with precedence.
so with 3 * 4
the stack would be as:
3
3 *
3 4
3 4 *

of course, if you implement the algorithm correct for infix and postfix, it will work perfectly.
so like (((a+2) * b)/c) will work perfectly

I remember creating a program just like this for one of my assignments are uni, but in java. Ours was slightly more complex, because we had to include >, <, ==,, and return a boolean if for example:
(((a+2) * b)/c) > a+b+c*d

which basically meant, all of it was added to stack, and correct preceden ce given to all where the highest was given to ^ and the lowest to the <,>,==.
Trickster
hmm okay i'm a little confused, so i would like to see a little sample of how its done in visual basic.
so say i have the equation: 2x^3 + 4 = 20
How would you calculate that in visual basic, and i would like to see it from start to finish with the dimensioning of the variable x and the final outcome in another variable named result. I think it would clear things out a lot, and remember in the final program i wouldn't know x, so couldn't say it's just 2*2^3 + 4 = 20.
ThePolemistis
 Trickster wrote: hmm okay i'm a little confused, so i would like to see a little sample of how its done in visual basic. so say i have the equation: 2x^3 + 4 = 20 How would you calculate that in visual basic, and i would like to see it from start to finish with the dimensioning of the variable x and the final outcome in another variable named result. I think it would clear things out a lot, and remember in the final program i wouldn't know x, so couldn't say it's just 2*2^3 + 4 = 20.

whoops... may have got slightly carried away... perhaps because you had this statement (3*x^7 + 5*x - 6) / (x - 4) with no equal sign, and i thought u wanted to evaluate this expression... but actually u still do

u can the same method I highlighted in my initial post. Basically all u want to do is find the unknown type. Lets look at 2x^3 + 4 = 20.
Using my method described in previous post, just ignore the =20 part, and make x=1. convert infix to postfix, and u will have an answer, increase x if the result was less than 20, decrease x if more than 20.
Also, r we assuming x is always an integer? If not, then u can still do this way, but means your result will be achieved in a few longer seconds

Indi has a point also, actually better than my substitute 1 as x but im giving u a quick solution from top of my head, and i cant be bothered to think. But having said that, to implement indi's solution can only be done once u evaluate the equation with precedence. So basically, u still need to implment the infix to postfix conversion.

or perhaps vb has a feature that evaluates an equation automatically? if so, forget all the infix postfix waffle.

anyways take care
Indi
 Trickster wrote: hmm okay i'm a little confused, so i would like to see a little sample of how its done in visual basic. so say i have the equation: 2x^3 + 4 = 20 How would you calculate that in visual basic, and i would like to see it from start to finish with the dimensioning of the variable x and the final outcome in another variable named result. I think it would clear things out a lot, and remember in the final program i wouldn't know x, so couldn't say it's just 2*2^3 + 4 = 20.

i can't do visual basic, but i can do it in fake c.

Before you begin, ignore all that stuff ThePolemistis wrote. He was answering the wrong question.

The first thing you have to do is set up your function and initial guesses:
 Code: // This is just your function - you can change it to any function // you need to solve float f(float x) {    // All you have to do is rewrite your function so that it's equal to zero    // In this case:    // 0 = 2*x^3 + 4 - 20        return 2 * (x * x * x) + 4 - 20; } // Set the upper guess to 6 and the // lower guess to -6 // (i just picked those randomly) float xu = 6.0f; float xl = -6.0f; // You have to make sure that one y is positive and one is negative: if (f(xu) * f(xl) > 0)    // Error: your guesses are no good. Pick different guesses else if (f(xu) * f(xl) == 0)    // One of your guesses is the answer

Now you just have to loop until you find it:
 Code: // Make x halfway between the upper and lower guess float x = (xu + xl) / 2; // Loop until the result is zero while (f(x) != 0) {    // See whether the answer is between the middle and the upper    if (f(xu) * f(x) < 0)       xl = x;    // Or the middle and the lower    else       xu = x;        // Set the new middle    x = (xu + xl) / 2; } // When you get here, your answer is x

There's not much to it. You can solve any equation, just by replacing f() with whatever equation you want.

(Note: this code isn't very efficient, but it would work. Of course, there are epsilong issues with floating point numbers that you should probably consider.)
Trickster
Hmm well indi float is not an option in visual basic, I would just like to know how to dimension x as an unknown integer or just an unknown number because if i just say dim x as integer it is empty and i can't work with it in an equation (x * x * x) would give an error.

and ThePolemistis i understand how your method works with the equal sign, but you were right, i need it for an divide equation like (3*x^7 + 5*x - 6) / (x - 4) an so i wouldn't know what x should be it just need to work with x as an unknown variable as you do when you calculate equations on paper.
Indi
 Trickster wrote: Hmm well indi float is not an option in visual basic, I would just like to know how to dimension x as an unknown integer or just an unknown number because if i just say dim x as integer it is empty and i can't work with it in an equation (x * x * x) would give an error. and ThePolemistis i understand how your method works with the equal sign, but you were right, i need it for an divide equation like (3*x^7 + 5*x - 6) / (x - 4) an so i wouldn't know what x should be it just need to work with x as an unknown variable as you do when you calculate equations on paper.

Visual Basic has a float type. i don't know what it's called, but it has one. Just Dim x as Float or whatever instead of integer.
ThePolemistis
 Trickster wrote: Hmm well indi float is not an option in visual basic, I would just like to know how to dimension x as an unknown integer or just an unknown number because if i just say dim x as integer it is empty and i can't work with it in an equation (x * x * x) would give an error. and ThePolemistis i understand how your method works with the equal sign, but you were right, i need it for an divide equation like (3*x^7 + 5*x - 6) / (x - 4) an so i wouldn't know what x should be it just need to work with x as an unknown variable as you do when you calculate equations on paper.

VB does have float,,, its called long
You declare it by

 Code: dim myVal as Long

thats how u did in VB 6,,, im not sure about .net... to be honest, I don't like .net much, its a big change in programming from VB 6,,, however, saying that,,, it is much easier to interact with windows and stuff..

Anyways, regarding ur equation stuff... Firstly, I am assuming you wanna work out values for any arbitrary equation for instance (x + 3)/2 =50 or ((((a+5) * 2)+6)/3)...
if that is the case, then Indi's code will not work because he has defined for a specific eqquation(ie. you have to rewrite the equation) as shown with:

 Code: return 2 * (x * x * x) + 4 - 20;

If what I assume is correct(that the person types in the equation at input), then do what I said in my previous post.
If u want to work out x for a specific equation only, then I don't see the point of programming such a thing.

[/code]
Indi
ThePolemistis wrote:
 Trickster wrote: Hmm well indi float is not an option in visual basic, I would just like to know how to dimension x as an unknown integer or just an unknown number because if i just say dim x as integer it is empty and i can't work with it in an equation (x * x * x) would give an error. and ThePolemistis i understand how your method works with the equal sign, but you were right, i need it for an divide equation like (3*x^7 + 5*x - 6) / (x - 4) an so i wouldn't know what x should be it just need to work with x as an unknown variable as you do when you calculate equations on paper.

VB does have float,,, its called long
You declare it by

 Code: dim myVal as Long

Long is not a float. Try Float or Double.

ThePolemistis wrote:
Anyways, regarding ur equation stuff... Firstly, I am assuming you wanna work out values for any arbitrary equation for instance (x + 3)/2 =50 or ((((a+5) * 2)+6)/3)...
if that is the case, then Indi's code will not work because he has defined for a specific eqquation(ie. you have to rewrite the equation) as shown with:

 Code: return 2 * (x * x * x) + 4 - 20;

As i said:
Quote:
 Code: // This is just your function - you can change it to any function // you need to solve
 Quote: You can solve any equation, just by replacing f() with whatever equation you want.

The first challenge for a programmer is to determine requirements. That means listening carefully to what kind of program is desired, and what it has to do. The original poster said he just wanted a "little program to solve... equations... (i)instead of calculating them with a paper and pen". He goes on to show some sample equations and says that he wants to solve for x. He is asking for a solver.

You're trying to write an equation parser. That is an entirely different problem than an equation solver.

If the original poster wants, he can use your parser to parse the equation and then my solver to solve it. (In fact, that would be a pretty neat program. Would take an hour or two to write in C++ with Boost.Spirit. In VB? No idea.)

So you are misleading the original poster by telling him that my code will not work. i have given him exactly what he asked for. He has the code to solve pretty much any equation he will ever have to deal with to find x. All he has to do is change f(), recompile and solve.

What you are giving him will not do what he wants. Your code does not solve for x, it just performs calculations using equations once you already know what x is. It could be tied to what i wrote so that he wouldn't have to recompile every time he changed the function. But by itself, your code will not solve functions.
MrBlueSky
 Trickster wrote: Hello I would like to know if it is possible to calculate equations with unknown variables like: 20 = 2*x^3 + 4 It's because I want to make a little program to solve divide equations with multiple x's like: (3*x^7 + 5*x - 6) / (x - 4) Instead of calculating them on a paper with a pen Please show some examples if it's possible

I am curious how you solve such equations using paper and pen. What do you mean by solving them?
qebab
 MrBlueSky wrote: I am curious how you solve such equations using paper and pen. What do you mean by solving them?

I am guessing that what he wants to do is to write a program that divides a polynomial on another (Polynomial division), though he's not expressing himself clearly.

Is something like this what you want? Basically this method?
ThePolemistis
 Indi wrote: Long is not a float. Try Float or Double.

If not Long, then try double... Float won't work,, at least for vb6-- asthe poster quite rightly stated.

 Indi wrote: The first challenge for a programmer is to determine requirements. That means listening carefully to what kind of program is desired, and what it has to do. The original poster said he just wanted a "little program to solve... equations... (i)instead of calculating them with a paper and pen". He goes on to show some sample equations and says that he wants to solve for x. He is asking for a solver.

Firstly, I don't think the poster made it clear what he wanted. Secondly, the challenge of the programmer is not to neccessarily meet the "exact" needs of the end-users (cus they themselves do not know what exactly they want), but rather to build a robust system that meets their "general" needs.

 Indi wrote: You're trying to write an equation parser. That is an entirely different problem than an equation solver.

The poster did not specify whether he wanted solver or parser. He simply said:
 Quote: calculate equations with unknown variables

Note: equations in plural perhaps signifying he will calculate several equations, which means altering code and recompiling may not be the best solution

 Indi wrote: If the original poster wants, he can use your parser to parse the equation and then my solver to solve it. (In fact, that would be a pretty neat program. Would take an hour or two to write in C++ with Boost.Spirit. In VB? No idea.)

Indeed it would, if we assume taht ur part would be correct, and it probably is.
I dunno whats boost.spirit,, but can be done in Java in like 200 lines of coding or under, so in VB, perhaps like 300 without the use of these third party software or whatever they are.

[quote="Indi]So you are misleading the original poster by telling him that my code will not work. i have given him exactly what he asked for. He has the code to solve pretty much any equation he will ever have to deal with to find x. All he has to do is change f(), recompile and solve.
[/quote]

I am not misleading anyone, I am providing a better solution or even additional information. The poster is within his right to reject what I say, but I don't see why you should speak on behalf of him to tell him i'm misleading him. He perhaps found my contributions interesting.
Also, I didn't say your code will not work, except that it will not work if u want it for any equation with re-compilation-- which is tru

 Indi wrote: What you are giving him will not do what he wants. Your code does not solve for x, it just performs calculations using equations once you already know what x is. It could be tied to what i wrote so that he wouldn't have to recompile every time he changed the function. But by itself, your code will not solve functions.

I think I did mention about a *possible* solution in my second post and also earlier on for x. Of course, with regards to the x case, ur post may hav a slight advantage, and also after writing the code for it, a greater advantage.

so i will wrap up this topic by saying its up to the poster to decide what he wants. but im sure, he found my post fruitful even if he is not going to implement any of it. I don't see why your in a position to state that my post is irrelavent or better put, not what he wanted.
MrBlueSky
qebab wrote:
 MrBlueSky wrote: I am curious how you solve such equations using paper and pen. What do you mean by solving them?

I am guessing that what he wants to do is to write a program that divides a polynomial on another (Polynomial division), though he's not expressing himself clearly.

Is something like this what you want? Basically this method?

If that's the case he can just use symbolic manipulation. That shouldn't be to hard to implement.
Trickster
Okay I see alot of people now confused what I in the first place wanted to program, so lets clear things up.

A week ago or so, I had math in school and we was learning about integral functions. And in division equations, it is easyest first to divide the equation before you integral the equation. And by dividing I would use the same method as you would do, dividing numbers on paper:

This is ofcourse only one of the methods to divide, but thats the one I use. I was actually just wondering how to dimension x, so that it didn't return empty and thereby an error, and I can follow the method of giving x the value 1 from the beginning if it's an equation with equal sign, and I guess i could make one for solving equations in visual basic that way. However I was just wondering if it also could be done in a division equation as you see above, where the is no answer to compare x to.
I see that Indi has given some examples, but as he is writing in some different programming language I can't quite get the point, or maybe i'm just not reading carefully enough?!

I don't want this to end up a fighting contest to see which one has the best working method, because this program idea was just thought of a simple fun little programming challenge for me, to use in the math lessons instead of opening maple which a lot of people don't have. I thought it would be a simple code, but I can see that its getting very complex and therefore I don't want people to struggle anymore explaining me this coding, unless you are willing to write the code yourself in visual basic 6.0 with one textbox where you type in the division equation and one command button to press for calculating the equation and one label to show the outcome. That would by far explain everything instead of reading an explanation of how it would be done in regular words.

Thank you very much for reading and trying to explain this problem, it is appreciated to know that people are willing to help everytime I come here