Need to solve the following equation with three knowns and 2 unknowns.
Show older comments
W0 = Wi + (1 - L)*Hi*gi
W0 and L is an unknowns.
Wi, Hi, and gi are the knowns with 172 values per each.
I want to find W0 and L and their standard deviations as well.
I want to get each values comes for W0 and L when the program runs.
But when this code runs i got 0 for standard deviation of both W10 and L. Is this code is right? Can anyone help me to solve this?
The following code was written.
Wi = Wi;
Hi = H_BM;
gi = g_mean;
% Define the objective function
fun = @(x) norm(Wi + (1 - x(1))*Hi.*gi - x(2));
% Set initial guess for L and W0
x0 = [0, 0];
% Solve the optimization problem
x = fminsearch(fun, x0);
% Extract the values of L and W0
L = x(1);
L
W0 = x(2);
W0
% Calculate standard deviation of W0 and L
std_W0 = std(W0);
std_W0
std_L = std(L);
std_L
4 Comments
Rik
on 21 May 2024
You are calculating the standard deviation of a single value, not of the fit. fminsearch does not return the goodness of fit parameters with this syntax.
Prabhath Manuranga
on 21 May 2024
Rik
on 21 May 2024
Since you didn't post any data I can't show you what to do. You might want to try the fit function, since that will report the goodness of fit as the second output argument.
Prabhath Manuranga
on 21 May 2024
Answers (2)
H_BM = rand(100,1);
g_mean = rand(100,1);
Wi = rand(100,1);
Hi = H_BM;
gi = g_mean;
x = Hi.*gi;
y = Wi + Hi.*gi;
fitlm(x,y)
20 Comments
Prabhath Manuranga
on 21 May 2024
Edited: Prabhath Manuranga
on 21 May 2024
Torsten
on 21 May 2024
W0 = Estimate intercept
L = Estimate x1
SE = standard errors of the estimated parameters
For further details, consult the documentation of "fitlm".
Prabhath Manuranga
on 21 May 2024
Edited: Prabhath Manuranga
on 21 May 2024
Torsten
on 21 May 2024
You have a linear regression equation
W0 + (L - 1)*Hi*gi = Wi
because if you set
W0 = a, L - 1 = b, Hi*gi = xi, Wi = yi
, your equation reads
a + b*x = y
You want to estimate a and b to make
sum_{i=1}^{i=172} (yi-(a+b*xi))^2
minimal.
This can be done via "fitlm" as I posted.
There are easier methods to get the coefficient a and b, but you wrote you also want statistical information about reliability etc.
format long
M = readmatrix("data123.xlsx")
Wi = M(:,1);
H_BM = M(:,2);
g_mean = M(:,3);
Hi = H_BM;
gi = g_mean;
x = Hi.*gi;
y = Wi;
fitlm(x,y)
n = numel(Wi);
Mat = [ones(n,1),x];
rhs = y;
sol = Mat\rhs;
W0 = sol(1)
L = 1 + sol(2)
Prabhath Manuranga
on 24 May 2024
Edited: Prabhath Manuranga
on 24 May 2024
As far as I can see, W0 = numerator/denominator. Thus you already computed W0. What do you want to fit here ?
As far as I understood your problem, you have values for Wi, Hi and Gi and you want to compute L and W0 to minimize the sum of squared differences
sum_{i=1}^{i=172} (W0 + (L - 1)*Hi*gi - Wi)^2
That's what fitlm does if you call it as I did.
Prabhath Manuranga
on 24 May 2024
I don't understand how you come to the equation for W0, especially because you state that L on the right-hand side is also unknown.
Thus
sum(Pi .* (Wi + (1 - L) .* Hi .* gi))/sum(Pi)
cannot be used to determine W0.
In order to use fitlm, you must specify the independent variable x and the independent variable y in the regression model y = a*x + b. Given (in your case 172) realizations of x and y, fitlm will then try to adjust a and b.
Maybe the original regression equation was
P*W0 = P*W + (1-L)*g
?
Prabhath Manuranga
on 27 May 2024
Prabhath Manuranga
on 27 May 2024
If L and W0 were computed using equation (5) with a least-squares approach, then the two ways to do so are given in my answer where I used your data.
As said: I can't understand what the authors of the article did. E.g. I cannot find any reason why the authors multiplied equation (1) by Pi before summing it from i=1 to i=m and finally dividing by sum_i_Pi. Maybe they gave weights to the measurement data this way, but this should be mentionned in the article.
Prabhath Manuranga
on 27 May 2024
Edited: Prabhath Manuranga
on 27 May 2024
Note that the weights for "fitlm" must be the usual weights squared.
format long
M = readmatrix("data123.xlsx");
Wi = M(:,1);
H_BM = M(:,2);
g_mean = M(:,3);
Hi = H_BM;
gi = g_mean;
x = Hi.*gi;
y = Wi;
weights = 1./Hi / sum(1./Hi);
s = fitlm(x,y,'Weights',weights.^2)
Wohat = s.Coefficients.Estimate(1)
Lhat = 1 + s.Coefficients.Estimate(2)
n = numel(Wi);
Mat = [weights,weights.*x];
rhs = weights.*y;
sol = Mat\rhs;
W0 = sol(1)
L = 1 + sol(2)
syms W0 L
f = sum((weights.*(W0-(Wi + (1 - L)*Hi.*gi))).^2);
dfdW0 = diff(f,W0);
dfdL = diff(f,L);
sol = solve([dfdW0==0,dfdL==0],[W0,L])
W0 = vpa(sol.W0)
L = vpa(sol.L)
Prabhath Manuranga
on 28 May 2024
Edited: Prabhath Manuranga
on 28 May 2024
Torsten
on 28 May 2024
Yes.
Prabhath Manuranga
on 30 May 2024
Edited: Prabhath Manuranga
on 30 May 2024
Rename your variable "sum". "sum" is an inbuilt MATLAB function to sum elements, and you overwrite this function in your code.
If you are sure that sigma_e^2(V_n) equals what you compute as sum(n) (I doubt it because you don't sum anything when computing sum(n)), your Dw should be computed as
Dw = sqrt(sum(s(2:78)))
Here, I renamed you variable "sum" to "s" and used the MATLAB function "sum" to sum elements in an array.
According to the mathematical formulae, the sigma1 and beta1 are lower triangular matrices, and your "sum" variable is computed by summing both sigma1 and beta1 over their columns, adding the result and multiply it by (GM/alpha)^2.
Prabhath Manuranga
on 30 May 2024
Rupesh
on 23 May 2024
Hi Prabhat,
I understand that you want to calculate the values of W0 and L, and their standard deviations, from your given data using an optimization approach. The initial code calculates W0 and L but results in zero standard deviations because it operates on single scalar values instead of distributions (set of values) and single scalar values don’t have standard deviation associated with them.
To solve this, we can use a bootstrapping method. By repeatedly sampling your data and performing the optimization for each sample, we can obtain distributions of W0 and L. From these distributions, we can calculate the standard deviations.
Wi = Wi; % Your known values
Hi = H_BM; % Your known values
gi = g_mean; % Your known values
% Number of bootstrap samples
num_samples = 100;
% Arrays to store the results
L_values = zeros(1, num_samples);
W0_values = zeros(1, num_samples);
% Perform bootstrap sampling
for i = 1:num_samples
% Randomly sample indices with replacement
sample_indices = randsample(length(Wi), length(Wi), true);
% Sample the data
Wi_sample = Wi(sample_indices);
Hi_sample = Hi(sample_indices);
gi_sample = gi(sample_indices);
% Define the objective function for this sample
fun = @(x) norm(Wi_sample + (1 - x(1))*Hi_sample.*gi_sample - x(2));
% Set initial guess for L and W0
x0 = [0, 0];
% Solve the optimization problem for this sample
x = fminsearch(fun, x0);
% Store the results
L_values(i) = x(1);
W0_values(i) = x(2);
end
% Calculate mean and standard deviation of L and W0
L_mean = mean(L_values);
L_std = std(L_values);
W0_mean = mean(W0_values);
W0_std = std(W0_values);
% Display the results
disp(['L mean: ', num2str(L_mean)]);
disp(['L std: ', num2str(L_std)]);
disp(['W0 mean: ', num2str(W0_mean)]);
disp(['W0 std: ', num2str(W0_std)]);
This script repeatedly samples your data and runs the optimization to build up distributions for W0 and L, allowing for the calculation of their standard deviations. You can refer to below documentation to get clear understanding of inbuilt sample bootstrapping.
Hope it helps!
Thanks
Categories
Find more on Linear Predictive Coding in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





