how to perform excel goal seek in Matlab?

62 visualizzazioni (ultimi 30 giorni)
Harsh Harsh
Harsh Harsh il 23 Mar 2023
Modificato: Harsh Harsh il 30 Mag 2023
I have a equation to find I_f as stated below
I_f = ((EC_n.*(tbal+I_b+DP+R))-(EC_in.*tbal)-(EC_b.*I_b))/(EC_f-EC_n);
here all the values are fixed/known other than that of I_b, to which i assign an initial value and i want to find the actual/optimum value using goal seek.
the conditions are
vap = 250-tbal;
h = vap - I_f -I_b; %h = 0 (this will be the target value in goal seek)(vap = I_f+I_b);
h = vap - I_f -I_b;
solving for (goalseek )h = y (where y = 0), (varying)I_b = 39.32214
which gives I_f = 35.67756

Risposte (2)

Joe Vinciguerra
Joe Vinciguerra il 23 Mar 2023
Simply rearrange the equations to solve for I_b when h=0, then calculate teh resulting I_f.
EC_n = 1.7;
EC_in = 1;
tbal = 175;
EC_f = 0.4;
EC_b = 6;
% I_b = 20; %(initialisation)
R = 0.2;
DP = 0.1;
% I_f = ((EC_n.*(tbal+I_b+DP+R))-(EC_in.*tbal)-(EC_b.*I_b))/(EC_f-EC_n);
% y = 0;
% vap = 250-tbal;
% h = vap - I_f -I_b;
% solving for (goalseek )h = y (where y = 0), (varying)I_b = 39.32214
% which gives I_f = 35.67756
% rearrange the equations to solve for I_b:
% h = vap - I_f -I_b;
% 0 = vap - I_f -I_b;
% 0 = 250 - tbal - (EC_n*tbal + EC_n*I_b + EC_n*DP + EC_n*R - EC_in*tbal - EC_b*I_b) / (EC_f-EC_n) - I_b;
% I_b*EC_f - I_b*EC_n + EC_n*tbal + EC_n*I_b + EC_n*DP + EC_n*R - EC_in*tbal - EC_b*I_b = (250 - tbal)*(EC_f-EC_n);
% I_b*EC_f - I_b*EC_n + EC_n*I_b - EC_b*I_b = (250 - tbal)*(EC_f-EC_n) - EC_n*tbal - EC_n*DP - EC_n*R + EC_in*tbal;
% I_b(EC_f - EC_n + EC_n - EC_b) = (250 - tbal)*(EC_f-EC_n) - EC_n*tbal - EC_n*DP - EC_n*R + EC_in*tbal
I_b = ((250 - tbal)*(EC_f-EC_n) - EC_n*tbal - EC_n*DP - EC_n*R + EC_in*tbal )/(EC_f - EC_n + EC_n - EC_b)
I_b = 39.3768
% solve for I_f:
I_f = ((EC_n.*(tbal+I_b+DP+R))-(EC_in.*tbal)-(EC_b.*I_b))/(EC_f-EC_n
I_f = 35.6232
  2 Commenti
Harsh Harsh
Harsh Harsh il 29 Mar 2023
but the values of the constants provided here actually keep changing every iteration. this is a part of a bigger model and i need to create a automatic loop/function to calcuate the I_f for every value of the other variables.
Joe Vinciguerra
Joe Vinciguerra il 29 Mar 2023
That doesn't matter for the problem that was presented.
% a bunch of random input data:
n = 10;
EC_n = [1.7 * 2*rand(1, n)];
EC_in = [1 * 2*rand(1, n)];
tbal = [175 * 2*rand(1, n)];
EC_f = [0.4 * 2*rand(1, n)];
EC_b = [6 * 2*rand(1, n)];
R = [0.2 * 2*rand(1, n)];
DP = [0.1 * 2*rand(1, n)];
% solve for I_b and I_f:
for i = 1:n
I_b(i) = ((250 - tbal(i))*(EC_f(i)-EC_n(i)) - EC_n(i)*tbal(i)- EC_n(i)*DP(i) - EC_n(i)*R(i) + EC_in(i)*tbal(i))/(EC_f(i) - EC_n(i) + EC_n(i) - EC_b(i));
I_f(i) = ((EC_n(i).*(tbal(i)+I_b(i)+DP(i)+R(i)))-(EC_in(i).*tbal(i))-(EC_b(i).*I_b(i)))/(EC_f(i)-EC_n(i));
end

Accedi per commentare.


Cris LaPierre
Cris LaPierre il 23 Mar 2023
Modificato: Cris LaPierre il 23 Mar 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:
  1. the equation for h: vap-I_f-I_b = 0
  2. 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);
EquationProblem : Solve for: I_b, I_f equation1: -I_f - I_b == -75 equation2: I_f - 3.3077*I_b == -94.6231
% Solve problem
[solution,objectiveValue,reasonSolverStopped] = solve(problem,initialPoint6);
Solving problem using lsqlin.
% Display results
solution
solution = struct with fields:
I_b: 39.3768 I_f: 35.6232
reasonSolverStopped
reasonSolverStopped =
EquationSolved
objectiveValue
objectiveValue = struct with fields:
equation1: 0 equation2: -1.4211e-14
% Clear variables
clearvars I_b6 I_f6 initialPoint6 reasonSolverStopped objectiveValue
And you can access the final values like this:
solution.I_b
ans = 39.3768
solution.I_f
ans = 35.6232
  2 Commenti
Harsh Harsh
Harsh Harsh il 29 Mar 2023
Thank you for this answer, but the issue is that in my model, the constants are actually variables.
EC_n , EC_in , tbal , EC_f , EC_b , R , DP , vap keep changing every iteration
Cris LaPierre
Cris LaPierre il 29 Mar 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.

Accedi per commentare.

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by