# Help with symbolic substitution

1 view (last 30 days)
I have created zz that has 3 symbols, x_1, x_2 and x_3. I later generated 3 random numbers, xx, to replace the symbols and calculate zz.
My problem now is how to substitute the 3 random numbers such that the 1st, 2nd and 3rd numbers respectively replace x_1, x_2 and x_3 in order to calculate a value for zz as pp.
%
syms x_1 x_2 x_3
y = (x_1 + x_2 + x_3)^2;
yy = expand(y)
numerator = (x_1*x_1) + (x_1*x_2) + (x_1*x_3);
zz = numerator / yy
% Generate a random number for each of x_1, x_2 and x_3:
xx = randi(10,[1 3]);
% Use xx to calculate zz:
pp = subs(zz, xx) % ouput is incorrect. Help is needed!

Yongjian Feng on 27 Jul 2021
%
syms x_1 x_2 x_3
y = (x_1 + x_2 + x_3)^2;
yy = expand(y)
numerator = (x_1*x_1) + (x_1*x_2) + (x_1*x_3);
zz = numerator / yy
% Generate a random number for each of x_1, x_2 and x_3:
x_1=randi(10);
x_2=randi(10);
x_3=randi(10);
pp = subs(zz)
Richard Fiifi Annan on 27 Jul 2021
Thanks very much @Yongjian Feng

KSSV on 27 Jul 2021
Why don't you use anonymous function ?
zz = @(x) (x(1) + x(2) + x(3))^2/(x(1)^2 + x(1)*x(2) + x(1)*x(3)) ;
% Generate a random number for each of x_1, x_2 and x_3:
xx = randi(10,[1 3]);
% Use xx to calculate zz:
pp = zz(xx)
pp = 1.2857
Richard Fiifi Annan on 27 Jul 2021
Thanks for the quick response @KSSV. Surely your answer solves the problem. My reason for posting the question was because zz is a product of some other calculations upstream. So I thought perhaps there might be an MATLAB in-built function for solving it.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}