我输入K值的循环只会输入第一个K值,打印正确的值,然后打印全部零,而不是使用下一个K值运行嵌套的for循环。
我的剧本
Exp1H1 = 35.6;
Exp1H2 = 24.7;
Exp2H1 = 46.8;
Exp2H2 = 37.8;
Exp3H1 = 45.7;
Exp3H2 = 36.4;
Exp4H1 = 47.7;
Exp4H2 = 39.2;
Radius = 3.75;
L = 10;
ArrayOfHeightDiff = [(Exp1H1-Exp1H2),(Exp2H1-Exp2H2),(Exp3H1-Exp3H2), (Exp4H1-Exp4H2)];
dhdl = ArrayOfHeightDiff./L
ArrayOfDarcys = [0.29,0.25,0.26,0.23];
v_meas = ((ArrayOfDarcys.*1000)./60)./(pi*Radius^2)
K = [-0.3 : 0.1 : 0.5];
for ii = 1 : 1 : length(K)
ExportSum = ls_error(dhdl, v_meas, K(ii))
ExportSum(1,ii) = ExportSum
end
ExportSum
这是功能
function [ExportSum] = ls_error(dhdl, v_meas, K)
total = 0;
L = length(dhdl);
for ii = 1 : 1 : L
dhdl1 = dhdl(1,ii);
vi = v_meas(1,ii);
yi = 1*K* dhdl1;
partialSum = (vi - yi)^2;
total = total + partialSum;
end
ExportSum = total;
end
答案 0 :(得分:1)
主要问题在于此var l123 = _.range(1, 4); // same as _(1, 2, 3)
var l0123 = _.range(3); // same as _(0, 1, 2)
var neg123 = _.range(-3, 0); // same as _(-3, -2, -1)
var empty = _.range(2,1); // same as _()
循环:
for
当这个循环执行时,它会在每次迭代时覆盖for ii = 1 : 1 : length(K)
ExportSum = ls_error(dhdl, v_meas, K(ii))
ExportSum(1,ii) = ExportSum
end
,然后将值复制到向量的ExportSum
列。没有理由将这些行分开,尝试将它们组合起来,看看你是否得到了你期望的结果:
iith
返回:
for ii = 1 : 1 : length(K)
ExportSum(1, ii) = ls_error(dhdl, v_meas, K(ii));
end