# Defining a function for a vector of values, while keeping two variables unknown

3 views (last 30 days)
Laura Freitas on 21 Oct 2021
Commented: Laura Freitas on 22 Oct 2021
I have created a function on matlab dependent on three variables, say:
f= @(x,y,z) f(x,y,z)
I have a vector of values for x, let's call it A. My goal is to estimate function: So what I am looking for is a Matlab command which will allow me to calculate f(x,y,z) for each value of vector A, and then sum them together. Here are a few things I have tried that failed:
% Attempt #1: returns g as a number (0), rather than a function.
g = @(y,z) sum(f(A,y,z))
% Attempt #2: returns g as a number (0), rather than as a function.
g = @(y,z) cumsum(f(A,y,z))
% Attempt #3: doesn't recognize A as an input.
function g = g(y,z)
g = f(A,y,z)
end
I have checked and the issue lies in estimating f(A,y,Z), which immediately turns all values to 0. I know that a possible alternative to this is doing a manual sum of f(a_1,y,z) + f(a_2,y,z) etc, but there are 175 different numbers in vector A and coding this manually would take forever. Does anyone have any other ideas?
Laura Freitas on 21 Oct 2021
Thank you, that's a very good point!

Walter Roberson on 22 Oct 2021
So I must achieve the function, EV(x,i), and then replace by the values of x and i
If you must achieve the function first, and then replace the placeholders later, then you need to use the symbolic toolbox and use piecewise()
The below is probably not correct. You say that you need to sum() but you did not define the dimension(s) you need to sum over.
theta_11 = 1
theta_11 = 1
beta = 2
beta = 2
RC = 8
RC = 8
u_0 = @(y) -theta_11*0.001*y;
u_1 = @(y) -RC;
EV0 = @(y,j) 0;
V = @(y) log(exp(u_0(y)+beta*EV0(y,0))+exp(u_1(y)+beta*EV0(y,1)));
PV = @(y,x,i) Prob(y,x,i).*V(y); % use .* since V is also now a vector
% make up some example values just to try code
i = 0
i = 0
x = 10
x = 10
y = [0, 3*2571 + x, 2*2571+x, 2571+x]
y = 1×4
0 7723 5152 2581
[yG, xG, iG] = ndgrid(y, x, i);
pv = arrayfun(PV, yG, xG, iG)
pv = sumPV = sum(pv, 1)
sumPV = function P = Prob(Y,X,I)
y = sym(Y); x = sym(X); i = sym(I);
P = piecewise( ...
i == 0 & x == 442212 & y == 447354, ...
0.3621 + 0.0143, ...
i == 0 & x == 444783 & y == 447354, ...
0.5152 + 0.3621 + 0.0143, ...
i == 0 & x == 447354 & y == 447354, ...
1, ...
i == 1 & y == 0, ...
1, ...
i == 0 & (y-x) == 0*2571, ...
0.1071, ...
i == 0 & (y-x) == 1*2571, ...
0.5152, ...
i == 0 & i == 0 & (y-x) == 2*2571, ...
0.3621, ...
i == 0 & i == 0 & (y-x) == 3*2571, ...
0.0143, ...
symtrue, ...
0 );
end
##### 2 CommentsShowHide 1 older comment
Laura Freitas on 22 Oct 2021
Thank you, that worked!

Sulaymon Eshkabilov on 21 Oct 2021
Edited: Sulaymon Eshkabilov on 21 Oct 2021
Supposedly, this is what you are trying to achieve:
syms y z
A = magic(3);
F = @(A, y, z) cumsum(A+y+z);
FF = (F(A, y,z))
FF = Laura Freitas on 21 Oct 2021
Thank you for the answer! However, this doesn't exactly fit my goal. I have a 1x175 vector: And I already have a function defined: . I want to evaluate this function at each value of the vector for x. Basically I want to get vector: I then want to sum up all values of this vector to get function: .
Sorry if this was not clear the first time around!

Sulaymon Eshkabilov on 21 Oct 2021
Edited: Sulaymon Eshkabilov on 21 Oct 2021
% Here is the complete solution
syms y z
A =1:5;
F = @(A, y, z) sum(A+y+z);
FF = (F(A, y,z))
Laura Freitas on 21 Oct 2021
Thank you for trying again! However please note that is not a linear function, and so this approach does not work for my purposes. I tried to adapt your approach in my code in the following way:
g = @(A,y,z) sum(f(A,y,z))
And it returned a value of zero. Please note my comment above detailing the specifics of the code!

Jon on 22 Oct 2021
One way to do what I think you want is to have the inner function, in your case PV(y,x,i) accept a vector argument for y and return a vector of values, pv. Then you can easily sum up the elements of pv using MATLAB's sum function.
Here is an attempt to illustrate this where I have appropriately modified your code snippet.
By the way your function EV0 seems to do nothing as it will always return zero, so then in your definition of your function V(y) the terms beta*EV0(y,0) are always zero. Doesn't seem to make much sense, but the main point is showing you how to have the function P accept and return a vector argument, and summing up the elements
% make up some example values just to try code
theta_11 = 1
beta = 2
RC = 8
u_0 = @(y) -theta_11*0.001*y;
u_1 = @(y) -RC;
EV0 = @(y,j) 0;
V = @(y) log(exp(u_0(y)+beta*EV0(y,0))+exp(u_1(y)+beta*EV0(y,1)));
PV = @(y,x,i) Prob(y,x,i).*V(y); % use .* since V is also now a vector
% make up some example values just to try code
i = 0
x = 10
y = [0, 3*2571 + x, 2*2571+x, 2571+x]
pv = PV(y,x,i)
sumPV = sum(pv)
% Define the probability matrix, to accept vector y
function P = Prob(y,x,i)
% preallocate output vector
P = zeros(size(y));
for k = 1:numel(P)
if i==0 && x== 442212 && y(k) == 447354
P(k) = 0.3621 + 0.0143;
elseif i==0 && x== 444783 && y(k)== 447354
P(k) = 0.5152 + 0.3621 + 0.0143;
elseif i==0 && x== 447354 && y(k)== 447354
P(k) = 1;
elseif i == 1 && y(k)==0
P(k) = 1;
elseif i==0 && (y(k)-x)==0*2571
P(k) = 0.1071;
elseif i==0 && (y(k)-x)==1*2571
P(k) = 0.5152;
elseif i==0 && i==0 && (y(k)-x)==2*2571
P(k) = 0.3621;
elseif i==0 && i==0 &&(y(k)-x)==3*2571
P(k) = 0.0143;
else
P(k) = 0;
end
end
end
Laura Freitas on 22 Oct 2021
Thank you very much for trying, but unfortunately no. Then again, I think my method is probably not the best to go about this problem.