Hello I would like some help on how to use some functions in R.
I have to find the value of a parameter that satisfies my equation. My equation is an integral. So I have to find a way to combine those two functions. The value of a must be less than 5
INTEGRAND<- function(x,a){exp(-x*a)*(1-exp(-sqrt(a)) }
INTEGRAL <- function(a){(integrate(INTEGRAND,lower=0, upper=Inf))$value - 1}
root<- uniroot(INTEGRAL,interval=c(0.01,4.99))
But to calculate the integral we need a value for 'a' but the is is what I am searching for.
Related
I am trying to translate an Excel Solver problem (which solves correctly) into R code. I am aware that there is an array of packages allowing to solve constrained optimisation problems, but I still have not found one being able to formulate my problem and solve it.
The problem is the following
min sum(x)
s.t.:
g(f(x)) > 0.9
where:
x is a vector of boolean parameters to be identified
f(x) = x+4
g(f(x)) = SUMIF(f(x), "<90", M)/1000
M is a vector of constant terms with length equal to the lenght of the parameters of X.
In words, the problem aims at minimising the objective function sum(x). Here, x is the vector of boolean parameters the value of which must be determined, f(x) is a function which sums 4 to each parameter, and g(f(x)) is the conditional (to the value of each corresponding f(x)) sum of the vector of constant terms M.
I struggle to set up the problem with every constrained optimisation R package I have found. Is this problem solvable in R in the first place? If so, how?
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.
I am trying to minimize a quadratic objective with quadratic constraints, is Rolnp the way to go? I have been reading the documentation and it seems like all the examples use equations instead of vector manipulation. All of my parameters are vectors and matrices and they can be quite large. Here is my problem:
X<-([Cf]+[H])%*%[A]
Y<-([Cf]+[H]-[R])%*%[B]
I want to find H that minimizes Y%*%Dmat%*%t(Y) for a given value of X%*%Dmat%*%t(X)
Cf, R, A, Dmat and B are matrices of constants.
The values for H sohould be between 0 and 1.
Is it possible to use Rsolnp to find the vector H even though the input functions will all return other vectors?
I ended up using AUGLAG and COBYLA to solve this problem.
I am trying to determine the weights of 9 metrics which will return the highest accuracy ratio. Since they are weights, the values need to sum to 1 and lie between 0 & 1. I am currently using the optim function, but do to constraints, I think I need to switch to constrOptim. I was wondering the best way to do this. Below I have included the code i am currently using. x.matrix is 20,000 by 9 matrix of values ranked between 1-10.
pars<-c(w1=(1/9),w2=(1/9),w3=(1/9),w4=(1/9),w5=(1/9),w6=(1/9),w7=(1/9),w8=(1/9),w9=(1/9))
OptPars<-function(pars){(-(rcorr.cens(x.matrix%*%pars),f)["Dxy"])}
opt<-optim(pars,OptPars)
Say you have values x on the range (-Inf, Inf) and you need values p in the range [0,1] that sum to 1, you can do the following transformation
p <- exp(x)/sum(exp(x))
If you do that translation in your optimization function and do the same transformation on the best set of parameters, you should get what you want.
I'd like some help please I really need to solve this problem.
Well before anything thank you for your time...
My problem: I have a matrix (826x826 double) and I want to integrate this matrix with respect to a vector of (826x1 double) I don't have the functions of any of this. Is there a command or an algorithm to take the integral of a matrix with respect to a vector? Please I really need help, I'm such a newbie at matlab.
Sincerely.
George
If it's a constant matrix A integrated with respect to vector x, your answer in simply Ax + c where c is some constant vector. If A is a function of x, you will need to specify exactly what it is. Another case is when both A and x are functions of t. There is no one simple answer and no computer program would do it in most cases. There are books written in this stuff. It's not an easy task.
If I understand correctly, you have a matrix Y (size mxn) and a vector X (size mx1) where Y(i, j) = f_j(X(i)) for some unknown function f_j. To approximate the integral of each column over X you could use the trapz function of Matlab which uses the trapezoidal method.
A = trapz(X, Y);
This will integrate Y along its columns using the vector X. If you wanted to integrate along rows you can call the trapz function with an added argument of dim=2. Of course, the dimensions of X and Y must be compatible in either case.