How to solve symbolic function like this in R - r

This should be simple for those with experience.
I want to solve an equation using R. I know you can solve
different linear/quadratic equations using Solve().
But I have something like this:
1/20 = 1/8 * (1/(12+x)) + 1/4*(1/(40+x)) + 3/4*(1/(50+x))
How can I solve x in this case? It can't be done by hand.
It gotta be some numeric methods involved to solve this like in TI83.
Is there a simple and quick way to do this in R without writing lines of codes?
Thank you!

As you say, there are indeed roots. First thing is to plot the function:
f <- function(x) {1/20 - 1/8 * (1/(12+x)) + 1/4*(1/(40+x)) + 3/4*(1/(50+x))}
x <- seq(-100,100)
par(mar=c(2,2,1,2)) # this just minimizes plot margins
plot(x,f(x), type="l")
abline(0,0,col="blue",lty=2)
So obviously, f(x) does cross 0, several times.
Next step is to estimate the crossings. One way to do this is to look for changes in sign:
x <- seq(-75,0,0.001)
y <- sign(f(x)) # vector of +1 or -1
plus.to.minus <- which(diff(y)<0) # diff(y)<0 when f crosses from (+) to (-)
minus.to.plus <- which(diff(y)>0) # diff(y)>0 when f crosses from (-) to (+)
# first two roots are (+) to (-); third is (-) to (+)
lower <- c(plus.to.minus[1:2],minus.to.plus[3])
roots <- sapply(lower,function(i)uniroot(f,interval=c(x[i],x[i+1]))$root)
lapply(roots,function(x) points(roots,c(0,0,0),col="red",pch=16))
roots
# [1] -67.38961 -41.72593 -10.38446
This code attempts to find x where f(x) changes sign. There are actually two reasons that f(x) could change sign: a root, or an asymptote. In your case there are three roots, and three asymptotes. Success here depends on having a small enough increment in x so that you don't completely miss a crossing. Based in the graph above it looks like 0.001 is small enough.
Here, y is a vector which contains the sign of f (as +1 or -1) at x between -75 and 0, in increments of 0.001. The limits (-75,0) were chosen by inspecting the plot above. We can see visually that there are three roots. The first two cross from (+) to (-), and the third crosses from (-) to (+). So we identify the index of x where the crossings occur (using which(...)), and then create a vector that contains the first two elements of plus.to.minus and the third element of minus.to.plus. Then we call uniroot(...) using increment=c(x[i],x[i+1]) where i is the index of the appropriate crossing.
Finally, we plot the results to confirm that we have in fact found the roots. This is really important - always, always plot the results. It turns out that uniroot(...) will find a "root" where there is an asymptote, so you have to make sure you've found actual roots.

Use uniroot() to solve equations in one variable:
f <- function(x){
1/8 * (1/(12+x)) + 1/4*(1/(40+x)) + 3/4*(1/(50+x)) - 1/20
}
uniroot(f, interval = c(-1e+08, 1e+08))
Notice that in the function, f, I subtract 1/20. This is because uniroot() finds the zero of the function.
In this case, you will get the error:
Error in uniroot(f, interval = c(-1e+08, 1e+08)) :
f() values at end points not of opposite sign
To correct this, you need to make sure the zero exists and if it does, move the interval, (a, b) so that f(a) == -f(b)

Related

Plot of function, DomainError. Exponentiation yielding a complex result requires a complex argument

