Let’s start exploring the MathStudio Script Language with this first tutorial. In this article we are going to code a simple second grade equation solver.

Some days ago we released the beta of our Mathematical IDE, MathStudio, which allows you to develop your technical applications in a simple way.

Actually, MathStudio has a simple command that implements the Brent’s equation solver, but we won’t use that in this tutorial. We will let the user input the coefficients of the second grade equation one by one. To do that in MathStudio, we use the input command:

output Insert 'a' coefficient:
[a] = input
output Insert 'b' coefficient:
[b] = input
output Insert 'c' coefficient:
= input

We now have all the coefficients declared. According to Maths theory, we have to calculate the delta (discriminant). Delta is defined as ∆=b^2-4*a*c, so:

[delta] = calc ({b})^2-4*({a})*({c})

Now, according to the algorithm to solve this kind of equations, we have two results: x = (-b±√∆)/2*a, so:

//First result
[x1] = calc (-({b}) + sqrt({delta}))/2*({a})
//Second result
[x2] = calc (-({b}) - sqrt({delta}))/2*({a})

All we have to do now is to printing the results on the screen:

output First result: {x1}
output Second result: {x2}
pause

In order to prevent the script from returning NaN (Not a Number) for both the results if delta is minor than zero, we can put a goto mark before the pause command:

if {delta} < 0 then

output [Delta < 0] Cannot calculate solutions.
goto end

endif

[…]

:end
pause

You can also plot the function graph associated to the equation if you want:

fdef f(x) = ({a})*x^2+({b})*x+({c})
plot "f"

This sample is included in the GoManga MathStudio installer and you can find it under C:\GoManga\MathStudio\Samples\equations.ms but you can also download from here.

Have fun!