MATLAB Answers

How can I get a calculated function value???

47 views (last 30 days)
연승 김
연승 김 on 14 Jan 2021
Commented: 연승 김 on 27 Jan 2021
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
elseif (x_wt11+x_wt21) == 1
elseif (x_wt11+x_wt21) == 2
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???

Accepted Answer

Steven Lord
Steven Lord on 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)
y = 
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)
z = 
Using subs to substitute the numeric value of a into z will perform the substitution.
z2 = subs(z) % or subs(z, 'a', a)
z2 = 
Alternately you could make your function a function of multiple symbolic variables:
syms w b
g(w, b) = b*w;
evaluateWithValueForw = g(4, b)
evaluateWithValueForw = 
evaluateWithValueForb = g(w, 5)
evaluateWithValueForb = 
evaluateWithValueForBoth = g(4, 5)
evaluateWithValueForBoth = 
연승 김
연승 김 on 27 Jan 2021
Wow!!!!! Thank you!!
"double" works!!!
I had to spend too much time on this trouble. uu...
Thank you again!
Happy new year and have a nice day~! smile^^

Sign in to comment.

More Answers (2)

SaiDileep Kola
SaiDileep Kola on 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.

연승 김
연승 김 on 19 Jan 2021
Thank you for your advice.
I checked it before, but the result is not changed...
I find a trick for this, but it is not a ultimate solution.
The trick is that,
after I receive an answer "52/(22*u) + 69/(22*v) + 74/56",
and I input "52/(22*u) + 69/(22*v) + 74/56".
And then, the anwser is calculated value.
(I don't know why...)
But I want to receive a calculated value at once.
연승 김
연승 김 on 26 Jan 2021
Thank you for your kind.
Now I'm trying to some tricks and if I solve the trouble, I'll contact to you.
Thank you!!

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by