I did not understand the error which is called 'Error using symengine: Array sizes must match.' What should i do?

x1=0.2232585;
x2=0.2232585;
iteration=0;
while true
iteration=iteration+1;
syms x1s x2s t
func=(x2s-x1s^2)^2+(1-x1s)^2;
dif_x1=diff(func,x1s);
dif_x2=diff(func,x2s);
grad_x1_temp=vpa(subs(dif_x1,x1));
grad_x1=vpa(subs(grad_x1_temp,x2));
grad_x2_temp=vpa(subs(dif_x2,x2));
grad_x2=vpa(subs(grad_x2_temp,x1));
tx1_temp=subs(dif_x1,x1-grad_x1*t);
tx1=subs(tx1_temp,x2s,x2-grad_x2*t);
tx2_temp=subs(dif_x2,x2s,x2-grad_x2*t);
tx2=subs(tx2_temp,x1s,x1-grad_x1*t);
eqn=tx1*grad_x1+tx2*grad_x2;
a=solve(eqn,t,'Real',true);
val_temp=vpa(subs(func,x1s,x1));
val=vpa(subs(val_temp,x2s,x2));
x1=x1-grad_x1*a;
x2=x2-grad_x2*a;
val_temp_end=vpa(subs(func,x1s,x1));
val_end=vpa(subs(val_temp_end,x2s,x2));
if(val_end>val)
break;
end
end
iteration
Error using symengine
Array sizes must match.
Error in sym/privBinaryOp (line 1013)
Csym = mupadmex(op,args{1}.s,
args{2}.s, varargin{:});
Error in - (line 7)
X = privBinaryOp(A, B,
'symobj::zipWithImplicitExpansion',
'_subtract');
Error in THE5 (line 26)
tx1_temp=subs(dif_x1,x1-grad_x1*t);
I keep getting these errors anyone has a clue?

5 Kommentare

My guess in this line:
tx1_temp=subs(dif_x1,x1-grad_x1*t);
it seems the two arguments should be the same size (could it be, for example, that dif_x1 is a vector, and x1-grad_xt*t is a scalar?)
The code works without error for just 1 iteration so i believe the problem isn't the argument sizes its about loop with symbolic definings in it. But im not sure.
One of the problems are the subs calls, since it is not always obvious to me what substitutions you are making.
One potential solution could be creating symbolic functions (introduced in R2012a), and then substituting the appropriate values as arguments to the functions. (That is how I would do it.)
For example, start with:
func(x2s,x1s)=(x2s-x1s^2)^2+(1-x1s)^2;
dif_x1=diff(func,x1s);
dif_x2=diff(func,x2s);
and being certain in the rest of the code that the appropriate values are assigned to the appropriate function arguments. That is likely to produce the results you want, once you get the subsequent arguments correct.
The next line would then be:
grad_x1_temp(X)=vpa(dif_x1(x1,x1)); % Needs Two Arguments
with the appropriate arguments assigned.
I cannot go further, since I cannot understand what your code is doing.
Thank you, it seems your way of doing it is very efficient and less likely to get error. I’ll try defining the syms function and see wheter there is a problem or not.
My pleasure!
If I can understand what you are doing, I would write a specific solution (and post it as an Answer).

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Hi Mert,
The Probelm arises in the 3rd iteration. In the 3rd Iteration x1 is size of 3x1 symfun and grad_x1 is size of 9x1 .
so the line
tx1_temp=subs(dif_x1,x1-grad_x1*t);
is throwing error as MATLAB is not able to resolve "x1- grad_x1*t".
So make sure both x1 and grad_x1 are of same size or "x1-grad_x1*t" should make sense like we can subtract 2x2 matrix from 1x2 like this :
syms x1 x2
p = [x1,x2;x2,x1];
t = [x1,x1] - p;
Thanks,
Deepak

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by