我有两个加速度矢量,我应该使用以下公式找到相对误差:
Relative_error =(1 / n)* sum(abs(a_s-a_m)/ a_m) 并且在区间1:n之间定义求和 a_s是模拟加速度,a_m是测量加速度
我尝试过以下代码,但似乎无法正常工作
for i=1:n;
re_1=abs(simulated_suspended_mass_acc(i)-cushion_base_acc(i))/cushion_base_acc(i)
result=(1/n)*sum(re_1(1:n))
end
我甚至尝试过cumsum和symsum。再次没有积极的结果。
任何帮助将不胜感激!
答案 0 :(得分:0)
如果我正确理解你的问题,那么将结果的计算放在循环之外并进行一些小修改应该会有所帮助:
single_errors = zeros(n,1); %Preallocate a length n vector to store the error at each point in the interval
valid_n = 0;
for i=1:n;
single_errors(i)=abs(simulated_suspended_mass_acc(i)-cushion_base_acc(i))/cushion_base_acc(i);
% You can add a check for NaNs in the loop with an easy if statement and try to do corrections
if isnan(single_errors(i))
single_errors(i) = 0;
else
valid_n = valid_n + 1; % One more valid value in the vector
end
end
result=(1/valid_n)*sum(single_errors);
%Division over valid_n to get the average value only for the valid values (not NaN)