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

1 回表示 (過去 30 日間)
Hi there, looking for help with my code, I have attached the question I have been asked and the code I have created? Thanks
h = 0.1; % step size
x = 0:h:5; % the range of x
y = zeros(numel(x),1); % size(x) give a square matrix
y(1) = (2); % the initial y value y(1) no y(i)
n = numel(y); % the number of y values
% The loop to solve the DE
for i=1:n-1
f = x.^4.*exp(-x.^3); %reference x(i)
y(i+1) = y(i) + h * f;
end
Screen Shot 2019-11-19 at 13.23.51.png

回答 (1 件)

Jyothis Gireesh
Jyothis Gireesh 2019 年 11 月 22 日
Here are a few pointers which may be of help to you:
  • Here “x” is defined to be a row vector of size 1 x 51. So, the variable “f” inside the for loop evaluates to a vector of the same size as “x”. And each element of “y”, “y(i)” is a scalar which is assigned a vector inside the loop, thereby resulting in a dimension mismatch error.
  • One possible solution may be to calculate the value of “f” for each index of “x”. This may be done by replacing “x” by “x(i)” in the expression for “f” as follows
f = x(i).^4.*exp(-x(i).^3);

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by