solving equations with yac in the Ryacas library

How can I solve equation for a variable z? I tried this

library(Ryacas)
yac("Solve((0.07-0.07exp(-z)-0.06508080z) == 0, z)")

I got the wrong answer. The correct answer z=0.148

A obvious answer is z = 0, since then you would have 0.07 - 0.07 \cdot e^0 - 0.0650808 \cdot 0 = 0.07 - 0.07 - 0 = 0.

If you want to solve such equations in R you could rely on the base function uniroot, which takes the intervall to search in for a root as well as the function as arguments and returns the x values for which your function is equal to zero, e.g. f(x) = 0:

func <- function(x) 0.07 - 0.07 * exp(-x) - 0.0650808 * x
# find the obvious answer by including 0 in the interval
uniroot(func, lower = 0, upper = 1)
#> $root
#> [1] 0
#> 
#> $f.root
#> [1] 0
#> 
#> $iter
#> [1] 0
#> 
#> $init.it
#> [1] NA
#> 
#> $estim.prec
#> [1] 0

# find the second root by adjusting the interval
uniroot(func, lower = 0.01, upper = 1)
#> $root
#> [1] 0.1475151
#> 
#> $f.root
#> [1] 1.406651e-07
#> 
#> $iter
#> [1] 9
#> 
#> $init.it
#> [1] NA
#> 
#> $estim.prec
#> [1] 6.103516e-05

Created on 2022-09-01 by the reprex package (v2.0.1)

As an additional hint: You have to explicitly use operators like "*" to tell R what it has to do. E.g. in your above code 0.07exp(-z) is not separated by "*", so R tries to find e.g. a function called 0.07exp which you try to pass an argument, -z, into it (and can obviously not find a suitable function because it wasn't defined).

I hope this clarifies some of your issues regarding R and function solving in R.

Kind regards

1 Like

yacas' newton solver finds your value

yac("Newton((0.07-0.07*Exp(-z)-0.06508080*z),z,0.1,0.0001)")
 "0.147545136027390784"

Its nice to throw equations at wolframAlpha to verify etc.
solve - Wolfram|Alpha (wolframalpha.com)

1 Like

Thank you for your help.

Thank you for your help.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.