0

We have the following mathematical formulas to solve differential equations by Euler's method

xn+1 = xn + h

yn+1 = yn + h*f(xn, yn)

Suppose we have been provided y(x0)=any value,then we have x0 and y0 and also h has been provided by the user.

I am having problem to understand how can I accept the function f(xn, yn) from the user since the function can be either algebraic,trigonometric,exponential or logarithmic and the function would be of type dy/dx=(expression).The program code should be able to solve any differential expression entered by the user and the answers of the approximations must be correct upto 4 decimal places.

It is possible to accept the expression as a String but I would not be able to perform calculations on String.Any suggestions or solutions would be appreciated.

Site of Euler's method:http://calculuslab.deltacollege.edu/ODE/7-C-1/7-C-1-h-c.html

Example Input:

expression input:

dy/dx=x+2y

initial conditions input:

x0=0, y0=0

enter step size: h=0.25

enter the value for which you want to find solution for: x=1

Example output:

output

  • 2
    Reading mathematical functions from input and then having them evaluated is not trivial. You should probably try to find a library to do that. – Qubit Sep 25 '18 at 07:52
  • 2
    So you want an expression evaluator? There is no such thing in standard C. You can write one or find some source code on the web, but it's a rather challenging thing for a beginner. – Jabberwocky Sep 25 '18 at 07:53
  • 2
    Please try to clarify by giving examples of input and output. How do several typical inputs look like? Your question as it is sounds like you refer to a string with the math expression of the function (in that case see above comments). But maybe there is an implemented function like `double inputfunction(double x)`? and you could take a pointer to it.... – Yunnosch Sep 25 '18 at 08:07
  • You will have to take the input as a string, otherwise there would be no way to preserve (e.g. `sin(3*x)`, etc..). As mentioned parsing expressions/equations is not trivial, but can be done. Have a look at [How to parse a formula from a string?](https://stackoverflow.com/questions/28450479/how-to-parse-a-formula-from-a-string) – David C. Rankin Sep 25 '18 at 08:17
  • And if you go to the length of using an expression parser/evaluator, you might want to use the classical Runge-Kutta 4th order method. With Euler you need step size h=1e-5 or smaller to reach your precision goal, with RK4 a step size h=0.01 should be sufficient. This materially reduces the number of function (parse tree) evaluations and thus the execution time. – Lutz Lehmann Sep 25 '18 at 11:33
  • "how can I accept the function ...either algebraic,trigonometric,exponential or logarithmic" --> than ask the user if "algebraic,trigonometric,exponential or logarithmic" is desired, then ask for co-efficients. – chux - Reinstate Monica Sep 25 '18 at 13:33

0 Answers0