Help with symbolic substitution
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Yongjian Feng
2021-7-27
%
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)
更多回答(1 个)
KSSV
2021-7-27
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!