I'm trying to input a second order differential equation to solve into matlab over x = 0 to x =1. I can't figure out how. Here's the equation:
y'' = 1 + 0.1 \sqrt{1+(y')^2}
with initial conditions at zero.
Normally you solve higher-order equations by converting to a system of first order equations. Here, you would define:
y' = v
v' = 1 + 0.1 \sqrt{1 + v^2}
Define a function computing the right-hand side, and use ode45.
Note that this equation is solvable without much trouble in closed form, too, so should be a good test for how to do it.
Related
So I want to ask whether there's any way to define and solve a system of differential equations in R using matrix notation.
I know usually you do something like
lotka-volterra <- function(t,a,b,c,d,x,y){
dx <- ax + bxy
dy <- dxy - cy
return(list(c(dx,dy)))
}
But I want to do
lotka-volterra <- function(t,M,v,x){
dx <- x * M%*% x + v * x
return(list(dx))
}
where x is a vector of length 2, M is a 2*2 matrix and v is a vector of length 2. I.e. I want to define the system of differential equations using matrix/vector notation.
This is important because my system is significantly more complex, and I don't want to define 11 different differential equations with 100+ parameters rather than 1 differential equation with 1 matrix of interaction parameters and 1 vector of growth parameters.
I can define the function as above, but when it comes to using ode function from deSolve, there is an expectation of parms which should be passed as a named vector of parameters, which of course does not accept non-scalar values.
Is this at all possible in R with deSolve, or another package? If not I'll look into perhaps using MATLAB or Python, though I don't know how it's done in either of those languages either at present.
Many thanks,
H
With my low reputation (points), I apologize for posting this as an answer which supposedly should be just a comment. Going back, have you tried this link? In addition, in an attempt to find an alternative solution to your problem, have you tried MANOPT, a toolbox of MATLAB? It's actually open source just like R. I encountered MANOPT on a paper whose problem boils down to solving a system of ODEs involving purely matrices.
My problem is one which should be quite common in statistical inference:
min{(P - k)'S(P - k)} subject to k >= 0
So my choice variable is k, a 3x1 vector. The 3x1 vector P and 3x3 matrix S are known. Is it possible to reformulate this problem so I can use R's solve.QP quadratic programming solver? This solver requires the problem to be in the form
min{-d'b + 0.5 b' D b} subject to A'b >= b_0.
So here the choice vector is is b. Is there a way I can make my problem fit into solve.QP? Thanks so much for any help.
I am using maple (version 12) for the first time.
I want to invert the equations using any maths tool. I tried it using MAPLE.
I have given two equations in this form:
a=x*b^r + y*(b^s)*(c^t) + z*c^u
Now I want to flip this equation (invert this equation)
I want two equations for a and b in terms of c
e.g
a= 0.98989c^2 + 78c + 87
b= 56c + 89
These are not my results. I just gave an example.
Any advice?
My two equations are:
Equation#1 is:
a= 1.093*(b^0.002939) - 0.1887*(b^0.7637)*(c^0.2306) - 0.04425 *(c^0.9143)
Equation#2 is:
a= 1.088*(b^0.003058) -0.1813*(b^0.7627)*(c^0.2501) -0.03958 *(c^0.9365)
From Equation#1, i can find b equation. I put the bequation in Equation#2 . In this way equation#2 has only a and c variables. i can get a equation. and then by putting this equation can get b equation ( with only c variables)
But these equations are very complex and it takes a lot of time to evaluate in MAPLE. How to simplify it? I tried to use natural log, but it doesnot give any answer. It just repeat my equation in command window.
ln(1.093*b^0.2939e-2-.1887*b^.7637*c^.2306-0.4425e-1*c^.9143)
Please guide me.
Need an algorithm to solve equation like these:
8*p^2+8*q^2+8*p-16=0
p^2+q^2+12*p+20=0
If anyone can point me to the name of algorithm also it'll be enough. I could have followed any matrix related algorithm but the quadratic inside these linear are causing a problem.
The Nelder-Mead Algorithm is a very easy-to-program non-linear solver that converges quickly and accurately in low dimensional problems like this one. The pseudo-code is easy to follow on Wikipedia, but you can find some coded implementations online as well.
Your cost function in this case would be the sum of the left hand sides' squares to ensure that lower costs are closer to the correct solutions.
In the case you wrote about, I'd suggest first subtracting 8 times the second equation from the first.
0 = (8*p^2+8*q^2+8*p-16) - 8*(p^2+q^2+12*p+20) = -88*p-176 = 0
p = -2
Then you are left with a simple quadratic equation in q, which you can solve using the common methods for solving quadratic equations in a single variable.
It's a non-linear problem. You'll need an iterative solution, something like Newton Raphson or BFGS.
Here's a simple approach.
Start by linearizing it:
16*p*dp + 16*q*dq + 8 = 0
2*p*dp + 2*q*dq + 12 = 0
This becomes a linear algebra problem:
| 16*p 16*q |[ dp ] [ -8 ]
| 2*p 2*q |[ dq ] = [ -12 ]
You'll start with an initial guess for (p, q):
(p, q) = (p0, q0)
You'll solve for dp and dq, using the usual linear algebra techniques.
You'll update your guesses for p and q:
(p1, q1) = (p0 + dp, q0 + dq)
You'll iterate again until your solution either diverges or converges.
There's no guarantee that it'll converge, depending on your initial guess.
I am trying to calculate point on a line.
I got the points of the edges and one distance between one edge to the point I want to find (which is B).
A(2,4)
B(x,y)
C(4,32)
The distance between A to B is 5.
How can I calculate Bx and By? using the following equations:
d = Math.Sqr((Bx-Ax)^2 + (By-Ay)^2)
d = Math.Sqr((Cx-Bx)^2 + (Cy-By)^2)
and than compare the equations above.
Here is the equations with the points placed:
5 = Math.Sqr((Bx-2)^2 + (By-4)^2)
23.0713366 = Math.Sqr((4-Bx)^2 + (32-By)^2)
or
Math.Sqr((Bx-2)^2 + (By-4)^2) - 5 = Math.Sqr((4-Bx)^2 + (32-By)^2) - 23.0713377
How can I solve this using VBA?
Thank you!
I won't solve your equations above because they are an unnecessarily complex way to state the problem (and the existence of a solution is questionable in the presence of rounding), but all the points on the line A=(Ax,Ay) to C=(Cx,Cy) can be described as B=(Ax,Ay) + t*(Cx-Ax,Cy-Ay) with t between 0 and 1.
The distance between B and A is then given by d=t*Sqrt((Cx-Ax)^2+(Cy-Ay)^2), which you can invert to get the proper t for a given d - t=d/Sqrt((Cx-Ax)^2+(Cy-Ay)^2)
In your case, B(t) = (2,4) + t*(2,28), t=5/Sqrt(2^2+28^2) ~ 0.178 -> B ~ (2,4) + 0.178 * (2,28) ~ (2.356, 8.987).
VBA has no Symbolic Language capability. To solve this problem, there are different approach :
Transform the equations to isolate one of the unknowns, most likely to use substitution, and compute it (I recommend this for your problem.)
Transform your functions and derive them to use Newton's methods (don't do this, it's overkill.)
Use a "brute force" convergence methods : Fix a min/max for each variable and use bisection methods to find what you want (I don't recommend this because you'll most likely "fall" into a local minimum/maximum in your case.)
So basically, I'd say you go with the first way. It requires 15mins of tinkering with mathematical equations, then you're set to go.