Background
I read here that newton method fails on function x^(1/3) when it's inital step is 1. I am tring to test it in julia jupyter notebook.
I want to print a plot of function x^(1/3)
then I want to run code
f = x->x^(1/3)
D(f) = x->ForwardDiff.derivative(f, float(x))
x = find_zero((f, D(f)),1, Roots.Newton(),verbose=true)
Problem:
How to print chart of function x^(1/3) in range eg.(-1,1)
I tried
f = x->x^(1/3)
plot(f,-1,1)
I got
I changed code to
f = x->(x+0im)^(1/3)
plot(f,-1,1)
I got
I want my plot to look like a plot of x^(1/3) in google
However I can not print more than a half of it
That's because x^(1/3) does not always return a real (as in numbers) result or the real cube root of x. For negative numbers, the exponentiation function with some powers like (1/3 or 1.254 and I suppose all non-integers) will return a Complex. For type-stability requirements in Julia, this operation applied to a negative Real gives a DomainError. This behavior is also noted in Frequently Asked Questions section of Julia manual.
julia> (-1)^(1/3)
ERROR: DomainError with -1.0:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
julia> Complex(-1)^(1/3)
0.5 + 0.8660254037844386im
Note that The behavior of returning a complex number for exponentiation of negative values is not really different than, say, MATLAB's behavior
>>> (-1)^(1/3)
ans =
0.5000 + 0.8660i
What you want, however, is to plot the real cube root.
You can go with
plot(x -> x < 0 ? -(-x)^(1//3) : x^(1//3), -1, 1)
to enforce real cube root or use the built-in cbrt function for that instead.
plot(cbrt, -1, 1)
It also has an alias ∛.
plot(∛, -1, 1)
F(x) is an odd function, you just use [0 1] as input variable.
The plot on [-1 0] is deducted as follow
The code is below
import numpy as np
import matplotlib.pyplot as plt
# Function f
f = lambda x: x**(1/3)
fig, ax = plt.subplots()
x1 = np.linspace(0, 1, num = 100)
x2 = np.linspace(-1, 0, num = 100)
ax.plot(x1, f(x1))
ax.plot(x2, -f(x1[::-1]))
ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')
plt.show()
Plot
That Google plot makes no sense to me. For x > 0 it's ok, but for negative values of x the correct result is complex, and the Google plot appears to be showing the negative of the absolute value, which is strange.
Below you can see the output from Matlab, which is less fussy about types than Julia. As you can see it does not agree with your plot.
From the plot you can see that positive x values give a real-valued answer, while negative x give a complex-valued answer. The reason Julia errors for negative inputs, is that they are very concerned with type stability. Having the output type of a function depend on the input value would cause a type instability, which harms performance. This is less of a concern for Matlab or Python, etc.
If you want a plot similar the above in Julia, you can define your function like this:
f = x -> sign(x) * abs(complex(x)^(1/3))
Edit: Actually, a better and faster version is
f = x -> sign(x) * abs(x)^(1/3)
Yeah, it looks awkward, but that's because you want a really strange plot, which imho makes no sense for the function x^(1/3).

Returning all the x coordinates where the graph cuts a y coordinate

I've got the first line down which is defining the function:
f <- function(x) 3034*log(x)+2305.84*log(1-x)-1517*log(1-x)
Now the problem I'm having is I need to find all the x values where
f(x)=-1947.92 but I've got no idea what the command is to do this?
Normally I would say you should use uniroot(), after modifying the function to return zero at the target, but that will be problematic here:
target <- -1947.92
f <- function(x) 3034*log(x)+2305.84*log(1-x)-1517*log(1-x)
g <- function(x) f(x)-target
uniroot(g,interval=c(1e-4,1-1e-4))
## Error in uniroot(g, interval = c(1e-04, 1 - 1e-04)) :
## f() values at end points not of opposite sign
What's going on is that your curve crosses zero in two places. uniroot() requires that you bracket the root:
Let's take a look:
curve(g(x))
abline(h=0,col=2)
Zoom in:
curve(g(x),from=0.75,to=0.85)
abline(h=0,col=2)
Now we can either just eyeball this (i.e. use interval=c(1e-4,0.8) or interval=c(0.8,1-1e-4) depending on which root we're interested in) or find
opt1 <- optim(g,par=0.5,method="L-BFGS-B",lower=1e-4,upper=1-1e-4,
control=list(fnscale=-1)) ## maximize rather than min
then use opt1$par as your cut-point. (Or you could do some simple calculus: the maximum [point where the derivative wrt x is zero] is much easier to compute than the roots ...)
Alternatively, you could ask Wolfram Alpha ...

MAPLE: Take the positive solution and plotting

I have these four equations:
eq1:= 1.6*10^(-7)*R*sin(t)-4.4*10^(-14)*R^2*cos(t)*sin(t)-1.6*10^(-14)*R^2*cos(t)^2+4.2*10^(-14)*R^2-1.3+2.1*10^(-9)*R*cos(t)=0;
eq2 := 8.3*10^(-8)*R*sin(t)-1.2*10^(-13)*R^2*cos(t)*sin(t)-2.9*10^(-44)*R^2*cos(t)^2+7.1*10^(-14)*R^2-1.3+8.3*10^(-8)*R*cos(t)=0;
eq3 := 8.3*10^(-8)*R*sin(t)-1.2*10^(-13)*R^2*cos(t)*sin(t)-2.2*10^(-44)*R^2*cos(t)^2+7.1*10^(-14)*R^2-1.3+8.3*10^(-8)*R*cos(t)=0;
eq4 := 2.1*10^(-9)*R*sin(t)-4.4*10^(-14)*R^2*cos(t)*sin(t)+1.6*10^(-14)*R^2*cos(t)^2+2.6*10^(-14)*R^2-1.3+1.6*10^(-7)*R*cos(t)=0;
I want to solve each equation for R, each obviously yielding two roots and I'm always assured that one root is above the horizontal axis while the other is below it, and pick only the four non-negative roots without doing it manually i.e. for instance, write something like res:=solve(eq1,R), plotting each one as a function of t and only then taking the positive root. I want the code to do it.
After obtaining the positive roots, say {r1,r2,r3,r4}, I want to plot on the same figure, the following 4 graphs
plot([r1*cos(t),r1*sin(t),t=0..2*Pi]);
plot([r2*cos(t),r2*sin(t),t=0..2*Pi]);
plot([r3*cos(t),r3*sin(t),t=0..2*Pi]);
plot([r4*cos(t),r4*sin(t),t=0..2*Pi]);
Finally, I need to outline the intersection area with some color and shade it.
I would appreciate your help.
Here is how to pick the positive root and how to iterate through your equations. From here I think you should be able to make the plots yourself. Just beware that your roots are "heavy" expressions.
restart:
with(plots):
# Number of equations
n := 4;
# Empty solution vector
solutions := Vector(n):
# List of equations
eq := [
1.6*10^(-7)*R*sin(t)-4.4*10^(-14)*R^2*cos(t)*sin(t)-1.6*10^(-14)*R^2*cos(t)^2+4.2*10^(-14)*R^2-1.3+2.1*10^(-9)*R*cos(t)=0 ,
8.3*10^(-8)*R*sin(t)-1.2*10^(-13)*R^2*cos(t)*sin(t)-2.9*10^(-44)*R^2*cos(t)^2+7.1*10^(-14)*R^2-1.3+8.3*10^(-8)*R*cos(t)=0 ,
8.3*10^(-8)*R*sin(t)-1.2*10^(-13)*R^2*cos(t)*sin(t)-2.2*10^(-44)*R^2*cos(t)^2+7.1*10^(-14)*R^2-1.3+8.3*10^(-8)*R*cos(t)=0 ,
2.1*10^(-9)*R*sin(t)-4.4*10^(-14)*R^2*cos(t)*sin(t)+1.6*10^(-14)*R^2*cos(t)^2+2.6*10^(-14)*R^2-1.3+1.6*10^(-7)*R*cos(t)=0
]:
# Iterate through all equations
for i from 1 to n do:
# Find and store positive root
solutions[i] := solve(eq[i], R, useassumptions) assuming R>0:
end do:
solutions;

Remove redundant points for line plot

I am trying to plot large amounts of points using some library. The points are ordered by time and their values can be considered unpredictable.
My problem at the moment is that the sheer number of points makes the library take too long to render. Many of the points are redundant (that is - they are "on" the same line as defined by a function y = ax + b). Is there a way to detect and remove redundant points in order to speed rendering ?
Thank you for your time.
The following is a variation on the Ramer-Douglas-Peucker algorithm for 1.5d graphs:
Compute the line equation between first and last point
Check all other points to find what is the most distant from the line
If the worst point is below the tolerance you want then output a single segment
Otherwise call recursively considering two sub-arrays, using the worst point as splitter
In python this could be
def simplify(pts, eps):
if len(pts) < 3:
return pts
x0, y0 = pts[0]
x1, y1 = pts[-1]
m = float(y1 - y0) / float(x1 - x0)
q = y0 - m*x0
worst_err = -1
worst_index = -1
for i in xrange(1, len(pts) - 1):
x, y = pts[i]
err = abs(m*x + q - y)
if err > worst_err:
worst_err = err
worst_index = i
if worst_err < eps:
return [(x0, y0), (x1, y1)]
else:
first = simplify(pts[:worst_index+1], eps)
second = simplify(pts[worst_index:], eps)
return first + second[1:]
print simplify([(0,0), (10,10), (20,20), (30,30), (50,0)], 0.1)
The output is [(0, 0), (30, 30), (50, 0)].
About python syntax for arrays that may be non obvious:
x[a:b] is the part of array from index a up to index b (excluded)
x[n:] is the array made using elements of x from index n to the end
x[:n] is the array made using first n elements of x
a+b when a and b are arrays means concatenation
x[-1] is the last element of an array
An example of the results of running this implementation on a graph with 100,000 points with increasing values of eps can be seen here.
I came across this question after I had this very idea. Skip redundant points on plots. I believe I came up with a far better and simpler solution and I'm happy to share as my first proposed solution on SO. I've coded it and it works well for me. It also takes into account the screen scale. There may be 100 points in value between those plot points, but if the user has a chart sized small, they won't see them.
So, iterating through your data/plot loop, before you draw/add your next data point, look at the next value ahead and calculate the change in screen scale (or value, but I think screen scale for the above-mentioned reason is better). Now do the same for the next value ahead (getting these values is just a matter of peeking ahead in your array/collection/list/etc adding the for next step increment (probably 1/2) to the current for value whilst in the loop). If the 2 values are the same (or perhaps very minor change, per your own preference), you can skip this one point in your chart by simply adding 'continue' in the loop, skipping adding the data point as the point lies exactly on the slope between the point before and after it.
Using this method, I reduce a chart from 963 points to 427 for example, with absolutely zero visual change.
I think you might need to perhaps read this a couple of times to understand, but it's far simpler than the other best solution mentioned here, much lighter weight, and has zero visual effect on your plot.
I would probably apply a "least squares" algorithm to obtain a line of best fit. You can then go through your points and downfilter consecutive points that lie close to the line. You only need to plot the outliers, and the points that take the curve back to the line of best fit.
Edit: You may not need to employ "least squares"; if your input is expected to hover around "y=ax+b" as you say, then that's already your line of best fit and you can just use that. :)

This is more a matlab/math brain teaser than a question

Here is the setup. No assumptions for the values I am using.
n=2; % dimension of vectors x and (square) matrix P
r=2; % number of x vectors and P matrices
x1 = [3;5]
x2 = [9;6]
x = cat(2,x1,x2)
P1 = [6,11;15,-1]
P2 = [2,21;-2,3]
P(:,1)=P1(:)
P(:,2)=P2(:)
modePr = [-.4;16]
TransPr=[5.9,0.1;20.2,-4.8]
pred_modePr = TransPr'*modePr
MixPr = TransPr.*(modePr*(pred_modePr.^(-1))')
x0 = x*MixPr
Then it was time to apply the following formula to get myP
, where μij is MixPr. I used this code to get it:
myP=zeros(n*n,r);
Ptables(:,:,1)=P1;
Ptables(:,:,2)=P2;
for j=1:r
for i = 1:r;
temp = MixPr(i,j)*(Ptables(:,:,i) + ...
(x(:,i)-x0(:,j))*(x(:,i)-x0(:,j))');
myP(:,j)= myP(:,j) + temp(:);
end
end
Some brilliant guy proposed this formula as another way to produce myP
for j=1:r
xk1=x(:,j); PP=xk1*xk1'; PP0(:,j)=PP(:);
xk1=x0(:,j); PP=xk1*xk1'; PP1(:,j)=PP(:);
end
myP = (P+PP0)*MixPr-PP1
I tried to formulate the equality between the two methods and seems to be this one. To make things easier, I skipped the summation of matrix P in both methods .
where the first part denotes the formula that I used, and the second comes from his code snippet. Do you think this is an obvious equality? If yes, ignore all the above and just try to explain why. I could only start from the LHS, and after some algebra I think I proved it equals to the RHS. However I can't see how did he (or she) think of it in the first place.
Using E for expectation, the one dimensional version of your formula is the familiar:
Variance(X) = E((X-E(X))^2) = E(X^2) - E(X)^2
While the second form might be easier programming, I'd worry about ending up with a negative (or, in the multidimensional case, non positive definite) answer by using it, due to rounding error.

Resources