Equation solving in terms of a parameter in R - r

I have the following equation I'd need to solve for η:
where the parameter θ is unknown (the general problem is to estimate it), and the vector h depends on that parameter, and a vector of known values y.
What ideally I would like to get is the vector η=η(θ) as a function of the parameter θ so that I can easily use it later.

Related

Optimisation tool in R to find the input parameter of function that minimises output value?

I wish to find an optimisation tool in R that lets me determine the value of an input parameter (say, a specific value between 0.001 and 0.1) that results in my function producing a desired output value.
My function takes an input parameter and computes a value. I want this output value to exactly match a predetermined number, so the function outputs the absolute of the difference between these two values; when they are identical, the output of the function is zero.
I've tried optimize(), but it seems to be set up to minimise the input parameter, not the output value. I also tried uniroot(), but it produces the error f() values at end points not of opposite sign, suggesting that it doesn't like the fact that increasing/decreasing the input parameter reduces the output value up to a point, but going beyond that point then increases it again.
Apologies if I'm missing something obvious here—I'm completely new to optimising functions.
Indeed you are missing something obvious:-) It's very obvious how you should/could formulate your problem.
Assuming the function that must equal a desired output value is f.
Define a function g satisfying
g <- function(x) f(x) - output_value
Now you can use uniroot to find a zero of g. But you must provide endpoints that satisfy the requirements of uniroot. I.e. the value of g for one endpoint must be positive and the value of g for the other endpoint must be negative (or the other way around).
Example:
f <- function(x) x - 10
g <- function(x) f(x) - 8
then
uniroot(g,c(0,20))
will do what you want but
uniroot(g,c(0,2))
will issue the error message values at end points not of opposite sign.
You could also use an optimization function but then you want to minimize the function g. To set you straight: optimize does not minimize the input paramater. Read the help thoroughly.

Error Solving Definite Integral in Maple

Basically I am trying to solve the following Definite Integral in Maple from Theta=0 to Theta0=45. I am trying to find an actual numerical value but need to find the integral first. I don't know how to ask Maple to help me solve an integral where there are two different values (theta and theta0) within. All I am trying to do is find the period of oscillation of a pendulum but I have been instructed to only use this method and equation.
From the equation d^2θ/dt^2= -g/L sin(θ) we find:
P = 4 sqrt(L/2g) ∫ (0 to θ0) dθ/sqrt[cos(θ)-cos(θ0)]
L= 1
g= 9.8
To simplify the value before the integral I did the following:
>L:=1;
>g:=9.8;
>evalf(4*sqrt(L/(2*g));
>M:=%;
So the integral to solve simplifies to:
P = M ∫ (0 to θ0) dθ/sqrt[cos(θ)-cos(θ0)]
When I try to evaluate the integral by itself I get the error:
"Error, index must evaluate to a name when indexing a module".
I am trying to figure out how Maple wants me to enter in the integral so it will solve it.
I have tried the following as well as similar combinations of variables:
int(1/sqrt[cos(t)-cos(45)],t=0..45);
I can't conceive how to make maple solve the definite integral for me given it is cos(theta)-cos(theta0) in the denominator instead of just one variable. When I try different values for the integral I also get the following error:
Error, index must evaluate to a name when indexing a module
I must be overlooking something considerable to continue getting this error. Thanks in advance for any help or direction! :)
As acer noted in his comment, maple syntax doesn't use square brackets for functions. The proper syntax for your task is:
int(1/sqrt(cos(t)-cos(Pi/4)),t=0..Pi/4);
Notice that maple works in radians, so I replaced your 45 with Pi/4.
If you need a numerical value you can use evalf:
evalf(int(1/sqrt(cos(t)-cos(Pi/4)),t=0..Pi/4));
maple's answer is 2.310196615.
If you need to evaluate with a generic variable theta0, you can define a function as:
myint:=theta0->int(1/sqrt(cos(t)-cos(theta0)),t=0..theta0);
Then just call it as, e.g.,
myint(Pi/4);
and for a numerical evaluation:
evalf(myint(Pi/4));

Error in Minimisation using the nlminb R-function

