# Need to solve the following equation with three knowns and 2 unknowns.

136 views (last 30 days)
Prabhath Manuranga on 21 May 2024 at 6:00
Commented: Prabhath Manuranga on 30 May 2024 at 23:18
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;
Unrecognized function or variable '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 CommentsShow 2 older commentsHide 2 older comments
Rik on 21 May 2024 at 8:40
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 at 9:07
Please find the attached excel file for Wi, H_BM, and g_mean values.

Sign in to comment.

### Answers (2)

Torsten on 21 May 2024 at 8:51
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)
ans =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ ________ ______ __________ (Intercept) 0.41233 0.043102 9.5664 1.0575e-15 x1 1.2316 0.15371 8.013 2.3796e-12 Number of observations: 100, Error degrees of freedom: 98 Root Mean Squared Error: 0.294 R-squared: 0.396, Adjusted R-Squared: 0.39 F-statistic vs. constant model: 64.2, p-value = 2.38e-12
##### 20 CommentsShow 18 older commentsHide 18 older comments
Torsten on 30 May 2024 at 10:00
Edited: Torsten on 30 May 2024 at 12:34
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 at 23:18
Thank you sir.

Sign in to comment.

Rupesh on 23 May 2024 at 4:44
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
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on Choose a Solver 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!