Azzera filtri
Azzera filtri

Get accurate values for the variables in the equation that fit all the recorded data

1 visualizzazione (ultimi 30 giorni)
What to do/use in MATLAB to make an system of nonlinear equation that takes multiple input values to give the exact same output value.
The image below shows a set of data as a sample.
I came up with a equation as follow: (I don't know how to write equation for this system)
And in MATLAB I used vpasolve tool and gives very close values to the output sensor but only one value match.
syms A a b c d;
eqns= ((A*(sensor_1.^a).*(sensor_2.^b).*(sensor_3.^c)).^d) == sensor_4;
[A a b c d] = vpasolve(eqns(1,1), [A a b c d]);
Can I use a tool that goes through all sets of data to define the value of the variables? Is there a tool that can give output match the output sensor? What tool should I use to get values for the variables that can fit all the sets of data?
Any help is totally appreciated. Thanks a lot.

Risposta accettata

Walter Roberson
Walter Roberson il 6 Set 2021
Modificato: Walter Roberson il 6 Set 2021
format long g
sensor_1 = 6.0*ones(10,1);
sensor_2 = [12.5;17.5;22.5;27.5;32.5;12.5;17.5;22.5;27.5;32.5];
sensor_3 = [9.1;12.4;15.7;18.9;22.2;6.3;12.4;15.7;18.9;22.2];
sensor_4 = [168.3*ones(5,1); 159.9*ones(5,1)];
syms A a b c d real;
eqns = ((A*(sensor_1.^a).*(sensor_2.^b).*(sensor_3.^c)).^d) == sensor_4;
residue = sum((lhs(eqns) - rhs(eqns)).^2);
f = matlabFunction(residue, 'vars', {[A,a,b,c,d]});
N = 50;
opts = optimoptions(@fmincon);
opts.Display = 'none';
lb = [0, -inf, -inf, -inf, -inf];
ub = [800,inf,inf,inf,inf];
for K = 1 : N
guess = [rand*500,randn*5,randn*5,randn*5,rand*10];
%mat2str(guess)
%f(guess)
try
[best_params{K}, fval(K)] = fmincon(f, guess, [], [], [], [], lb, ub, [], opts);
catch ME
best_params{K} = guess;
fval(K) = f(guess);
fprintf('fmincon failed at initial point [%g,%g,%g,%g,%g], where f = %g\n', guess, fval(K));
end
end
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.397198e-16.
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 5.769084e-17.
[best_fval, best_idx] = min(fval)
best_fval =
148.753415608039
best_idx =
19
A = best_params{best_idx}(1)
A =
318.324510592058
a = best_params{best_idx}(2)
a =
4.41564530002228
b = best_params{best_idx}(3)
b =
-0.319969220189679
c = best_params{best_idx}(4)
c =
0.290000822280844
d = best_params{best_idx}(5)
d =
0.378668237537184
projected_4 = ((A*(sensor_1.^a).*(sensor_2.^b).*(sensor_3.^c)).^d);
[projected_4, sensor_4]
ans = 10×2
166.451931020549 168.3 165.325748376833 168.3 164.577265129137 168.3 163.929606356834 168.3 163.509140241818 168.3 159.864279044102 159.9 165.325748376833 159.9 164.577265129137 159.9 163.929606356834 159.9 163.509140241818 159.9
If you are hoping to have equations that "exactly" predict sensor_4, then you cannot use that model.
M = [sensor_2.^3, sensor_2.^2, sensor_3.^2, sensor_2, sensor_3, ones(numel(sensor_2),1)];
b = sensor_4;
x = M\b
x = 6×1
-0.00357438423645327 0.0356157635468336 0.541871921182194 -3.63500000000078 -5.34482758620575 218.919273399015
projected_4 = M*x
projected_4 = 10×1
168.3 164.1 164.1 164.1 164.1 159.9 164.1 164.1 164.1 164.1
quadratic_residue = sum((projected_4 - sensor_4).^2)
quadratic_residue =
141.12
If you compare quadartic_residue (141) to best_fval (148) you will see that a cubic fit is a slightly better model than the more elaborate one you came up with. It is obviously still not a good model. (I did not use sensor_1 in the calculation as the values are constant)
  2 Commenti
el-musleh
el-musleh il 6 Set 2021
I tried your answer on the entire completed data and I got the following:
fmincon failed at initial point [178.434,1.17997,-4.17586,-6.37978,7.11129], where f = Inf
fmincon failed at initial point [312.286,1.44691,1.97658,-4.35281,3.48785], where f = Inf
fmincon failed at initial point [365.525,-5.17212,6.69777,-4.8457,5.88209], where f = Inf
fmincon failed at initial point [183.078,2.56008,0.0567709,-0.219943,8.77049], where f = Inf
fmincon failed at initial point [176.571,-0.234397,13.4151,-5.73345,9.72958], where f = Inf
fmincon failed at initial point [310.139,7.36257,-11.3755,-8.16645,9.74802], where f = Inf
fmincon failed at initial point [325.675,-3.27384,-1.48174,-7.48459,2.68439], where f = Inf
fmincon failed at initial point [128.923,-3.62899,-4.33243,-2.10923,1.21658], where f = Inf
fmincon failed at initial point [442.077,-4.94217,9.08971,-1.87218,0.474015], where f = Inf
fmincon failed at initial point [446.816,-7.29521,-2.90855,-9.15075,1.95477], where f = Inf
fmincon failed at initial point [357.839,3.48183,-0.563578,-0.194119,5.60713], where f = Inf
The equation logical array is showing 0. The fval are either 0's or Inf.
Is there anything I can do about it? I am open to using any method or tool to solve it. so any suggestions? Thanks.
Walter Roberson
Walter Roberson il 6 Set 2021
In the line
guess = [rand*500,randn*5,randn*5,randn*5,rand*10];
you could reduce the multipliers to constrain the range of the guesses.

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by