I would like to estimate the 4-parameters that minimises the log-likelihood function LogL(\theta). The first two parameters (a and sigma) are positive; the third one is unconstrained and the last one should be leas than the minimum value in the data. So, I tried to use nlminb function and write it as:
nlminb(start=c(a=0.13,mu=1,sigma=31,xi=0.01),f,lower=c(0.0001,0,0.123,0.01210),
upper=c(2,18.21,50,0.95),control=list(eval.max=100, iter.max=100))
I got a good result, but there is still problem that the estimator of mu take the same value of the upper limit for all the values I try to use and for xi, it take the same lower value even If I change the starting values.
The log likelihood function take form:
-loglik=-n*log(a)+n*log(1-exp(-1))+n*log(sigma) -(a-1)*sum(log(x-mu))+(1/xi+1)*sum(log(1+xi*((x-mu)**a)/sigma))+ sum((1+xi*((x-mu)**a)/sigma)*(-1/xi))
I have another condition that x-mu should be positive and 1+xi*((x-mu)*a)/sigma as well
Any suggestion will be appreciated

Determinant of a complex matrix in R

Is there a way to calculate the determinant of a complex matrix?
F4<-matrix(c(1,1,1,1,1,1i,-1,-1i,1,-1,1,-1,1,-1i,-1,1i),nrow=4)
det(F4)
Error in determinant.matrix(x, logarithm = TRUE, ...) :
determinant not currently defined for complex matrices
library(Matrix)
determinant(Matrix(F4))
Error in Matrix(F4) :
complex matrices not yet implemented in Matrix package
Error in determinant(Matrix(F4)) :
error in evaluating the argument 'x' in selecting a method for function 'determinant'
If you use prod(eigen(F4)$values)
I'd recommend
prod(eigen(F4, only.values=TRUE)$values)
instead.
Note that the qr() is advocated to use iff you are only interested in the
absolute value or rather Mod() :
prod(abs(Re(diag(qr(x)$qr))))
gives the Mod(determinant(x))
{In X = QR, |det(Q)|=1 and the diagonal of R is real (in R at least).}
BTW: Did you note the caveat
Often, computing the determinant is
not what you should be doing
to solve a given problem.
on the help(determinant) page ?
If you know that the characteristic polynomial of a matrix A splits into linear factors, then det(A) is the product of the eigenvalues of A, and you can use eigen value functions like this to work around your problem. I suspect you'll still want something better, but this might be a start.

How to plot implicit equations

What is the usual method or algorithm used to plot implicit equations of 2 variables?
I am talking about equations such as,
sin(x*y)*y = 20
x*x - y*y = 1
Etc.
Does anyone know how Maple or Matlab do this? My target language is C#.
Many thanks!
One way to do this is to sample the function on a regular, 2D grid. Then you can run an algorithm like marching squares on the resulting 2D grid to draw iso-contours.
In a related question, someone also linked to the gnuplot source code. It's fairly complex, but might be worth going through. You can find it here: http://www.gnuplot.info/
Iterate the value of x across the range you want to plot. For each fixed value of x, solve the equation numerically using a method such as interval bisection or the Newton-Raphson method (for which you can calculate the derivative using implicit differentiation, or perhaps differentiate numerically). This will give you the corresponding value of y for a given x. In most cases, you won't need too many iterations to get a very precise result, and it's very efficient anyway.
Note that you will need to transform the equation into the form f(x) = 0, though this is always trivial. The nice thing about this method is that it works just as well the other way round (i.e. taking a fixed range of y and computing x per value).
There're multiple methods. The easiest algorithm I could find is descripted here:
https://homepages.warwick.ac.uk/staff/David.Tall/pdfs/dot1986b-implicit-fns.pdf and describes what Noldorin has described you.
The most complex one, and seems to be the one that can actually solve a lot of special cases is described here:
https://academic.oup.com/comjnl/article/33/5/402/480353
i think,
in matlab you give array as input for x.
then for every x, it calculates y.
then draws line from x0,y0 to x1, y1
then draws line from x1,y1 to x2, y2
...
...

Resources