Is it possible to write the following equation using vector notation in Mathematica?
dp/dt= div(k1 / k2 . grad p)
Where p is a scalar, k1 is vector, and k2 is a scalar.
You can find the vector calculus operators in the VectorAnalysis package where Laplacian (you did mean Laplacian, right?) is Laplacian and gradient is Grad. Both have some fancy symbolic replacements I belive. The default Cartesian coordinates are {Xx,Yy,Zz}, so this should give what I think you are asking for:
<< VectorAnalysis`
D[p[t, Xx, Yy, Zz], t] == Laplacian[{k1x, k1y, k1z}.Grad[p[t, Xx, Yy, Zz]]]/k2
I'm assuming k2 is a scalar? The p^(0,0,0,1) etc. int the output is Mathematica's way of denoting partial derivatives. If p is actually a defined function, they will be calculated.
HTH
Related
I am trying to solve two equations below for my project. I was unable to find any straightforward guide to solving the differential equations.
I am trying to plot a graph of h over time for the equations with a given initial h, miu, r, theta, g, L, and derivatives of h and theta wrt t. Is this possible? And if so, how?
The two equations mentioned
I tried to type the equations into Octave with the given conditions, but there seems to be an error that I am unable to identify.
Whatever is the numerical software you will use, Octave or another one (that does not formal calculation), the first step is to transform your system or N coupled Ordinary Differential Equations (ODEs) of order p into a system of p*N coupled ODEs of order 1.
This is always done by setting intermediate derivatives as new variables.
For your system, then
will become
Then, with Octave, and as explained in doc lsode you define a function say Xdot = dsys(X), that codes this system. X is the vector [h, theta, H, J], and Xdot is the returned vector of their respective derivatives, as defined by the right hand expressions of the system of first order ODEs.
So, the 2 first elements of Xdot will be trivial, just Xdot=[X(3) X(4) ...].
Of course, dsys() must also use the parameters M, g, m, µ, L, and r. As far as i understand, they can't be passed as additional arguments to dsys(). So you must defined them before calling lsode.
For initial states, you must define the vector X0=[h0, theta0, H0, J0] of known initial values.
The vector of increasing times >= 0 to which you want to compute and get the values of X must then be defined. For instance, t = 0:100. 0 must be the first element of t.
Finally, call Xt = lsode(#dsys, X0, t). After that you should get
Xt(:,1) are the values of h(t)
Xt(:,2) are the values of theta(t)
Xt(:,3) are the values of H(t)=(dh/dt)(t)
Xt(:,4) are the values of J(t)=(dtheta/dt)(t)
I wonder what / operator does in Octave. (I am not sure whether it works the same way in MATLAB)
V = [1; 2; 3]
then
1 / V = [0.071429 0.142857 0.214286]
I know that ./ operator does element-wise division of vectors or matrices.
Then what does / operator do?
This behavior is described in the documentation:
x / y
Right division. This is conceptually equivalent to the expression
(inverse (y') * x')'
but it is computed without forming the inverse of y’.
If the system is not square, or if the coefficient matrix is singular, a minimum norm solution is computed.
MATLAB has exactly the same behavior, see its documentation.
I am familiar with regular log transformations:
DF1$RT <- log(DF1$RT)
How do I perform an inverse log transformation in R?
The term inverse can be used with different meanings. The meanings are:
reciprocal. In this case the inverse of log(x) is 1/log(x)
inverse function. In this case it refers to solving the equation log(y) = x for y in which case the inverse transformation is exp(x) assuming the log is base e. (In general, the solution is b^x if the log is of base b. For example, if log10(y) = x then the inverse transformation is 10^x.)
I have two functions: one for a line (y) and another for a curve (hnc). I would like to determine the one x-value at which the two functions intersect
sigma = 0.075
mu = 0
r=0.226
theta=0.908
H=0.16
hnc <- function(x) (1/(sigma*sqrt(2*pi)))*(exp(-(x^2)/(2*(sigma^2))))
y <- function(x) 2*pi*x+(pi*r^2/((360/theta)/H))
curve(hnc,0,r,n=100,col="blue")
plot(y,0,r,add=T,col="red")
I have tried using the nleqslv package, but this results in two separate x-values that do not agree (perhaps because I am using it incorrectly)
int <- function(x){
z <- numeric(2)
z[1] <- (1/(sigma*sqrt(2*pi)))*(exp(-(x[1]^2)/(2*(sigma^2))))
z[2] <- 2*pi*x[2]+(pi*r^2/((360/theta)/H))
z}
nleqslv(c(0.14,0.14),int,method="Broyden")
Any help would be much appreciated!
Thanks,
Eric
Using optimize here to find the minimum of a function if a single variable seems to work well
xx <- optimize(function(x) abs(hnc(x)-y(x)), c(.10,.20))$minimum
abline(v=xx, lty=2)
You are not using nleqslv in the correct way. It is meant for solving a system of non linear equations with as many variables as there are equations.
You have two functions and you want to determine the intersection which in your case consists of a single value for x.
You need to define a new function like this
g <- function(x) hnc(x) - y(x)
Then you can use uniroot to find a zero of g(x) like this:
uniroot(g,c(0,1))
The root found will be 0.1417802 which corresponds with the graph in the first answer.
Minimizing won't always work to find a point of intersection; if there is no point of intersection you will get misleading results.
I want to use Maxima to do linear stability analysis as function of r:
f(x):=rx + x^3 - x^5
A:solve(f(x)=0,x)
J:jacobian([f(x)],[x])
Now for each element in A, I want to check the sign of J as a function of r. In general I want a function from r that gives that tells me if there exists any eigenvalue to J with positive real part.
Maybe you know this already, but: multiplication in Maxima is indicated by an asterisk. So you have to write:
f(x):=r*x + x^3 - x^5;
I don't see any problem with your approach so far. The Jacobian is a 1 by 1 matrix so it is trivial to compute the eigenvalue. Then substitute values of x into that, and look at the real part (function realpart).