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

3 次查看(过去 30 天)
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-12-8
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;

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2023-12-8
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)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by