Azzera filtri
Azzera filtri

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

3 visualizzazioni (ultimi 30 giorni)
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 Commento
Dyuman Joshi
Dyuman Joshi il 8 Dic 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;

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 8 Dic 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)

Categorie

Scopri di più su Large Files and Big Data in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by