How can I get a calculated function value???
42 ビュー (過去 30 日間)
古いコメントを表示
I want to calculate Lagrangian fuction.
L(x_wt11, x_wt21, x_wt12, x_wt22) = Vt1 * ( (1-p_wt11)^x_wt11 * (1-p_wt21)^x_wt21 )...
+ Vt2 * ( (1-p_wt12)^x_wt12 * (1-p_wt22)^x_wt22 )...
- lambda1 * ( 1-(x_wt11 + x_wt12))...
- lambda2 * ( 1-(x_wt21 + x_wt22))...
- lambda3 * ( Vw1 * (1-s_wt11/u)^x_wt11 * (1-s_wt12/v)^x_wt12...
+ Vw2 * (1-s_wt21/u)^x_wt21 * (1-s_wt22/v)^x_wt22 - 2 * beta);
When I input L(0,0,0,0) (or L(1,0,0,0), L(0,1,1,0)...etc)
the answer is not calculated value, although I declare all parameters.
In this equation, "x_wt11, x_wt21, x_wt12, x_wt22" are binary variable,
and anothers are parameters(constant).
I declare parameters before this equation except 'u, v'
because 'u,v' are dependent on "x_wt11, x_wt21, x_wt12, x_wt22" value.
In summary,
1. declare parameters. (except 'u,v')
2. declare Lagrangian function.
3. declare parameters 'u, v'
* The reason why I declaring 'u,v' afterwards, I want to prevent deviding by zero.
* u = x_wt11 + x_wt21 , v = x_wt12 + x_wt22
* So, if "x_wt11=0", "x_wt21=0", and then u is zero.
* To solve this problem, I use "if" statement.
if (x_wt11+x_wt21) == 0
u=1;
elseif (x_wt11+x_wt21) == 1
u=1;
elseif (x_wt11+x_wt21) == 2
u=2;
else
u=0;
end
And then, if I input "L(0,0,0,0)"
the answer is like this way, "52/(22*u) + 69/(22*v) + 74/56"
I declare parameters 'u,v' but, Why the answer is not a calculated??? and How I can solve this problem???
0 件のコメント
採用された回答
Steven Lord
2021 年 1 月 26 日
Since you're mentioning that you evaluate the function and then define a value for u, I assume you're using symbolic variables from Symbolic Math Toolbox. Let's take a slightly simpler example.
syms x a
f(x) = a*x;
y = f(2)
When I create y, MATLAB substitutes 2 in place of x in the symfun body. Giving a value to the variable a after f(x) has been created doesn't change f(x).
a = 3;
z = f(2)
Using subs to substitute the numeric value of a into z will perform the substitution.
z2 = subs(z) % or subs(z, 'a', a)
Alternately you could make your function a function of multiple symbolic variables:
syms w b
g(w, b) = b*w;
evaluateWithValueForw = g(4, b)
evaluateWithValueForb = g(w, 5)
evaluateWithValueForBoth = g(4, 5)
3 件のコメント
Steven Lord
2021 年 1 月 26 日
If the symbolic expression does not contain any symbolic variables, use double to convert it to a double array. If it does contain symbolic variables, use vpa.
その他の回答 (2 件)
SaiDileep Kola
2021 年 1 月 19 日
I see no reason why it is not calculated after declaring all values, check if values "u" and "v" are present in the workspace and try to run the equation once again if you see those values are present.
0 件のコメント
연승 김
2021 年 1 月 19 日
2 件のコメント
SaiDileep Kola
2021 年 1 月 19 日
If possible can you share your code snipped and commands being executed, so that I can reproduce the issue at my end
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!