Good time of day! I'm working on creating an Eulers Method Calculator program. The problem I got stuck on is I don't know how to convert the user's string into a Differential Equation (dy/dx).

This is what I have so far:

import static java.lang.System.*;
import java.util.Scanner;

class EulersMethod {

   public static void main(String args[]) {
      double xcor, ycor, delx, dely, xcorplusdelx, ycorplusdely, fx;
      String dydx;

      Scanner input = new Scanner(System.in);
        out.print(" Enter  x coordinate          ");                                        
        xcor = input.nextDouble();
        out.print(" Enter  y coordinate          ");                                        
        ycor = input.nextDouble();
        out.print(" Enter  delta x               ");                                        
        delx = input.nextDouble();
        out.print(" Enter dy/dx                  ");                                        
        dydx = input.next();
      out.print(" Enter x coordinate at which" +'\n'+ " you want to find the y value ");                                        
        fx = input.nextDouble();

         while (xcor != fx)            
   }
}

Fot the dydx user input, the user can input different Differential Equations, such as (x, y, x+y, 2x+y,...) and so on. Thus I dont know how to convert those inputs into a calculations within the code. What advice could you give me in order to resolve my issue?

when the user inputs for example 2x+y, you then want it to be 2*xcor(enteredvalue)+ ycor(enteredvalue) or do you want to apply integrals to this equation since differetiantion is all about integrals. If you want to do the first thing then i would suggest to see check if the equation contains x and replace it with your xcor double variable, same for y.

Yes, I want it to be 2*xcor(enteredvalue)+ ycor(enteredvalue), but I don't know how to convert the users input into the code like you have above. What would the code be instead of the (enteredvalue)?

That s not really an easy task.You could somehow make a an array of characters with the tochararray method and then implement a for loop to go through each cell and check where x,y,numbers and math operators are and somehow accumulate this in a integer variable in c# there is a class for that and you can do it much easier for Java i suggest to take this code snippet from this Link:Click Here
let s say that you want squares and square roots, logarithms etc... then this would be fairly complicated to achieve but only using basic operators i think that this link should sort that out for you.

For the sake of minimizing the complexity to parse the dydx, lets use a predefined formula of "x + y" aka

dydx = (xcor+ycor);

The code bellow gives the output, but only finds the first (x, y) coordinates. I tried the while loop, but the program doesn't work properly to give the output when the fx matches with xcor, instead goes on non-stop executing outputs.

import static java.lang.System.*;
import java.util.Scanner;

class EulersMethod {

   public static void main(String args[]) {
      double xcor, ycor, delx, dely, xcorplusdelx, ycorplusdely, fx, newxcor, newycor;
      double dydx;

      Scanner input = new Scanner(System.in);
        out.print(" Enter  x coordinate          ");                                        
        xcor = input.nextDouble();
        out.print(" Enter  y coordinate          ");                                        
        ycor = input.nextDouble();
        out.print(" Enter  delta x               ");                                        
        delx = input.nextDouble();
        //out.print(" Enter dy/dx                  ");                                      
        //dydx = input.next();
      out.print(" Enter x coordinate at which" + '\n' + " you want to find the y value ");                                      
        fx = input.nextDouble();

         //while (xcor != fx)  { 
            dydx = (xcor+ycor);
            dely = (dydx*delx);
            xcor = (xcor+delx);
            ycor = (ycor+dely);
            out.println("(" + xcor + ", " + ycor + ")");            
   }
}

How can this problem get resolved?

I was able to fix the issue by replacing:

while (xcor != fx)

with:

while (xcor <= fx)

however, this only works with a positive delx. When I tried a negative delx, I had to use:

while (xcor >= fx)

I however get lost on behalf of how to make which code to work. When the user inputs positive or negative delx. To be exact I don't know how to interpret the users input to determine weather there is a negative sign before the number or not, and accordingly set a proper code into action. If anyone could give me any idea, I will try my best to make this work properly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.