"Reverse" the x(integer) giving y=0 of a trigonometric graph - math

Hello
The graph for y=sin(x*pi/5)*2*sin(x*pi4) looks like this:
Some y=0 are 4,5,8,10,12,15,16
Is there a general technique (imagine the equation having more components) - that can give us a new equation where the integers that satisfy y(x)=0 will be an arbitrary nonzero number, and the integers with a nonzero numbers will be set to zero?
In python I can easily find the nonzero with this code:
#for 100 first integers
for i < 100
if y != 0:
print x
1 += 1
But I wonder if this can be done mathematically - preferably as a trigonometric function.

Related

two dimensional identifier that is circular?

i need an identifier value that is circular when lerping to the next one. Example:
0,1,2,3
When lerping form any id to the next one in the row, we always get a value between both ids. Except for 3: when lerping to the next id (back to zero) it will be lerped through all ids.
So what i am searching for is something two dimesional for example:
(0,0), (0,1), (1,1), (1,0)
When learping from (1,0) to (0,0) it will lerp fine.
How do I warp this in a function or loop to convert for examle 0,1,2,3,4 to two-dimensional ids?
So you are looking for a function that takes a number k from 0 to N-1 and returns a point on a circle? That's what the trigonometric functions sine and cosine do by definition.
You would transform the number k into the point (x, y) where:
x = cos(k*360°/N)
y = sin(k*360°/N)
(Note that most software libraries take the input in radians and not degrees, if that's your case you would replace 360 degrees with 2*pi, or use a built-in "toRadians" function)

Differentiating a scalar with respect to matrix

I have a scalar function which is obtained by iterative calculations. I wish to differentiate(find the directional derivative) of the values with respect to a matrix elementwise. How should I employ the finite difference approximation in this case. Does diff or gradient help in this case. Note that I only want numerical derivatives.
The typical code that I would work on is:
n=4;
for i=1:n
for x(i)=-2:0.04:4;
for y(i)=-2:0.04:4;
A(:,:,i)=[sin(x(i)), cos(y(i));2sin(x(i)),sin(x(i)+y(i)).^2];
B(:,:,i)=[sin(x(i)), cos(x(i));3sin(y(i)),cos(x(i))];
R(:,:,i)=horzcat(A(:,:,i),B(:,:,i));
L(i)=det(B(:,:,i)'*A(:,:,i)B)(:,:,i));
%how to find gradient of L with respect to x(i), y(i)
grad_L=tr((diff(L)/diff(R)')*(gradient(R))
endfor;
endfor;
endfor;
I know that the last part for grad_L would syntax error saying the dimensions don't match. How do I proceed to solve this. Note that gradient or directional derivative of a scalar functionf of a matrix variable X is given by nabla(f)=trace((partial f/patial(x_{ij})*X_dot where x_{ij} denotes elements of matrix and X_dot denotes gradient of the matrix X
Both your code and explanation are very confusing. You're using an iteration of n = 4, but you don't do anything with your inputs or outputs, and you overwrite everything. So I will ignore the n aspect for now since you don't seem to be making any use of it. Furthermore you have many syntactical mistakes which look more like maths or pseudocode, rather than any attempt to write valid Matlab / Octave.
But, essentially, you seem to be asking, "I have a function which for each (x,y) coordinate on a 2D grid, it calculates a scalar output L(x,y)", where the calculation leading to L involves multiplying two matrices and then getting their determinant. Here's how to produce such an array L:
X = -2 : 0.04 : 4;
Y = -2 : 0.04 : 4;
X_indices = 1 : length(X);
Y_indices = 1 : length(Y);
for Ind_x = X_indices
for Ind_y = Y_indices
x = X(Ind_x); y = Y(Ind_y);
A = [sin(x), cos(y); 2 * sin(x), sin(x+y)^2];
B = [sin(x), cos(x); 3 * sin(y), cos(x) ];
L(Ind_x, Ind_y) = det (B.' * A * B);
end
end
You then want to obtain the gradient of L, which, of course, is a vector output. Now, to obtain this, ignoring the maths you mentioned for a second, if you're basically trying to use the gradient function correctly, then you just use it directly onto L, and specify the grid X Y used for it to specify the spacings between the different elements in L, and collect its output as a two-element array, so that you capture both the x and y vector-components of the gradient:
[gLx, gLy] = gradient(L, X, Y);

Rotation matrix, normalization, determinant -1

I'm currently implementing an algorithm for 3D pointcloud filtering following a scientific paper.
I run in some problems when computing the rotation matrix for specific values. The goal is to rotate points into the coordinatesystem which is defined by the direction of the normal vector ( Z Axis). Since the following query is rotationally symmetric in X,Y axis, the orientation of these axis does not matter.
R is defined as follows: Rotationmatrix
[1 1 -(nx+ny)/nz]
R = [ (row1 x row3)' ]
[nx ny nz ]
n is normalized. The problem occures when n_z becomes really small or zero. Therefore i considered to normalize row 1 before computing the crossproduct for row 2.
Nevertheless the determinant becomes -1. Will the rotationmatrix sill lead to correct results? R is orthogonal but det|R| not +1
thanks for any suggestions
You always get that
det(a, a×b, b) = - det( a, b, a×b)
= - dot(a×b, a×b)
is always negative. Thus you need to change the second row by negating it or by re-arranging the overall order of the rows.
Are you interested in rotating points around arbitrary axis? If yes, maybe quaternions is good solution.
You can check this if you want to transform a quaternion to matrix before you actually use it.

How to solve symbolic function like this in 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)

Drawing a symbolic function either in R or Matlab

I want to draw this equation either in Matlab or R (Matlab is preferred) :
f = p+(1-p)*(T-S)
where 0 < S < 1, 0 < p < 1 and T is a constant. I want to draw the function and find the min, max based on S and p. My basic problem is defining the span of the graph as a symbol. Since S changes from 0 to T.
Use ezsurf to plot. For example:
f = 'p + (1 - p) * (5 - S)'
ezsurf(f, [0 5 0 1])
Then use regular calculus to find critical values, double differentiate to find their type, and so on...
This is all explained in the online documentation (diff, solve, etc.). Also, this external example covers all of the points you want very nicely: http://msemac.redwoods.edu/~darnold/math50c/matlab/maxmin/index.xhtml

Resources