how to perform excel goal seek in Matlab?
Mostrar comentarios más antiguos
I have a equation to find a missing variable 'If'
all the other values are fixed/known other than that of another variable 'Ib', to which i assign an initial value and i want to find the actual/optimum value using goal seek.
the conditions are
vap = 250-tb;
h = vap - If -Ib; %h = 0 (this will be the target value in goal seek)(vap = If+Ib);
h = vap - If -Ib;
solving for (goalseek)h = y (where y = 0), (varying)Ib = 39.32214
which gives If = 35.67756
Respuesta aceptada
Más respuestas (1)
Cris LaPierre
el 23 de Mzo. de 2023
Editada: Cris LaPierre
el 23 de Mzo. de 2023
If you have the optimization toolbox, I suggest using the Optimization Live Editor Task (Problem based) to solve this. You could do this programatically, but this allows you to use a user interface to set up the problem. If you are new to this, it can be a bit overwhelming at first, so I'll walk you through it.
Begin by opening a live script and defining all your constants.
EC_n = 1.7;
EC_in = 1;
tbal = 175;
EC_f = 0.4;
EC_b = 6;
R = 0.2;
DP = 0.1;
vap = 250-tbal;
Then, add the Optimize Live Editor Task and select Problem Based. Once that opens, add I_b and I_f as optimization variables (these are the ones whose values change).
For Define problem, select Solve equations.
Enter 2 equations:
- the equation for h: vap-I_f-I_b = 0
- and the equation for I_f: I_f = (EC_n.*(tbal+I_b+DP+R)-EC_in.*tbal-EC_b.*I_b)/(EC_f-EC_n)

Finally, click Solve problem. The results are captured in the structure solution, and are also displayed in the task output.
Solve for:
I_b, I_f
equation1:
-I_f - I_b == -75
equation2:
I_f - 3.3077*I_b == -94.6231
Solving problem using lsqlin.
solution = struct with fields:
I_b: 39.3768
I_f: 35.6232
reasonSolverStopped =
EquationSolved
objectiveValue = struct with fields:
equation1: 0
equation2: -1.4211e-14
Once solved, you can export the live task to code, which looks like this.
% Create optimization variables
I_b6 = optimvar("I_b");
I_f6 = optimvar("I_f");
% Set initial starting point for the solver
initialPoint6.I_b = repmat(20,size(I_b6));
initialPoint6.I_f = zeros(size(I_f6));
% Create problem
problem = eqnproblem;
% Define problem equations
problem.Equations.equation1 = vap - I_f6 -I_b6 == 0;
problem.Equations.equation2 = I_f6 == (EC_n.*(tbal+I_b6+DP+R)-EC_in.*tbal-EC_b.*I_b6)/(EC_f-EC_n);
% Display problem information
show(problem);
% Solve problem
[solution,objectiveValue,reasonSolverStopped] = solve(problem,initialPoint6);
% Display results
solution
reasonSolverStopped
objectiveValue
% Clear variables
clearvars I_b6 I_f6 initialPoint6 reasonSolverStopped objectiveValue
And you can access the final values like this:
solution.I_b
solution.I_f
2 comentarios
Harsh Harsh
el 29 de Mzo. de 2023
Cris LaPierre
el 29 de Mzo. de 2023
That's not a problem. Now you know what to do. Any variables that change must be defined as an optimization variable. I think the rest of the problem stays the same.
As an aside, it was my observation that, when using goal seek in Excel, none of the other variables actually changed value, so the results I came up with in MATLAB matched the results in Excel.
Categorías
Más información sobre Introduction to Installation and Licensing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!