I was just looking for some feedback for my c# program to run Euler's method. I was just wondering on any possible improvements, refactoring, inefficiencies... the usual stuff you probably deal with here.
I am using YAMP, a math parser that can evaluate string inputs (so I can run things like calculating the derivative), and is capable of assignment operators (so I can replace x= with its actual value). Here is my code:
class Program
{
static void Main(string[] args)
{
string derivativeinput;
string[] xyvals;
int xval;
int yval;
int numxval;
int stepby;
int stepto;
derivativeinput = GetDerivative();
xyvals = GetInitVals();
Parser.Load();
Console.WriteLine("Estimate what value of x?");
stepto = int.Parse(Console.ReadLine());
Console.WriteLine("Step by=?");
stepby = int.Parse(Console.ReadLine());
yval = Int32.Parse(xyvals[1]);
xval = Int32.Parse(xyvals[0]);
LoadInitVals(xval, yval);
for (int i = 1; i <= stepto; i+= stepby)
{
int derivative = Int32.Parse(Parser.Parse(derivativeinput).Execute().ToString());
yval = yval + derivative * stepby;
Parser.Parse("y =" + yval.ToString()).Execute();
xval = xval + stepby;
Parser.Parse("x = " + xval.ToString()).Execute();
}
Console.WriteLine(yval);
Console.ReadKey();
}
static string GetDerivative()
{
Console.WriteLine("Dy/Dx=");
return Console.ReadLine();
}
static string[] GetInitVals()
{
Console.WriteLine("Initial value: X,Y");
var initval = Console.ReadLine();
return initval.Split(',');
}
static void LoadInitVals(int initx, int inity)
{
Parser.Parse("x = " + initx.ToString()).Execute();
Parser.Parse("y =" + inity).Execute();
}
}
I realize there probably isn't much to evaluate (the parser does a lot of the work). I guess some of the things I'm interested in is handling input, converting (I believe this parser will only evaluate and output a string at the moment), and my methods (for instance, GetDerivative- do I return a string, like I am? Or do I go ahead and set the 'derivative' string to the derivative in the function).
Anything else is welcome as well.