Skip to content

Commit

Permalink
r-invert-polynomial
Browse files Browse the repository at this point in the history
  • Loading branch information
edwbaker committed May 18, 2024
1 parent 22fc47c commit 85ae64c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions notes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
- [Linux audio recipes](/notes/linux-audio-recipes)
- [Acoustics figures](/notes/acoustics-figures)

## R

- [Invert a polynomial in R](/notes/r-invert-polynomial.md)

## Mathematical visualisations

- [Collatz Conjecture](/notes/collatz-conjecture)
23 changes: 23 additions & 0 deletions notes/r-invert-polynomial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Inverting a polynomial using R

R code to numerically find the inverse of a polynomial.

```r
# Define a polynomial function to invert
pn <- function(x) {
return(3*x^3 + 2*x^2)
}

# Generic invert() function
invert = function(fn, interval = NULL, ...){
Vectorize(function(y){
uniroot(function(x){fn(x)-y}, interval, ...)$root
})
}

# Inversion function specific to pn()
pn.inverse <- invert(pn, interval = c(-10, 10))

# Find the inverse for a specific value
pn.inverse(4)
```

0 comments on commit 85ae64c

Please sign in to comment.