Keep getting the following error : Unable to convert expression containing symbolic variables into double array.

4 visualizaciones (últimos 30 días)
Other fixes I've seen before do not seem to work on my code and I am unsure as to why this is. The code is the following:
b = [0 -2 -1 -1]';
A = [1 0 -1 3; 0 2 1 0; -1 1 6 -1; 3 0 -1 10];
syms alpha real;
x = zeros(4,10);
x(:,1) = [0 1 0 0];
xupdated = zeros(4,10);
a = alpha;
for i = 1:1:10
gradient= A*[x(:,i)] + b;
xupdated(:,i) = x(:,i)-gradient(:,i).*a;
fupdated = ([xupdated(:,1)]'*A*[xupdated(:,1)]+ [b]'*[xupdated(:,1)]);
diff_fupdated = diff(fupdated,a);
S = solve(diff_fupdated,a);
xupdated = x(:,1) - S*gradient;
x(:,i+1) = xupdated;
end
Any help would be greatly appreciated.

Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de Abr. de 2021
Recheck your assignments and your indexing.
b = [0 -2 -1 -1]';
A = [1 0 -1 3; 0 2 1 0; -1 1 6 -1; 3 0 -1 10];
syms alpha real;
x = zeros(4,10);
x(:,1) = [0 1 0 0];
xupdated = zeros(4,10, 'sym');
a = alpha;
for i = 1:1:10
gradient(:,i) = A*[x(:,i)] + b
xupdated(:,i) = x(:,i)-gradient(:,i).*a
fupdated = ([xupdated(:,1)]'*A*[xupdated(:,1)]+ [b]'*[xupdated(:,1)])
diff_fupdated = diff(fupdated,a)
S = solve(diff_fupdated,a)
xupdated(:,i) = x(:,1) - S*gradient(:,i)
x(:,i+1) = xupdated(:,i)
end
gradient = 4×1
0 0 0 -1
xupdated = 
fupdated = 
diff_fupdated = 
S = 
xupdated = 
x = 4×10
0 0 0 0 0 0 0 0 0 0 1.0000 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0
gradient = 4×2
0 0.1500 0 0 0 -0.0500 -1.0000 -0.5000
xupdated = 
fupdated = 
diff_fupdated = 
0
S = 
0
xupdated = 
x = 4×10
0 0 0 0 0 0 0 0 0 0 1.0000 1.0000 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0
gradient = 4×3
0 0.1500 0 0 0 0 0 -0.0500 0 -1.0000 -0.5000 -1.0000
xupdated = 
fupdated = 
diff_fupdated = 
0
S = 
0
xupdated = 
x = 4×10
0 0 0 0 0 0 0 0 0 0 1.0000 1.0000 1.0000 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0
gradient = 4×4
0 0.1500 0 0 0 0 0 0 0 -0.0500 0 0 -1.0000 -0.5000 -1.0000 -1.0000
xupdated = 
fupdated = 
diff_fupdated = 
0
S = 
0
xupdated = 
x = 4×10
0 0 0 0 0 0 0 0 0 0 1.0000 1.0000 1.0000 1.0000 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0
gradient = 4×5
0 0.1500 0 0 0 0 0 0 0 0 0 -0.0500 0 0 0 -1.0000 -0.5000 -1.0000 -1.0000 -1.0000
xupdated = 
fupdated = 
diff_fupdated = 
0
S = 
0
xupdated = 
x = 4×10
0 0 0 0 0 0 0 0 0 0 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0
gradient = 4×6
0 0.1500 0 0 0 0 0 0 0 0 0 0 0 -0.0500 0 0 0 0 -1.0000 -0.5000 -1.0000 -1.0000 -1.0000 -1.0000
xupdated = 
fupdated = 
diff_fupdated = 
0
S = 
0
xupdated = 
x = 4×10
0 0 0 0 0 0 0 0 0 0 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0
gradient = 4×7
0 0.1500 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.0500 0 0 0 0 0 -1.0000 -0.5000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
xupdated = 
fupdated = 
diff_fupdated = 
0
S = 
0
xupdated = 
x = 4×10
0 0 0 0 0 0 0 0 0 0 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0
gradient = 4×8
0 0.1500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.0500 0 0 0 0 0 0 -1.0000 -0.5000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
xupdated = 
fupdated = 
diff_fupdated = 
0
S = 
0
xupdated = 
x = 4×10
0 0 0 0 0 0 0 0 0 0 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0
gradient = 4×9
0 0.1500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.0500 0 0 0 0 0 0 0 -1.0000 -0.5000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
xupdated = 
fupdated = 
diff_fupdated = 
0
S = 
0
xupdated = 
x = 4×10
0 0 0 0 0 0 0 0 0 0 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0
gradient = 4×10
0 0.1500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.0500 0 0 0 0 0 0 0 0 -1.0000 -0.5000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
xupdated = 
fupdated = 
diff_fupdated = 
0
S = 
0
xupdated = 
x = 4×11
0 0 0 0 0 0 0 0 0 0 0 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0 0
x
x = 4×11
0 0 0 0 0 0 0 0 0 0 0 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0 0

Más respuestas (0)

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by