I was following this thread and copied the code in my project. Playing around with it turns out that it seems not to be very precise.
Recall the formula: y = ax^2 + bx +c
Since the first given point I have is at x1 = 0, we already have c=y1 . We just need to find a and b. Using:
y2 = ax2^2 + bx2 +c
y3 = ax3^2 + bx3 +c
Solving the equations for b yields:
b = y/x - ax - cx
Now setting both equations equal to each other so b falls out
y2/x2 - ax2 - cx2 = y3/x3 - ax3 - cx3
Now solving for a gives me:
a = ( x3*(y2 - c) + x2*(y3 - c) ) / ( x2*x3*(x2 - x3) )
(is that correct?!)
And then using again b = y2/x2 - ax2 - cx2 to find b. However so far I haven't found the correct a and b coeffs. What am I doing wrong?
Edit
Ok I figured out, but had to use a CAS because I don't know how to invert symbolic matrices by hand. (Gauss algo doesn't seem to work)
Writing it down in Matrix form:
| 0 0 1 | |a|
| x2^2 x2 1 | * |b| = Y
| x3^2 x3 1 | |c|
Let's call the Matrix M and multiply from the left with M^(-1)
|a|
|b| = M^(-1)*Y
|c|
Then I got out of maple:
a = (-y1 * x2 + y1 * x3 - y2 * x3 + y3 * x2) / x2 / x3 / (-x2 + x3)
Guess I did a stupid mistake somewhere above.
Which gives me the same result as the formula in the thread quoted above.
Your problem is that you have three unknowns (the coefficients a, b, and c) and only one equation that I can see: y = y1 when x = 0; this gives c = y1, as you said.
Without more information, all you can do is tell how b is related to a. That's it. There isn't one solution, there are many solutions.
If you're telling me that you have two other points (x2, y2) and (x3, y3), then you should substitute all of them into the equation and solve. Start with:
(source: equationsheet.com)
Now substitute the three points (x1, y1), (x2, y2), and (x3, y3):
(source: equationsheet.com)
This is the matrix equation that you need to invert. You can use Cramer's rule or LU decomposition. Another possibility is Wolfram Alpha:
http://www.wolframalpha.com/input/?i=inverse{{x1*x1,+x1,+1},+{x2*x2,+x2,+1},+{x3*x3,+x3,+1}}
Take the inverse that the link gives you and multiply the right hand side vector by it to solve for your three coefficients.
It's a pretty easy thing to code if you note that
det = (x2 x1^2-x3 x1^2-x2^2 x1+x3^2
x1-x2 x3^2+x2^2 x3)
Divide all the entries in the matrix by this value. The numerators are pretty simple:
(source: equationsheet.com)
Divide this by the determinant and you've got your inverse.
If you have more points than three you need to do a least squares fit. Do the same trick of substituting all the points you have (x1, y1)...(xn, yn). You'll have more equations than unknowns. Multiply both sides by the transpose of the nx3 matrix and solve. Voila - you'll have the set of coefficients that minimize the squares of errors between the points and the function values.
Related
I have this simple pytorch code:
x = torch.arange(3,dtype=float)
x.requires_grad_(True)
y = 3*x + x.sum()
y.backward(torch.ones(3))
x.grad
This gives me [6,6,6], but shouldn't it be [4,4,4] ?
Because if we have f(x)=3 * x0 + 3 * x1 + 3 * x2 + x0+x1+x2, partial derivatives would be 3+1=4 ?
The result is correct, and here is why.
I will refer to the first element of your results, and you can extend to the other elements. You want to compute dy1/dx1, but this is not the correct way. The result your code computes is dy1/dx1+ dy2/dx1 + dy3/dx1.
The ones you pass in the .backward implies that the result computed would be dot_product(ones, dy/dx). Note that dy/dx is a 3x3 matrix.
I have an expression that contains several parts. However, for simplicity, consider only the following part as MWE:
Let's assume we have the inverse of a matrix Y that I want to differentiate w.r.t. x.
Y is given as I - (x * b * t(b)), where I is the identity matrix, x is a scalar, and b is a vector.
According to The Matrix Cookbook Equ. 59, the partial derivative of an inverse is:
Normally I would use the function D from the package stats to calculate the derivatives. But that is not possible in this case, because e.g. solve to specify Y as inverse and t() is not in the table of derivatives.
What is the best workaround to circumvent this problem? Are there any other recommended packages that can handle such input?
Example that doesn't work:
f0 <- expression(solve(I - (x * b %*% t(b))))
D(f0, "x")
Example that works:
f0 <- expression(x^3)
D(f0, "x")
3 * x^2
I assume that the question is how to get an explicit expression for the derivative of the inverse of Y with respect to x. In the first section we compute it and in the second section we double check it by computing it numerically and show that the two approaches give the same result.
b and the null space of b are both eigenspaces of Y which we can readily verify by noting that Yb = (1-(b'b)x)b and if z belongs to the nullspace of b then Yz = z. This also shows that the corresponding eigenvalues are 1 - x(b'b) with multiplicity 1 and 1 with multiplicity n-1 (since the nullspace of b has that dimension).
As a result of the fact that we can expand such a matrix into the sum of each eigenvalue times the projection onto its eigenspace we can express Y as the following where bb'/b'b is the projection onto the eigenspace spanned by b and the part pre-multiplying it is the eigenvalue. The remaining terms do not involve x because they involve an eigenvalue of 1 independently of x and the nullspace of b is independent of x as well.
Y = (1-x(b'b))(bb')/(b'b) + terms not involving x
The inverse of Y is formed by taking the reciprocals of the eigenvalues so:
Yinv = 1/(1-x(b'b)) * (bb')/(b'b) + terms not involving x
and the derivative of that wrt x is:
(b'b) / (1 - x(b'b))^2 * (bb')/(b'b)
Cancelling the b'b and writing the derivative in terms of R code:
1/(1 - x*sum(b*b))^2*outer(b, b)
Double check
Using specific values for b and x we can verify it against the numeric derivative as follows:
library(numDeriv)
x <- 1
b <- 1:3
# Y inverse as a function of x
Yinv <- function(x) solve(diag(3) - x * outer(b, b))
all.equal(matrix(jacobian(Yinv, x = 1), 3),
1/(1 - x*sum(b*b))^2*outer(b, b))
## [1] TRUE
I have a linear scale that ranges form 0.1 to 10 with increments of change at 0.1:
|----------[]----------|
0.1 5.0 10
However, the output really needs to be:
|----------[]----------|
0.1 1.0 10 (logarithmic scale)
I'm trying to figure out the formula needed to convert the 5 (for example) to 1.0.
Consequently, if the dial was shifted halfway between 1.0 and 10 (real value on linear scale being 7.5), what would the resulting logarithmic value be? Been thinking about this for hours, but I have not worked with this type of math in quite a few years, so I am really lost. I understand the basic concept of log10X = 10y, but that's pretty much it.
The psuedo-value of 5.0 would become 10 (or 101) while the psuedo-value of 10 would be 1010. So how to figure the pseudo-value and resulting logarithmic value of, let's say, the 7.5?
Let me know if addition information is needed.
Thanks for any help provided; this has beaten me.
Notation
As is the convention both in mathematics and programming, the "log" function is taken to be base-e. The "exp" function is the exponential function. Remember that these functions are inverses we take the functions as:
exp : ℝ → ℝ+, and
log : ℝ+ → ℝ.
Solution
You're just solving a simple equation here:
y = a exp bx
Solve for a and b passing through the points x=0.1, y=0.1 and x=10, y=10.
Observe that the ratio y1/y2 is given by:
y1/y2 = (a exp bx1) / (a exp bx2) = exp b(x1-x2)
Which allows you to solve for b
b = log (y1/y2) / (x1-x2)
The rest is easy.
b = log (10 / 0.1) / (10 - 0.1) = 20/99 log 10 ≈ 0.46516870565536284
a = y1 / exp bx1 ≈ 0.09545484566618341
More About Notation
In your career you will find people who use the convention that the log function uses base e, base 10, and even base 2. This does not mean that anybody is right or wrong. It is simply a notational convention and everybody is free to use the notational convention that they prefer.
The convention in both mathematics and computer programming is to use base e logarithm, and using base e simplifies notation in this case, which is why I chose it. It is not the same as the convention used by calculators such as the one provided by Google and your TI-84, but then again, calculators are for engineers, and engineers use different notation than mathematicians and programmers.
The following programming languages include a base-e log function in the standard library.
C log() (and C++, by inclusion)
Java Math.log()
JavaScript Math.log()
Python math.log() (including Numpy)
Fortran log()
C#, Math.Log()
R
Maxima (strictly speaking a CAS, not a language)
Scheme's log
Lisp's log
In fact, I cannot think of a single programming language where log() is anything other than the base-e logarithm. I'm sure such a programming language exists.
I realize this answer is six years too late, but it might help someone else.
Given a linear scale whose values range from x0 to x1, and a logarithmic scale whose values range from y0 to y1, the mapping between x and y (in either direction) is given by the relationship shown in equation 1:
x - x0 log(y) - log(y0)
------- = ----------------- (1)
x1 - x0 log(y1) - log(y0)
where,
x0 < x1
{ x | x0 <= x <= x1 }
y0 < y1
{ y | y0 <= y <= y1 }
y1/y0 != 1 ; i.e., log(y1) - log(y0) != 0
y0, y1, y != 0
EXAMPLE 1
The values on the linear x-axis range from 10 to 12, and the values on the logarithmic y-axis range from 300 to 3000. Given y=1000, what is x?
Rearranging equation 1 to solve for 'x' yields,
log(y) - log(y0)
x = (x1 - x0) * ----------------- + x0
log(y1) - log(y0)
log(1000) - log(300)
= (12 - 10) * -------------------- + 10
log(3000) - log(300)
≈ 11
EXAMPLE 2
Given the values in your question, the values on the linear x-axis range from 0.1 to 10, and the values on the logarithmic y-axis range from 0.1 to 10, and the log base is 10. Given x=7.5, what is y?
Rearranging equation 1 to solve for 'y' yields,
x - x0
log(y) = ------- * (log(y1) - log(y0)) + log(y0)
x1 - x0
/ x - x0 \
y = 10^| ------- * (log(y1) - log(y0)) + log(y0) |
\ x1 - x0 /
/ 7.5 - 0.1 \
= 10^| --------- * (log(10) - log(0.1)) + log(0.1) |
\ 10 - 0.1 /
/ 7.5 - 0.1 \
= 10^| --------- * (1 - (-1)) + (-1) |
\ 10 - 0.1 /
≈ 3.13
:: EDIT (11 Oct 2020) ::
For what it's worth, the number base 'n' can be any real-valued positive number. The examples above use logarithm base 10, but the logarithm base could be 2, 13, e, pi, etc. Here's a spreadsheet I created that performs the calculations for any real-valued positive number base. The "solution" cells are colored yellow and have thick borders. In these figures, I picked at random the logarithm base n=13—i.e., z = log13(y).
Figure 1. Spreadsheet values.
Figure 2. Spreadsheet formulas.
Figure 3. Mapping of X and Y values.
I'm new to R and I need to plot the quadratic matrix equation:
x^T A x + b^T x + c = 0
in R^2, with A being a 2x2, b a 2x1, and c a constant. The equation is for a boundary that defines classes of points. I need to plot that boundary for x0 = -6...6, x1 = -4...6. My first thought was generate a bunch of points and see where they are zero, but it depends on the increment between the numbers (most likely I'm not going guess what points are zero).
Is there a better way than just generating a bunch of points and seeing where it is zero or multiplying it out? Any help would be much appreciated,
Thank you.
Assuming you have a symmetric matrix A,
eg
# A = | a b/2 |
# | b/2 c |
and your equation represents a conic section, you can use the conics package
What you need is a vector of coefficients c(a,b,c,d,e,f) representing
a.x^2 + b*x*y + c*y^2 + d*x + e*y + f
In your case, say you have
A <- matrix(c(2,1,1,2))
B <- c(-20,-28)
C <- 10
# create the vector
v <- append(c(diag(A),B,C),A[lower.tri(A)]*2), 1)
conicPlot(v)
You could easily wrap the multiplication out into a simple function
# note this does no checking for symmetry or validity of arguments
expand.conic <- function(A, B, C){
append(c(diag(A),B,C),A[lower.tri(A)]*2), 1)
}
I have differential equations derived from epidemic spreading. I want to obtain the numerical solutions. Here's the equations,
t is a independent variable and ranges from [0,100].
The initial value is
y1 = 0.99; y2 = 0.01; y3 = 0;
At first, I planned to deal these with ode45 function in matlab, however, I don't know how to express the series and the combination. So I'm asking for help here.
**
The problem is how to express the right side of the equations as the odefun, which is a parameter in the ode45 function.
**
Matlab has functions to calculate binomial coefficients (number of combinations) and the finite series can be expressed just as matrix multiplication. I'll demonstrate how that works for the sum in the first equation. Note the use of the element-wise "dotted" forms of the arithmetic operators.
Calculate a row vector coefs with the constant coefficients in the sum as:
octave-3.0.0:33> a = 0:20;
octave-3.0.0:34> coefs = log2(a * 0.05 + 1) .* bincoeff(20, a);
The variables get combined into another vector:
octave-3.0.0:35> y1 = 0.99;
octave-3.0.0:36> y2 = 0.01;
octave-3.0.0:37> z = (y2 .^ a) .* ((1 - y2) .^ a) .* (y1 .^ a);
And the sum is then just evaluated as the inner product:
octave-3.0.0:38> coefs * z'
The other sums are similar.
function demo(a_in)
X = [0;0;0];
T = [0:.1:100];
a = a_in; % for nested scope
[Xout, Tout ]= ode45( #myFunc, T, X );
function [dxdt] = myFunc( t, x )
% nested function accesses "a"
dxdt = 0*x + a;
% Todo: real value of dxdt.
end
end
What about this, and you simply need to fill in the dxdt from your math above? It remains to be seen if the numerical roundoff matters...
Edit: there's a serious issue due to the 1=y1+y2+y3 constraint. Is that even allowed, since you have an IVP with 3 initial values given and 3 first order ODE's? If that constraint is a natural consequence of the equations, it may not be needed.