Skip to content

Instantly share code, notes, and snippets.

@tbnbooij
Last active January 14, 2018 14:38
Show Gist options
  • Save tbnbooij/d681f6ddd3ddef67c7fa7f6621b5721f to your computer and use it in GitHub Desktop.
Save tbnbooij/d681f6ddd3ddef67c7fa7f6621b5721f to your computer and use it in GitHub Desktop.
A small MATLAB function that allows you to quickly retrieve the relative differences between model outcomes and measurement values.
function [ differences ] = drdiff(model_in, model_out, measured_in, measured_out)
%DRDIFF Discrete relative difference
%drdiff(model_in, model_out, measured_in, measured_out)
%Requirements:
%* model_in and model_out are two vectors of the same length
%* measured_in and measured_out are two vectors of the same length
%
%Returns:
%A vector with the relative difference for each measurement point.
differences = [];
for i=1:length(measured_in)
x = measured_in(i);
d = sign(model_in - x);
for k=1:length(d)
if (k ~= length(d))
if (d(i) + d(k+1) == 0 || d(i) + d(k+1) == 1 || d(i) + d(k+1) == -1)
boundA = k;
boundB = k+1;
break
end
end
end
slope = (model_out(boundB) - model_out(boundA))/(model_in(boundB) - model_in(boundA));
approx = model_out(boundA) + slope*abs(x - model_in(boundA));
relDiff = abs((measured_out(i) - approx)/approx);
if (relDiff ~= Inf && ~isnan(relDiff))
differences = [differences relDiff];
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment