Function and surface plot Julia - function

I want to plot a function using surface in Julia. I manage to plot te desired function:
x = 0:0.1:4
y = 0:0.1:4
f(x,y) = x^0.2 * y^0.8
surface(x, y, f, camera=(10,30),linealpha=0.3, fc=:heat)
However, I would f(*) to be a proper function over which I could also optimize (e.g. utility maximisation in economics). This is my attempt:
function Utility(x1, x2)
u= x.^0.2 .* y.^0.8
return u
end
But it unfortunately does not work. Can anybody help me?
Best
Daniel

I think Benoit's comment really should be the answer, but let me expand a little bit.
First of all, an inline function definition is not any different from a multi-line function definiton (see the first two examples in the docs here). Therefore, doing
utility(x, y) = x^0.2 * y^0.8
will give you a function that works exactly like
function utility(x, y)
x^0.2 * y^0.8
end
However, your Utility function is actually different from your f function - you are defining it with the arguments x1 and x2, but in the function body you are using y rather than x2.
This would ordinarily raise an undefined variable error, except that in the code snippet you posted, y is already defined in global scope as the range 0:0.1:4, so the function will use this:
julia> y = 0:0.1:4
0.0:0.1:4.0
julia> u(x1, x2) = x1 .^ 0.2 * y .^ 0.8
u (generic function with 1 method)
julia> u(2.0, 0.0)
41-element Array{Float64,1}:
0.0
0.18205642030260805
0.3169786384922227
...
this is also where your introduction of broadcasting in the Utility function (the second difference between your two examples as Benoit pointed out) comes back to haunt you: calling the function while relying on it to use the global variable y would error immediately without broadcasting (as you can't exponentiate a range):
julia> u2(x1, x2) = x1^0.2 * y^0.8
u2 (generic function with 1 method)
julia> u2(2.0, 0.0)
ERROR: MethodError: no method matching ^(::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Float64)
with broadcasting, however, this exponentiation works and returns the full range, with every element of the range exponentiated. Thus, your function returns an array rather than a single number (as you can see above from my call to u(2.0, 0.0). This is what Plots complains about - it doesn't know how to plot an array, when it expects to just be plotting a single data point.

Related

Julia: Having a function by summing over an array of functions

I have an array of functions and I want to make another function using a weighted sum over these functions. Let's say T=[T1, ..., T20]
and T1(x,y)=x^2+5, T2(x,y)=sin(x+y), ...
I want to make another function named g(x,y)=10*T1(x,y)+...+32*T20(x,y). the coefficients may change so I want to use some sort of loop but I couldn't.
This is my try, probably there is a better solution.
If you can change the signature of g:
g(x, y, weights, T) = sum(weights[i]*Ti(x,y) for (i,Ti) in enumerate(T))
If you need exactly this signature g(x,y), I suggest a generator function:
generate_g(weights, T) = (x,y) -> sum(weights[i]*Ti(x,y) for (i,Ti) in enumerate(T))
You pass the weights into it, and it returns a new function, that takes x and y as arguments. Here is an example
julia> T1(x,y)=x^2+5; T2(x,y)=sin(x+y); T=[T1, T2];
julia>g = generate_g([10,11], T)
#3 (generic function with 1 method)
julia> g(1,2)
61.55232008865854
When the weights change, you can generate a new g.

Differential equation; function in Octave

I try to solve a differential equation in Octave. In a first attempt, all my independent variables are set constant (n,y,w). This is my code:
function xdot= f(x,t);
% xdot=zeros(1,1);
X=1.44221E+12;
IO=5.318E+11;
GO=6.81E+11;
beta=0;
gamma=0;
y=58.5021088;
w= 31.29;
n=1363.5;
tw=0.4;
tp=0.3;
sw=0.07;
sp=0.25;
mw=0.593941689;
mp=0.593941689;
% aw=(1-tw)(sw+mw)
% ap=(1-tp)(sp+mp)
xdot=-(X+IO*(1+gamma*(diff(n)/n+diff(y)/y))+GO*beta(tw(diff(n)/n+diff(w)/w)+tp(diff(y)/y-diff(w)/w)-(x*n)/((y-w)((1-tp)(sp+mp)+tp)+((1-tw)(sw+mw)+tw)*w))*x)/(IO*gamma+GO*beta*tw);
endfunction
When I add
t = [0:(1/360):10]
x = lsode ("f", 39290000, t);
to solve the equation in the command line, I get this error:
error: index (0.843942): subscripts must be either integers 1 to (2^31)-1 or logicals
error: lsode: evaluation of user-supplied function failed
error: called from
f at line 23 column 7
It seems to me, in some way I misunderstood how to make the function.
Any help?
EDIT:
This is my new code:
function xdot= f(x,t);
X=1.44221E+12;
IO=5.318E+11;
GO=6.81E+11;
beta=0;
gamma=0;
y=58.5021088;
w= 31.29;
n=1363.5;
tw=0.4;
tp=0.3;
sw=0.07;
sp=0.25;
mw=0.593941689;
mp=0.593941689;
xdot=-X+IO-(x*n)/((y-w)*((1-tp)*(sp+mp)+tp)+w*(tw+(1-tp)*(sp+mp)))
endfunction
It does work if I copy it into the command line but if I start it as programm (f.m) and solve it then I get this error:
error: 'x' undefined near line 25 column 15
error: called from
f at line 25 column 7
From the documentation of lsode:
Syntax: [x, istate, msg] = lsode (fcn, x_0, t)
...
The first argument, fcn, is a string, inline, or function handle that names the function f to call to compute the vector of right hand sides for the set of equations. The function must have the form
xdot = f (x, t)
in which xdot and x are vectors and t is a scalar.
So firstly, your f function is of the wrong form to the one expected by lsode, since it takes 5 arguments as opposed to 2.
Secondly, inside your f function, the variables y, w, and n are overwritten, regardless of whether they were supplied, whereas t and x need to be supplied by the user.
When lsode calls the handle f, it calls it with two arguments as per its specification, which means it instantiates t and y, and calls the function with the remaining input arguments undefined. Of the remaining arguments, w and n are given values inside the function, as we pointed out above, but x remains undefined. So when you run the final calculation which relies on x, you get an error saying it has not yet been defined during the call to this function.
So, to fix this issue, rewrite your function such that its signature is f(x, t), which you can do so easily since y, w, and n are defined inside your function anyway, so they don't really need to be given as inputs.

Lua - Higher-order Derivative function

How does returning functions work in Lua? I'm trying to wrap my head around this derivative function but I'm having trouble understanding it.
function derivative(f, delta)
delta = delta or 1e-4
return function(x)
return (f(x + delta) - f(x))/delta
end
end
Any help is appreciated, thanks.
First see here.
Shortly, functions are first-class citizens and you an store them in variable and return from functions.
Second. In your example there is a closure to be created. It will have upvalues f and delta, wich can be used in inner function.
So when you call your derivative function, new closure will be created with copy of f and delta. And you can call it later like any other function
local d = derivative(f, 1e-6)
d(10)
EDIT: "Answer on but I'm having trouble understanding how the x argument is treated in the anonymous function in my example"
Each function have a signature, number of formal attributes, it will get.
In Lua you can call function with any number of arguments. Let's consider an example.
local a = function(x1, x2) print(x1, x2) end
a(1) // 1, nil
a(1, 2) // 1, 2
a(1, 2, 3) // 1, 2
When you call function in variable a, each given argument value, one by one will be matched with function argumentList. In 3-rd example 1 will be assigned to x1, 2 to x2, 3 will be thrown away. In term's of vararg function smth like this will be performed.
function a(...)
local x1 = (...)[1]
local x2 = (...)[2]
// Body
end
In your example x is treated as inner function argument, will be visible inside it, and initialized when you call your inner function instance.
f and delta will be unique for each function instance, as I mentioned above.
Hope my clumsy explanations will hit their goal and help you a little.

Error plotting a function of 2 variables

I am trying to plot the function
f(x, y) = (x – 3).^2 – (y – 2).^2.
x is a vector from 2 to 4, and y is a vector from 1 to 3, both with increments of 0.2. However, I am getting the error:
"Subscript indices must either be real positive integers or logicals".
What do I do to fix this error?
I (think) I see what you are trying to achieve. You are writing your syntax like a mathematical function definition. Matlab is interpreting f as a 2-dimensional data type and trying to assign the value of the expression to data indexed at x,y. The values of x and y are not integers, so Matlab complains.
If you want to plot the output of the function (we'll call it z) as a function of x and y, you need to define the function quite differently . . .
f = #(x,y)(x-3).^2 - (y-2).^2;
x=2:.2:4;
y=1:.2:3;
z = f( repmat(x(:)',numel(y),1) , repmat(y(:),1,numel(x) ) );
surf(x,y,z);
xlabel('X'); ylabel('Y'); zlabel('Z');
This will give you an output like this . . .
The f = #(x,y) part of the first line states you want to define a function called f taking variables x and y. The rest of the line is the definition of that function.
If you want to plot z as a function of both x and y, then you need to supply all possible combinations in your range. This is what the line containing the repmat commands is for.
EDIT
There is a neat Matlab function meshgrid that can replace the repmat version of the script as suggested by #bas (welcome bas, please scroll to bas' answer and +1 it!) ...
f = #(x,y)(x-3).^2 - (y-2).^2;
x=2:.2:4;
y=1:.2:3;
[X,Y] = meshgrid(x,y);
surf(x,y,f(X,Y));
xlabel('x'); ylabel('y'); zlabel('z');
I typically use the MESHGRID function. Like so:
x = 2:0.2:4;
y = 1:0.2:3;
[X,Y] = meshgrid(x,y);
F = (X-3).^2-(Y-2).^2;
surf(x,y,F);
xlabel('x');ylabel('y');zlabel('f')
This is identical to the answer by #learnvst. it just does the repmat-ing for you.
Your problem is that the function you are using uses integers, and you are trying to assign a double to it. Integers cannot have decimal places. To fix this, you can make it to where it increases in increments of 1, instead of 0.2

summing functions handles in matlab

Hi
I am trying to sum two function handles, but it doesn't work.
for example:
y1=#(x)(x*x);
y2=#(x)(x*x+3*x);
y3=y1+y2
The error I receive is "??? Undefined function or method 'plus' for input arguments of type 'function_handle'."
This is just a small example, in reality I actually need to iteratively sum about 500 functions that are dependent on each other.
EDIT
The solution by Clement J. indeed works but I couldn't manage to generalize this into a loop and ran into a problem. I have the function s=#(x,y,z)((1-exp(-x*y)-z)*exp(-x*y)); And I have a vector v that contains 536 data points and another vector w that also contains 536 data points. My goal is to sum up s(v(i),y,w(i)) for i=1...536 Thus getting one function in the variable y which is the sum of 536 functions. The syntax I tried in order to do this is:
sum=#(y)(s(v(1),y,z2(1)));
for i=2:536
sum=#(y)(sum+s(v(i),y,z2(i)))
end
The solution proposed by Fyodor Soikin works.
>> y3=#(x)(y1(x) + y2(x))
y3 =
#(x) (y1 (x) + y2 (x))
If you want to do it on multiple functions you can use intermediate variables :
>> f1 = y1;
>> f2 = y2;
>> y3=#(x)(f1(x) + f2(x))
EDIT after the comment:
I'm not sure to understand the problem. Can you define your vectors v and w like that outside the function :
v = [5 4]; % your 536 data
w = [4 5];
y = 8;
s=#(y)((1-exp(-v*y)-w).*exp(-v*y))
s_sum = sum(s(y))
Note the dot in the multiplication to do it element-wise.
I think the most succinct solution is given in the comment by Mikhail. I'll flesh it out in more detail...
First, you will want to modify your anonymous function s so that it can operate on vector inputs of the same size as well as scalar inputs (as suggested by Clement J.) by using element-wise arithmetic operators as follows:
s = #(x,y,z) (1-exp(-x.*y)-z).*exp(-x.*y); %# Note the periods
Then, assuming that you have vectors v and w defined in the given workspace, you can create a new function sy that, for a given scalar value of y, will sum across s evaluated at each set of values in v and w:
sy = #(y) sum(s(v,y,w));
If you want to evaluate this function using an array of values for y, you can add a call to the function ARRAYFUN like so:
sy = #(y) arrayfun(#(yi) sum(s(v,yi,w)),y);
Note that the values for v and w that will be used in the function sy will be fixed to what they were when the function was created. In other words, changing v and w in the workspace will not change the values used by sy. Note also that I didn't name the new anonymous function sum, since there is already a built-in function with that name.