Commit c1933f30 authored by Ferdinand Fischer's avatar Ferdinand Fischer
Browse files

Added function to find zeros in quantity.Discrete

parent 28c96f24
......@@ -240,6 +240,29 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
methods (Access = public)
function [z_idx, z_value] = findZeros(obj, optArg)
arguments
obj,
optArg.tol = 100*eps;
end
assert(obj(1).nargin == 1, "This function is only implemented for function dependent on one argument so far");
for i = 1:numel(obj)
data = obj(i).valueDiscrete();
zeros = find( abs(data) <= optArg.tol);
upCrossing = find( data(1:end-1) <= 0 & data(2:end) > 0);
downCrossing = find( data(1:end-1) >= 0 & data(2:end) < 0);
% todo: do a interpolation:
%ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
z_idx{i} = unique( [zeros; upCrossing; downCrossing]);
z_value{i} = data(z_idx{i});
end
end
function h = hash(obj)
%HASH create a SHA-1 hash value for this object
% h = hash(obj) will copmute the SHA-1 hash value based on the
......
......@@ -12,6 +12,17 @@ testCase.TestData.sym.t = t;
testCase.TestData.sym.sy = sy;
end
function testFindZeros(testCase)
x = quantity.Domain('x', linspace(0,1));
f = quantity.Discrete( sin( x.grid * 4 * pi ), 'domain', x);
[zIdx, zVal] = f.findZeros();
testCase.verifyEqual(zIdx{:}, [1 25, 50, 75, 100]');
end
function testMean(tc)
z = quantity.Domain("z", linspace(0, 1, 11));
x = quantity.Discrete(z.grid, "domain", z);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment