I keep getting this error in my code

2 ビュー (過去 30 日間)
johnmurdock
johnmurdock 2019 年 4 月 27 日
編集済み: per isakson 2019 年 4 月 28 日
Code:
y = 0.2;
t = 0;
h = 0.01;
tn = 0.2;
kfv = [30 20 40];
for Kidx = 1 : length(kfv)
kf = kfv(Kidx);
%we assume that all the t values are the same
[t, y(:,Kidx)] = Euler(t, y, h, tn, kf);
end
for Kidx = 1 : length(kfv)
kf = kfv(Kidx);
fprintf('The concentration of H2O after %g seconds (kf=%g): %g\n', t(end), kf, y(end, Kidx));
end
function [t, y] = Euler(t0, y0, h, tn, kf)
t = (t0:h:tn)';
y = zeros(size(t));
y(1) = y0;
for i = 1:1:length(t)-1
y(i+1) = y(i) + h*f(y(i), t(i), kf);
end
end
function dydt = f(y, t, kf)
c = 0.2;
b = 0.4;
a = 0.5;
dydt = kf*b*a;
end
>> TakeHomeQ1
Assignment has more non-singleton rhs dimensions than non-singleton
subscripts
Error in TakeHomeQ1 (line 9)
[t, y(:,Kidx)] = Euler(t, y, h, tn, kf);
Thanks!
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 4 月 28 日
編集済み: per isakson 2019 年 4 月 28 日
You have a conflict between y = 0.2 (a scalar), passing y (all of it) into Euler where it gets stored into the scalar location y(1), and receiving the output of Euler (which is length(t)) into y(:,Kidx)

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by