I want to find constants in an equation that has got many parameters

3 views (last 30 days)
I have an equation. But it has got more than 3 known variables. Because of that, I cannot use curve fitting toolbox. For example my equation is z=h*(a*x+b*y/x^2+c*u) I have got 1600 values of z, h, x, y and u values in excel. These are known variables. a, b, c are constants. And I want to find best a, b and c values. How can I do it? I am very new in Matlab. Please help me.

Accepted Answer

Star Strider
Star Strider on 3 Sep 2016
Something like this should work:
% % % % % z=h*(a*x+b*y/x^2+c*u)
% % PARAMETER MAPPING: p(1) = a, p(2) = b, p(3) = c
% % VARIABLE MAPPING: v(:,1) = z, v(:,2) = h, v(:,3) = x, v(:,4) = y, v(:,5) = u
z = @(p,v) v(:,2).*(p(1).*v(:,3) + p(2).*v(:,4) ./ v(:,3).^2 + p(3).*v(:,5)); % Objective Function
v = [z(:) h(:) x(:) y(:) u(:)]; % Variable Array
P0 = [1; 2; 3]; % Choose Appropriate Initial Parameter Estimates
SSECF = @(p) sum((v(:,1) - z(p,v)).^2); % Sum-Squared-Error Cost Function
[abc, SSE] = fminsearch(SSECF, P0); % Estimate Parameters
a = abc(1)
b = abc(2)
c = abc(3)
NOTE: I tested this on random data and it ran without error. I have obviously not been able to test it with your data.
  17 Comments
Star Strider
Star Strider on 3 Aug 2021
No. Not for nonlinear problems. If you can transform it (for example logarithmically) and then linearise it, you can use linear methods to estimate the parameters, however that is not possible with the function posted. Also, don’t subscript ‘v’ inside the funciton. Pass it as whatever length it is suposed to be. It may also be encessary to use element-wise operations. I always default to that unless I know I need to use matrix operations.
Walter Roberson
Walter Roberson on 3 Aug 2021
Sometimes it is productive to take calculus approaches. Use symbolic variables for v and p and call SSECF() with the symbolic p to get a formula for SSECF.
At that point you would try to differentiate SSECF with respect to p(1) or p(2) or p(3) and then solve for the derivative being 0.
However... I ran those tests, and found that for this particular formula, MATLAB is not able to find a solution. WIth the number of exp() and additions involved, I would be surprised if there does turn out to be a closed form formula in this particular case.
Calculus is a useful tool in some cases, but not so much here.
.. Though I guess you could vpasolve() the derivative and hope that gave you something to work with.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!