Defining a function for a vector of values, while keeping two variables unknown
이전 댓글 표시
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
Jon
2021년 10월 21일
could you also please provide the code that defines your function f(x,y,z)
Laura Freitas
2021년 10월 21일
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
Laura Freitas
2021년 10월 21일
채택된 답변
추가 답변 (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
Laura Freitas
2021년 10월 22일
Jon
2021년 10월 22일
I don't know what you mean by saying x and i are meant to remain unknown. If you have a function Prob(y,x,i) certainly you must know the values of y, x, and i to evaluate it.
Laura Freitas
2021년 10월 22일
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?
Laura Freitas
2021년 10월 22일
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



