how to perform excel goal seek in Matlab?
37 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Joe Vinciguerra
2023-3-23
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)
% solve for I_f:
I_f = ((EC_n.*(tbal+I_b+DP+R))-(EC_in.*tbal)-(EC_b.*I_b))/(EC_f-EC_n
1 个评论
Joe Vinciguerra
2023-3-29
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
更多回答(1 个)
Cris LaPierre
2023-3-23
编辑:Cris LaPierre
2023-3-23
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1334425/image.png)
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 个评论
Cris LaPierre
2023-3-29
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!