Borrar filtros
Borrar filtros

Unable to perform assignment because the left and right sides have a different number of elements.

3 visualizaciones (últimos 30 días)
I keep getting the error 'Unable to perform assignment because the left and right sides have a different number of elements.' whenever I try to run it, and I'm not quite sure why as according to the workspace, all elements have the same size/value. Any help fixing this would be appreciated!
h = 0.08;
t = 0:h:2;
x = zeros(size(t));
x(1) = 2/25;
n = numel(x);
f = 5*(x-t.^2);
sz_x = size(x);
sz_f = size(f);
for i = 1:n-1
f = 5*(x-t.^2);
x(i+1) = x(i) + h*f;
end
  1 comentario
Dyuman Joshi
Dyuman Joshi el 8 de Dic. de 2023
f is a vector, thus the RHS of the assignment is a vector, and you are trying to fit a vector into a scalar placeholder, which results in the error you get.
x(i+1) = x(i) + h*f;

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 8 de Dic. de 2023
This version of the code does more work than is necessary, but has the minimal change needed to prevent the particular error you were encountering.
h = 0.08;
t = 0:h:2;
x = zeros(size(t));
x(1) = 2/25;
n = numel(x);
f = 5*(x-t.^2);
sz_x = size(x);
sz_f = size(f);
for i = 1:n-1
f = 5*(x-t.^2);
x(i+1) = x(i) + h*f(i);
end
plot(t, x)

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by