Please help me understand whether I have set up a loop and performed an RMSE (Root Mean Square Error) calculation correctly

I am returning to Matlab after such a long break that I'm essentially a newbie. I'd be very grateful if someone would please help me figure out if I have done what I intend to do!
  • I have a column of data (avgW) from measuring something over time.
  • I have an equation (second line of my code below) to calculate the same thing I measured (wCalc). In the equation, the parameters, a, b, c, d, e, and f are all constants. The parameter "columnOfData" is a column of distinct values.
  • My goal is to find the value of "x" in the equation. What I am trying to do below is first plug in a test value for x (1 - 10000). Then figure out which value (between 1 and 10000) gives me the smallest difference between wCalc and avgW.
I think my method so far would work if "columnOfData" were a constant, but it isn't and I am lost. I'm not even sure what I'm going to end up with when my current calculation finishes. Would anyone happen to know how to find and plot and record the lowest value of RMSE for each value of the column/vector, columnOfData?
Please feel free to comment if there are more appropriate tags for this question. Thank you.
for x = [1:1:10000]
wCalc = ( ( a ./ ( ( columnOfData ./ x ) - b ) ) - c - d - e ) .* f;
RMSE(x) = sqrt( sum( ( wCalc - avgW).^2) ./ g );
end

2 Comments

if RMSE is calculated for each "x", then plot(x,RMSE) would work. and the lowest value is found by "[i,m]=min(RMSE)".
if columnOfData changes, then add another for loop to calculate for each Data set.
if the goal is to find the error of RMS value calculation, why not using integral and calculate the convergence error?
@hosein Javan, I don't know how to use the integral method you mentioned (I'm pretty new to error analysis) but will look it up. I'll think about your loop suggestion and see if I can come up with a solution. Thank you.

Sign in to comment.

 Accepted Answer

You could do something like this.
I just made up some numbers to try the code.
Also, obviously variable names like columnOfData are not very meaningful and I wouldn't recommend them but I didn't want to depart to far from your starting point.
I removed some of the ./ and just used / where the operation involved a vector and a scalar so element by element calculations were not needed. Would work with ./ but makes it less clear what you are doing. I also defined x separately from the index in the loop, in case you wanted to use other x values and also so tha you could have an index for recording the RMSE values.
With some more work, you could probably fully vectorize this (eliminate the loop) but at least this should get you started.
% define constants
a = 2.2
b = 3.4
c = 8.1
d = 10.4
e = 0.6
f = 22.4
% define measured value
avgW = [8;7.2;44;3.12]
% define column of data
columnOfData = [3;9;8.3;2]
% number of elements in avgW (and column of data)
g = length(avgW)
x = 1:10000;
for k = 1:10000
wCalc = ( ( a ./ ( ( columnOfData / x(k) ) - b ) ) - c - d - e ) * f; % a vector
RMSE(k) = sqrt( sum( ( wCalc - avgW).^2) / g );
end
% find optimal value, and it's index in the array x
[RMSEOpt,idx] = min(RMSE)
xOpt = x(idx)

8 Comments

@Jon, thank you very much indeed for your help. I'm going to think about your suggestion and try it. It's night-time for me, so I'll let you know tomorrow if I managed to understand.
@Jon, I'm wondering whether it should be x(k) (where x and k are both varying from 1 - 10000) or columnOfData(k).
From what I could infer from your problem description, it looked like there was only one columnOfData and you would use this to produce values of the vector wCalc, for a range of values of x. If this is the case, then it should be x(k) as written not columnOfData(k). It wouldn't make sense to index columnOfData if there is only one of them.
Thanks a lot, Jon. I'm testing it right now. It is taking a very long time to run, but I will let you know how it goes when it is done.
The little example code I attached earlier takes only milliseconds to run on my laptop. Maybe your machine is a little slower, but shouldn't take very long. Are you sure that you have programmed it correctly?
Good point. I really feel like a fish out of water even though Matlab is not that different from the methods I have been using for a few years! Thanks for your patience.
I ran your example and it works very fast, like you said. So now I need to figure out what I am doing wrong as far as translating it to my example!
If you are stuck, and you could always attach your code, or a relevant fragment of it. If at this point your original question is answered, when you have a chance, please accept this answer so others will know it has been answered.
Thank you very much for all your help, Jon. I would not have been able to figure this out otherwise. I had to add another step because I kept getting a "nan" result (even though I thought I had elimated nans from my inputs), but it is finally working. Your solution is much more convenient than my original idea.

Sign in to comment.

More Answers (1)

is this way more efficient?
freq = 1;
T = 1/freq;
w = 2*pi*freq;
a = [1 1/2]; % harmonic amplitudes
y = @(t) a(1)*sin(w*t) + a(2)*sin(2*w*t); % define a periodic function with 2 harmonics
format long
RT = 1e-5; % relative tolerance of integral
RMS_cal = sqrt(freq*integral(@(t)y(t).^2,0,T, 'RelTol', RT )) % calculated RMS value using integral
RMS_exact = sqrt(sum(a.^2))/sqrt(2) % exact value of RMS
RMS_cal =
0.790569415042095
RMS_exact =
0.790569415042095
if your signal's frequency is known, you can find the exact rms value and compare it to your integral.

4 Comments

I did not see anything in the original post about harmonic analysis. It just looked liked the OP was fitting a vector valued function. Was there some other information that I missed? If not are you trying to say that something similar to what you show in your example could be applied to the function fitting problem the OP has presented? Thanks for clarifying.
No, it was just a propose, I thought if this way is better. harmonics could be useful when trying to calculate RMS value of a signal. however I was trying to calculate RMS value by using integral rather than discrete method which is inaccurate, because discrete signals cannot contain all data.
Thanks, hosein. I'm not sure if harmonic analysis would be applicable to my problem, but I will think it over once I'm done with testing another possible solution.
you're welcome. the harmonics are used only to compute the exact RMS value only for comparison. the integral part is what we must use. if you have your signal function you don't need harmonics at all however it could be easily derived.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2016b

Asked:

on 10 Aug 2020

Edited:

on 11 Aug 2020

Community Treasure Hunt

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

Start Hunting!