Unable to perform assignment

8 visualizaciones (últimos 30 días)
JRC
JRC el 18 de Abr. de 2024
Comentada: JRC el 18 de Abr. de 2024
I have code that compiles for t=4. For t greater than 4 the following error message appears:
"Unable to perform assignment because the left and right sides have a different number of elements"
The code is:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t=5;
syms u10 u9 u8 u7 u6 u5 u4 u3 u2 u1;
syms x10 x9 x8 x7 x6 x5 x4 x3 x2 x1;
syms y10 y9 y8 y7 y6 y5 y4 y3 y2 y1;
a12 =0.8;
a13=0.05;
b21 =0.6;
u=[];x=[];y=[];
for j=1:1:t+1
u = [u; sym(['u' num2str(j)])];
x = [x; sym(['x' num2str(j)])];
y = [y; sym(['y' num2str(j)])];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
J(t+1)=0;
for N=t:-1:1
sis1 = x(N)+a12*y(N)-a13*x(N)*y(N);
sis2 = y(N)-a12*y(N)+b21*u(N)+a13*x(N)*y(N);
Func =u(N)^2+3*y(N);
JJ(N) = subs(Func + J(N+1), {x(N+1),y(N+1)},{sis1,sis2});
JJ1(N)=simplify(JJ(N));
b1(N) = diff(JJ1(N), u(N));
[p1(N)] = solve(b1(N), u(N));
[p(N)] = simplify(p1(N));
J1(N) = subs(JJ(N), {u(N)}, {p(N)});
J(N) = simplify(J1(N));
end
Unable to perform assignment because value of type 'sym' is not convertible to 'double'.

Caused by:
Error using mupadengine/feval2char
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Thanks.
  2 comentarios
Dyuman Joshi
Dyuman Joshi el 18 de Abr. de 2024
Dynamically defining variables is not a good idea, as you have done here -
syms u10 u9 u8 u7 u6 u5 u4 u3 u2 u1;
syms x10 x9 x8 x7 x6 x5 x4 x3 x2 x1;
syms y10 y9 y8 y7 y6 y5 y4 y3 y2 y1;
and here -
u=[];x=[];y=[];
for j=1:1:t+1
u = [u; sym(['u' num2str(j)])];
x = [x; sym(['x' num2str(j)])];
y = [y; sym(['y' num2str(j)])];
end
A better method is to define a symbolic array like this, so that you can define all of them in a single command (as I have done below) -
syms u [1 10]
u
u = 
And use indexing to access the individual variables -
u(1)
ans = 
%Define them collectively in a single command
syms u x y [1 10]
As for the error occuring in the 2nd for loop, it is not clear to me what the objective is or what you want to do, thus I can not suggest anything other than correcting the size of preallocated variable.
JRC
JRC el 18 de Abr. de 2024
Thank you for the symbolic command that simplifies the expression, but did not solve the problem. The problem is in looping since I cannot compile for t equal to 5, for example.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de Abr. de 2024
Movida: Walter Roberson el 18 de Abr. de 2024
t=5;
syms u10 u9 u8 u7 u6 u5 u4 u3 u2 u1;
syms x10 x9 x8 x7 x6 x5 x4 x3 x2 x1;
syms y10 y9 y8 y7 y6 y5 y4 y3 y2 y1;
a12 =0.8;
a13=0.05;
b21 =0.6;
u=[];x=[];y=[];
for j=1:1:t+1
u = [u; sym(['u' num2str(j)])];
x = [x; sym(['x' num2str(j)])];
y = [y; sym(['y' num2str(j)])];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
J(t+1) = sym(0);
P1 = zeros(1,t,'sym');
for N=t:-1:1
fprintf('N = %d\n', N);
sis1 = x(N)+a12*y(N)-a13*x(N)*y(N);
sis2 = y(N)-a12*y(N)+b21*u(N)+a13*x(N)*y(N);
Func =u(N)^2+3*y(N);
JJ(N) = subs(Func + J(N+1), {x(N+1),y(N+1)},{sis1,sis2});
JJ1(N)=simplify(JJ(N));
b1(N) = diff(JJ1(N), u(N));
sol = solve(b1(N), u(N));
if isempty(sol)
fprintf('warning: for N = %d there are no solutions\n', N);
p1(N) = sym(NaN);
else
if numel(sol) > 1
fprintf('warning: for N = %d, there are %d solutions\n', N, numel(sol));
else
fprintf('warning: for N = %d, there is the expected single solution\n', N);
end
p1(N) = sol(1);
end
[p(N)] = simplify(p1(N));
J1(N) = subs(JJ(N), {u(N)}, {p(N)});
J(N) = simplify(J1(N));
end
N = 5
warning: for N = 5, there is the expected single solution
N = 4
warning: for N = 4, there is the expected single solution
N = 3
warning: for N = 3, there is the expected single solution
N = 2
warning: for N = 2, there is the expected single solution
N = 1
warning: for N = 1, there are 5 solutions
  1 comentario
JRC
JRC el 18 de Abr. de 2024
Hi Walter Roberson. Thank you for the response.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Symbolic Variables, Expressions, Functions, and Preferences en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by