-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildDesignMatrix.m
More file actions
43 lines (35 loc) · 1.07 KB
/
Copy pathBuildDesignMatrix.m
File metadata and controls
43 lines (35 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function [X, uX] = BuildDesignMatrix(modelStr, params, x_i, ux_i)
% Create design matrix from model string
%
% modelStr : string like 'a + b*x + c*sin(x)'
% params : string array of parameter names, e.g. ["a","b","c"]
% x_i : column vector of x values
% ux_i : scalar or vector of uncertainties in x (set to 0 to skip)
syms x
expr = str2sym(modelStr);
n = length(x_i);
p = length(params);
X = zeros(n, p);
uX = zeros(n, p);
for k = 1:p
sym_param = sym(params(k));
% Basis function (design matrix column)
col_expr = diff(expr, sym_param);
% Uncertainty propagation: d/dx of basis function, times u_x
dcol_dx = diff(col_expr, x);
% Evaluate basis function
if isnumeric(col_expr)
X(:, k) = double(col_expr) * ones(n, 1);
else
f = matlabFunction(col_expr, 'Vars', x);
X(:, k) = f(x_i);
end
% Evaluate uncertainty column
if isnumeric(dcol_dx) || dcol_dx == 0
uX(:, k) = 0;
else
f = matlabFunction(dcol_dx, 'Vars', x);
uX(:, k) = abs(f(x_i)) .* ux_i(:);
end
end
end