 # How to solve differential equations in R

Im trying to solve differential equations in R but I cant a way to move it into the language.
Im trying to solve these y'=2x and y'=2y.

2 Likes

Thanks, I had to analyse the whole book again. I just didnt really understand the whole DE thing as I have just started learning it and I think that was the case. Anyway that's what I got so far and I was just wondering if thats correct, oh ant btw. is there any solution to create a flowField for the y'=2x function aswell?

``````

# Setup -------------------------------------------------------------------

library(phaseR)
library(deSolve)

# y' = 2y -----------------------------------------------------------------

# Function describing the differential equation
equation_1 <- function(x, y, parameters){
dx.dy_1 <- 2*y
list(dx.dy_1)
}

# We start from y = 0.1 to visualize how the function grows in time
y <- 0.1
# As there are no parameters we leave parms <- FALSE | By parms I mean q,p,s,k etc,
parms1 <- FALSE
# Now we specify the time in which do we want our differential equation solved
# times <- 0:100 or times <- seq(0,100,1), but we choose 5 just to make visualization clearer

# Solving the differential equation and plotting it
e1solution <- ode(y, times = 0:6, func = equation_1, parms1)
# Descriptive statistics for the solution
tail(e1solution)
summary(e1solution)
# Solution visualisation
plot(e1solution ,main = "Rozwiązanie RR y'= 2y", lwd = 2, xlab = "time (y)")

# Flow field (Error works as a warning)
equation_1.flowField <- flowField(equation_1, xlim = c(-1, 1),
ylim = c(-1, 1), parameters = NULL,
points = 31, system = "one.dim",
add = FALSE, xlab = "x", ylab = "y",
main = "Pole kierunków RR y' = 2y", col = "darkblue")+
grid(col = "black")

#  y' = 2x ----------------------------------------------------------------

equation_2 <- function(x, y, parameters){
dx.dy_2 <- 2*x
list(dx.dy_2)
}

parms2 <- FALSE

e2solution <- ode(y = x <- 0.1, times = 0:6, func = equation_2, parms2)

tail(e2solution)
summary(e2solution)

equation_2.flowField <- flowField(equation_2, xlim = c(-1, 1),
ylim = c(-1, 1), parameters = NULL,
points = 31, system = "one.dim",
add = FALSE, xlab = "x", ylab = "y",
main = "Pole kierunków RR y' = 2x")+ grid()

``````

Unless this is something you need to do repeatedly and dynamically, then I would recommend not doing it in R, but using e.g. Wolfram Alpha

Well yes and no, I’ve got an assignment to do as much with those DE until Monday and program it in R. I already know about wolfram and I used it to check if my direction poles were right. Thank you anyway. I’m still looking for a solution for the y’=2x

For y'=2x ;
while trying to code a Flow Field I'm getting this error message

``````1: In doTryCatch(return(expr), name, parentenv, handler) :
zero-length arrow is of indeterminate angle and so skipped
``````

The code used for it is:

``````equation_2 <- function(x, y, parameters){
dx.dy_2 <- 2*x
list(dx.dy_2)
}

parms2 <- FALSE

# -------------------------------------------------------------------------

# MANIPULATING 'times' IN 'e2soolution' DOESNT SIGNIFICANTLY IMPACT
# HOW THE SOLUTION IS VISUALIZED

# -------------------------------------------------------------------------

e2solution <- ode(y = 0, times = 0:100, func = equation_2, parms2)

tail(e2solution)
summary(e2solution)

plot(e2solution, main = "Rozwiązanie RR y'= 2x", lwd = 2, xlab = "time (y)")+
grid()

equation_2.flowField <- flowField(equation_2, xlim = c(-1, 1),
ylim = c(-1, 1), parameters = NULL,
points = 31, system = "one.dim",
add = FALSE, xlab = "x", ylab = "y",
main = "Pole kierunków RR y' = 2x")+ grid()
``````
1 Like

Please forgive me if I'm setting you off on a wild goose chase; it's been over 50 years since I had DE.

Take a look at the function signature for `ode` in the `deSolve::ode` help page.

`parms` is a required argument, which consists of the arguments to `func`. In your case that is `equation2`, which takes three arguments, `x, y, parameters`. But with the `parms` argument set to `FALSE`, it doesn't get them. In addition, there is no default value for the `method` argument, so you have to supply one or select one of the options, such as `euler`.

One of the greatest obstacles to learning `R` is the discipline required carefully to read the help pages, understand the required and optional arguments and work the examples.

Good luck!

1 Like

Thank you first of all for the assistance. My `parms = ` is set to FALSE as it’s converts to logical value anyway `0`, so I assumed it should be ok as I don’t have any anyway and I just wanted to create a solution for the `y’=2x`. As I checked with wolfram my solutions and slope fields were looking alraight. The only problem I had that the arrows were skipped while writing the plot but in the end I draw them using a loop function. I guess everything will be somehow acceptable hah. Thank you

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.