Get accurate values for the variables in the equation that fit all the recorded data
1 view (last 30 days)
Show older comments
el-musleh
on 6 Sep 2021
Commented: Walter Roberson
on 6 Sep 2021
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/730909/image.png)
I came up with a equation as follow: (I don't know how to write equation for this system)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/730914/image.png)
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.
0 Comments
Accepted Answer
Walter Roberson
on 6 Sep 2021
Edited: Walter Roberson
on 6 Sep 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
[best_fval, best_idx] = min(fval)
A = best_params{best_idx}(1)
a = best_params{best_idx}(2)
b = best_params{best_idx}(3)
c = best_params{best_idx}(4)
d = best_params{best_idx}(5)
projected_4 = ((A*(sensor_1.^a).*(sensor_2.^b).*(sensor_3.^c)).^d);
[projected_4, sensor_4]
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
projected_4 = M*x
quadratic_residue = sum((projected_4 - sensor_4).^2)
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 Comments
Walter Roberson
on 6 Sep 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.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!