How to evaluate a derivative at a given point in Maxima - math

I need to generate in the Maxima software the expression f'(e) - 1, or using other notation, (df/dx)(e) - 1. I'm doing it calculating the derivative of f(x) and then making a substitution:
subst(%e, x, diff(f(x), x)) - 1
However, I get the following result:
Does any body know how could I get the correct expression?

Use at instead of subst:
at(diff(f(x),x), [x=%e]);
Execution yields the desired result.
See the documentation here.

Related

Generating my own arbitrary polynomials in Julia

I have a problem where I need to generate my own monomial sequence of N terms and evaluate it numerically - a polynomial basis, mind you.
What I need is a function that I can generate. The problem is that (as far as I know) I can't iterate over a function I have already defined in order to keep adding terms of my own devising (the monomials), which is what I want. The only way I know how to add the terms that I want is manually, but that won't do.
Basically, I want something like...
N=4
#Code runs, and then I get something like the following:
f(x) = x^0 + c1*x + c2*x^2 + c3*x^3 + c4*x^4
And I want it to be a function that can be evaluated or differentiated via other packages.
Note: I want to operate on the coefficients in order to see if I can solve a problem where I assume the polynomial is the solution that I then have to optimize.
Yes, I know the package Polynomials.jl can tackle the aforementioned example quite readily and cleanly, but what I want is to be able to define each of the terms that I keep adding to the polynomial sequence myself, make them a trigonimetric function, an exponential, etc. Maybe I want to define the Fourier series, or whatever. I want to build my own polynomial base that I can then evaluate.
Like this:
N=4
#Code runs, and then I get the following:
f(x) = x^0 + c1*cos(x) + c2*sin(x) + c3*exp(3*x) + c4*x^4
I tried with Symbolics.jl, played with Polynomials.jl, tried to get it done with varargs, and nothing worked. I am genuinely stumped. Any help would be greatly appreciated, even if just to tell me "Go learn metaprogramming," or something like that.
Maybe this can help or at least gives you an insight:
julia> function myf(x, coefs)
myExpressions = [x^0, cos(x), sin(x), exp(3*x), x^4]
#assert length(coefs) == length(myExpressions) "The length of coefficients doesn't match the length of the expressions."
return coefs*myExpressions
end
julia> myf(1, [1 2 3 4 5])
1-element Vector{Float64}:
89.94716525891064
julia> myf(1, [1 2 3 4])
ERROR: AssertionError: The length of coefficients doesn't match the length of the expressions.

Simplify the division of Normals cumulatives functions

I'm struggling on how I can simplify the quotient of two normal probability functions in R. Actually, I'm calculating a conditional skew-Normal density, them I have the division between this two function:
pnorm(alpha0+t(alpha2)%*%chol2inv(chol(omega2))%*%t(y2-xi2.1))/pnorm(tau2.1)
where alpha0+t(alpha2)%*%chol2inv(chol(omega2))%*%t(y2-xi2.1) and tau2.1 result in real numbers. For example, sometimes I have pnorm(-50)/pnorm(-40), e.g. an inconsistency 0/0. But these values are not zero, R is just approximating. I tried to use the erf function, but I got the same problem (0/0).
Any hint on how can I overcome this issue?
pnorm has a log parameter, which makes it return log(p). Change your equation to exp(log(p1) - log(p2)):
exp(pnorm(-50, log = TRUE) - pnorm(-40, log = TRUE))
#[1] 2.95577e-196

Using uniroot in R with integrate

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.

How to increase precision of solution of nlm-solver

Given is a function F1:
F1 <- function(C1,C2,C3,...,x,u_target) {
# a lot of equations follow
...
u_actual - u_target
}
F1 returns the result of the very last equation
u_actual - u_target
I want to determine the value for the parameter x in a way that the result of the last equation converges to zero. With
nlm(f=F1,p=c(0),C1=C1,C2=C2,...,stepmax=0.001,ndigit=8)
I get a result, but not a satisfying one:
u_actual = 0.1316566
u_target = 0.1
I played a lot with the arguments of the nlm command (gradtol,stepmax,iterlim etc.), but I was not able to get a better result. I also tried optim, optimize and uniroot, but was not able to get them run at all.
u and x show a negative exponential development. With decreasing x, u increases exponential. If x is zero, u results in a finite value. x also has an upper boundary, which is unknown. So I guessed it would be promising if the iteration starts at the lower boundary (zero) and increases step by step. However, whether I decrease or increase the value of stepmax, the result is not getting better.
I would appreciate any hint from the r-community.
Thank you very much.
PS: in matlab a colleague uses fsolve(#(x) F1(x,u_target,C1,C2,...),0), and it works fine.

How to have sigma of something while plotting?

I am using plotFun function of mosaic package in R. I am trying to plot a 3D plot. Following is my code snippet:
plotFun(2*l*(w/log(beta*kspill*lambda^2+(1+(w/x-w)/10)*(lambda^2*5+lambda^1*5+1))+(w/x-w)/log((1+((w/x-w)/10)^1)*(lambda^2*5+lambda^1*5+1))) ~ x & lambda ,
x.lim=range(0.8,1), lambda.lim=range(1,3),
l=60,w=40,kspill=10,f=1,beta=0.5,surface=TRUE)
This is working fine. Now suppose I want to fix lambda and introduce a new variable t such that if t=2 we get lambda^2*5+lambda^1*5+1 as in the above case. If t=3 we get lambda^3*5+lambda^2*5+lambda^1*5+1 and so on. So now I have t.lim=range(1,3) for a fixed lambda :
plotFun(2*l*(w/log(beta*kspill*lambda^2+(1+(w/x-w)/10)*("depends on t"))+(w/x-w)/log((1+((w/x-w)/10)^1)*("depends on t"))) ~ x & lambda ,
x.lim=range(0.8,1), t.lim=range(0.5,1),
l=60,w=40,kspill=10,f=1,beta=0.5,lambda=1,surface=TRUE)
What to write in the "depends on t" part above. I guess we can't put a for loop there to calculate 5* {summation i=0 to i=t}lambdai. How to go about doing this?
You can define your "depends on t" with
depends_on_t <- makeFun(5 * sum(lambda^(1:round(t))) + 1 ~ t + lambda, lambda = 1)
But you still have some issues to resolve:
1) Your plotFun() command is creating a plot using x and lambda, but I'm guessing you meant x and t.
2) t can only be an integer if you are going to use it in a sum of the type you are suggesting. But you are creating a plot that assumes continuous variables for the axes. I inserted round(t) as one way to move from real values to integer values before computing the sum. The makes the function work for non-integer values, but it might not be what you really want.
Finally, some additional suggestions:
3) x & lambda should be replaced with x + lambda. The use of & here dates back to the very early days of the mosaic package, and although it is still supported (I think, we don't really test for it anymore), we prefer x + lambda.
4) I recommend separating out the definition of your function from the plotFun() command. You can use mosaic::makeFun() as illustrated above, or the usual function() to define your function and whatever default values you want for its arguments. Then you can do sanity checks on the function, or use it in multiple plots rather than including all of the function's definition in each plot.
5) Using spaces and returns would make your code much more readable. (As would simplifying your example to a minimal example that demonstrates the issue you are asking about.)
6) I think you might want
depends_on_t <- makeFun(5 * sum(lambda^(0:round(t))) ~ t + lambda, lambda = 1)
rather than the formula as you describe it, but without more context, I can't really know.

Resources