フィルターのクリア

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 Center および File ExchangeLarge 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