How to setup Optimisation ?

Hello,
I am trying to find multiple variables in equations. Suppose I have a cost factor, for different products, that changes annually. However, the form of the equation remains the same. The equation would be:
p=A^(x(1))*(1-B^(-x(2)))*(1-C*x(3))
The Variable A,B.C change with each year, but they are known and in an array. The same applies to p. Therefore my variables I am looking for are the ones in the x array. As example for the variables would be (where each row represent a year):
A=[1.25;1.25;1.25,1.15]
B=[1.5;1.4;1.3;1.2]
C=[0;0;1;1]
p=[1.53;1.51;1.45;1.21]
As for the Initial guess (for the optimisation) my guess would be for:
xo=[2;10;0.01]
How do i setup the optimisation to find the variables of x ? From the Matlab documentation it seems the LSQCURVEFIT is the approch I have to use, however I do not find any Information regarding the changing variables of A,B,C. I tried to solve this problem with the solve function, where i have 4 equations, but because p is rounded, it seems that the solve function is unable to solve for x.
Any help would be really appreciated and best regards

3 commentaires

John D'Errico
John D'Errico le 30 Mai 2021
The reason you cannot use solve is you have 3 unknowns and effectively 4 equations. This is an overdetermined problem, and there will not be an exact solution.
As for what you mean by changing variables A,B,C, I have no clue. They don't change. They are fixed.
Perhaps your problem is you do not understand how to pass in those variables as vectors into your function. Learn about function handles and how to use them.
Torsten
Torsten le 30 Mai 2021
Modifié(e) : Torsten le 30 Mai 2021
You have 4 equations to determine 3 parameters x(1),x(2) and x(3):
p - A.^x(1).*(1-B.^x(2)).*(1-C*x(3)) = 0
Since the number of equations is bigger than the number of unknowns, those problems are usually solved in the least-squares sense.
Use lsqnonlin to do this. In principle, you only have to include the arrays for p,A,B and C and the above equation in a function for the objective and to call lsqnonlin. The examples provided should show you how to proceed.
Thanks for the answers,
lsqnonlin solved my problem. In case someone who has a similiar problem here is the solution I found
A=[1.25;1.25;1.25;1.15];
B=[1.5;1.4;1.3;1.2];
C=[0;0;1;1];
p=[1.53;1.51;1.45;1.21];
xo=[2;10;0.01];
fun= @(x) funtest(A,B,C,p,x);
xsol=lsqnonlin(fun,xo);
% Plot test
papprox=A.^xsol(1).*(1-B.^(-xsol(2))).*(1-C*xsol(3));
plot(p,'--k')
hold on
plot(papprox,'r')
function test = funtest(A,B,C,p,x)
test=p - A.^x(1).*(1-B.^(-x(2))).*(1-C*x(3));
end

Connectez-vous pour commenter.

Réponses (0)

Catégories

Commenté :

le 31 Mai 2021

Community Treasure Hunt

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

Start Hunting!

Translated by