I'm trying to solve a system of recursive equations. For this, the value V(m) is defined to be the solution to an equation like V(m)==g(V(m)), where g is a known function. How can I define a function in that way?
To be specific, consider the system:
In Julia I have:
T=4;
beta=.95;
alpha=.3;
gamma=.3;
zeta=.3;
V(m) = m<T ? alpha+beta*V(m+1)+beta*(Z(m+1,1)+Z(m+1,2)+Z(m+1,3)+Z(m+1,4)) : 0.
U(f) = f<T ? gamma+beta*U(f+1)+beta*(Z(1,f+1)+Z(2,f+1)+Z(3,f+1)+Z(4,f+1)) : 0.
Z(m,f) = m<T && f<T ? zeta+beta*(Z(m+1,f+1)+V(m+1)+U(f+1)) : 0.
Notice that when computing V(2) for example, the rhs also contains V(2). So V(2) is the solution to that equation. How can I compute the values of V, U and Z in a setting like this?
Related
Take the following combination of constraints, for example:
var('x y', domain='real')
constraint1 = x^2 + y^2 == 1
constraint2 = x==y OR x==-y
solve([constraint1, constraint2],x,y)
This of course does not work, because OR is invalid syntax. Is it possible to mark disjoint constraints as input to sage's solve function?
I need to solve the following nonlinear equation to get the value of (sd.1.est).
(k)and (R.bar) are known values calculated from a previous step. this is my code:
library(nleqslv)
k=0.7642437
R.bar=0.4419803
sd=1.109488
fun <- function(sd.1.est){
(-(k^2)/(2*(sd.1.est^2)))+log((k/sd.1.est)+
(((k/sd.1.est)^3)*factorial(3)/((factorial(1))^2*(factorial(2))*(2^3)))+
(((k/sd.1.est)^5)*factorial(5)/((factorial(2))^2*(factorial(3))*(2^6)))+
(((k/sd.1.est)^7)*factorial(7)/((factorial(3))^2*(factorial(4))*(2^9))))-log(4*R.bar/sqrt(2*pi))}
ss1=nleqslv(sd,fun,method="Broyden",global="qline",control=list(cndtol=10^-12,maxit=1000,allowSingular =TRUE))
I need to put a constraint on sd.1.est to be positive ( as it is an estimate for the scale parameter).
I do not know how to write this.
We cannot run your code since you have not provided any data.
In other words your code is not reproducible.
There is a way to get yourself a positive value for sd.1.est by a change of variable.
Write the header of your function as follows:
fun <- function(z) {
sd.1.est <- z*z
........
}
Here z is an intermediate variable. By squaring z and assigning the result to sd.1.est you will always get a positive value for sd.1.est.
In the call to nleqslv the initial value for z should be set to the sqrt of your initial value sd.
Warning: this may not work.
I found out about how to increase or decrease a pixel's contrast, and my teacher answered me I could use this formula : R = r*coefficient ; G = g*coefficient ; B = b*coefficient where R, G and B are the contrasted pixel, and r, g, b its old values. The coefficient is a positive or negative percentage choosed by the final-user of my retouching program.
But this formula doesn't work. I searched on the Internet why and I found I must change this formula using for example the sigmoid function.
So my question is : is the formula I wrote below correct ? I don't think so because I did some tests : it doesn't work and I don't understand why.
R = 1/(1 + Math.exp(-(r + coefficient*r)))
NB : it would be fine if I could restrain the images of my function to : [0-255] and the abscissas to [0 - 255] but I don't know how.
I am trying to evaluate a function in Scilab using the following steps:
x=poly(0,'x')
y=(x^18+x^11)^3 // function (the function is variable)
y1=derivat(y) // first derivate
y2=derivat(y) //second derivate
y3=derivat(y) //third derivate
I need evaluate the 3 derivatives in any point.
I know the function: evstr(expression) but it does not work with the return value of the derivative.
I try to use: string(y) but it returns something strange.
How can to do it, to cast the return of derivat to string to evaluate with evstr or how can I evaluate the n-th derivative in any point using Scilab.
To evaluate numerical derivatives of almost any kind of function (of one or sereval variables) up to machine precision (you won't get better results if you evaluate symbolic expressions obtained by hand), you can use the complex step method (google these terms you will have a bunch of references). For example:
function y = f(x)
s = poly(0,'s');
p = (s-s^2)^3;
y = horner(p,x).*exp(-x.^2);
end
x=linspace(-1,1,100);
d = imag(f(x+complex(0,1e-100)))/1e-100;
true_d = exp(-x.^2).*(-1+x).^2.*x^2.*(3-6*x-2*x.^2+2.*x^3)
disp(max(abs(d-true_d)))
--> disp(max(abs(d-true_d)))
1.776D-15
To evaluate a symbolic polynomial at a particular point or points, use the horner command. Example:
t = 0:0.1:1
v1 = horner(y1, t)
plot(t, v1)
This is the closest I got to a solution to this problem.
He proposes using:
old = 'f';
for i=1:n
new = 'd'+string(i)+'f';
deff('y='+new+'(x)','y=numderivative('+old+',x)');
old=new;
end
I know, it's horrible, but I think there is no better solution, at least in Scilab.
I found a way:
function y = deriva(f, v, n, h)
deff("y = DF0(x)", "y="+f)
if n == 0 then
y = DF0(v);
else
for i=1:(n-1)
deff("y=DF"+string(i)+"(x)", "y=numderivative(DF"+string(i-1)+",x,"+string(h)+",4)");
end
deff("y=DFN(x)", "y=numderivative(DF"+string(n-1)+",x,"+string(h)+",4)");
y = DFN(v);
end
endfunction
disp(deriva("x.*x", 3, 2, 0.0001));
This correctly calculates numerical derivatives of nth order. But it needs to have the function passed as a string. Errors can get pretty large, and time to compute tends to go up fast as a function of n.
I need to make a histogram, and my data points each carry a statistical weight. The standard hist function isn't equipped to handle this. I could of course import the numpy.histogram function, which handles weighted data just fine, but I thought it would be a good exercise in learning julia to try and augment the hist() function to accept weights as an optional (named) argument.
I started by looking at the julia source for hist(), and was able to modify it slightly (if amateurishly -- suggestions for improvements welcome), to get it sort of working:
function sturges(n) # Sturges' formula
n==0 && return one(n)
iceil(log2(n))+1
end
function weightedhist!{HT}(h::AbstractArray{HT}, v::AbstractVector, edg::AbstractVector; init::Bool=true, weights::AbstractVector = ones(HT,length(v)))
n = length(edg) - 1
length(weights) == length(v) || error("length(weights) must equal length(v)")
length(h) == n || error("length(h) must equal length(edg) - 1.")
if init
fill!(h, zero(HT))
end
for j=1:length(v)
i = searchsortedfirst(edg, v[j])-1
if 1 <= i <= n
h[i] += weights[j]
end
end
edg, h
end
weightedhist(v::AbstractVector, edg::AbstractVector; weights::AbstractVector = ones(Int,length(v))) = weightedhist!(Array(Float64, length(edg)-1), v, edg; weights=weights)
weightedhist(v::AbstractVector, n::Integer; weights::AbstractVector = ones(Int,length(v))) = weightedhist(v, histrange(v,n); weights=weights)
weightedhist(v::AbstractVector; weights::AbstractVector = ones(Int,length(v))) = weightedhist(v, sturges(length(v)); weights=weights)
If I generate some random data with
v = randn(10^5);
w = rand(length(v));
edges = floor(minimum(v)):0.1:ceil(maximum(v));
then weightedhist(v, edges; weights=w) agrees with numpy.histogram(v, edges, weights=w). If I leave out the optional keyword argument for weights, then weightedhist(v, edges) agrees with the built in hist(v, edges), and weightedhist(v) agrees with the built in hist(v), except for the fact that my function outputs floats rather than ints when no weights are provided.
I don't understand why this is the case (is h getting created as a float array? promoted?), and I'd like for the my function to fall back on the behavior of the built in one as closely as possible when no weights are provided.
Can anyone suggest why my function is outputting floats, and how I might change that behavior to output ints when no weights are provided? I'd like to do this without first creating the h array and then converting it from one type to another, since I'd like the code to be as fast as possible.
If I understand correctly, when you call
weightedhist(v, edges)
you are using the first of your three "extra" definitions at the bottom.
This calls
weightedhist!(Array(Float64, length(edg)-1), v, edg; weights=weights)
so in your "main" weightedhist! the HT parameterization will be Float64, so h will be filled with HT == Float64, hence the Float64 output. So changing it to Array(eltype(weights), length(edg)-1) would be sufficient, I believe.