Defining a function for a vector of values, while keeping two variables unknown
3 次查看(过去 30 天)
显示 更早的评论
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?
4 个评论
Walter Roberson
2021-10-21
elseif i==0 & ((y-x)/2571)==1
You are asking for the floating point calculation (y-x)/2571 to exactly equal an integer. That is risky because of floating point round-off. You would be better off rewriting it by multiplying it out,
elseif i==0 & (y-x)==2571*1
采纳的回答
Walter Roberson
2021-10-22
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
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]
[yG, xG, iG] = ndgrid(y, x, i);
pv = arrayfun(PV, yG, xG, iG)
sumPV = sum(pv, 1)
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 个评论
Walter Roberson
2021-10-22
Note: I don't say that this is necessarily the best way -- just that this method matches the requirements you defined about defining the function first and then substituting in values.
更多回答(3 个)
Sulaymon Eshkabilov
2021-10-21
编辑:Sulaymon Eshkabilov
2021-10-21
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))
Sulaymon Eshkabilov
2021-10-21
编辑:Sulaymon Eshkabilov
2021-10-21
% Here is the complete solution
syms y z
A =1:5;
F = @(A, y, z) sum(A+y+z);
FF = (F(A, y,z))
Jon
2021-10-22
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
5 个评论
Jon
2021-10-22
I'm sorry, I can't really follow the details of what you are trying to do here, but did my answer solve your problem?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!