How can I get a calculated function value???
40 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Steven Lord
le 26 Jan 2021
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 commentaires
Steven Lord
le 26 Jan 2021
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.
Plus de réponses (2)
SaiDileep Kola
le 19 Jan 2021
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 commentaires
연승 김
le 19 Jan 2021
2 commentaires
SaiDileep Kola
le 19 Jan 2021
If possible can you share your code snipped and commands being executed, so that I can reproduce the issue at my end
Voir également
Catégories
En savoir plus sur Conversion Between Symbolic and Numeric dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!