필터 지우기
필터 지우기

how to perform excel goal seek in Matlab?

조회 수: 34 (최근 30일)
Harsh Harsh
Harsh Harsh 2023년 3월 23일
편집: Harsh Harsh 2024년 5월 24일
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

채택된 답변

Joe Vinciguerra
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)
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
  댓글 수: 1
Joe Vinciguerra
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
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:
  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
Harsh Harsh
Harsh Harsh 2023년 3월 29일
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 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 CenterFile Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by