how to Distribute power in a sym array?

hi everyone,
I just want to have x^2 + 4*x + 4 rather than (x+2).^2
and in this case i mean "r" variable
clc
clear
n=1;
k=[10,-3,2;-3,5,1;2,1,6];
p=[10;10;22];
sz=size(p,1);
s=0;
d = sym('d',[1 sz]);
for i=1:sz
for j=1:sz
s = s+ sum(k(i,j).*d(j));
end
f(i)=s;
s=0;
r(i)=p(i)-f(i);
end
sum(r.^2)
answer i get is :
(3*d1 - 5*d2 - d3 + 10)^2 + (10*d1 - 3*d2 + 2*d3 - 10)^2 + (2*d1 + d2 + 6*d3 - 22)^2

Respuestas (1)

Walter Roberson
Walter Roberson el 17 de En. de 2023

0 votos

expand()
It is common to also use collect() after expand() especially if there is more than one symbolic variable

Categorías

Más información sobre Symbolic Math Toolbox en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Etiquetas

Preguntada:

el 17 de En. de 2023

Respondida:

el 17 de En. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by