how can we increase a value until we get answer
조회 수: 3 (최근 30일)
이전 댓글 표시
clc
clear all
% -------------------------------------data input-----------------------------------
alpha = [510; 310; 78; 120; 350; 490; 53; 150; 170; 149];
beta = [7.20; 7.85; 7.98; 7.10; 7.30; 6.90; 8.01; 7.30; 7.42; 7.16];
gamma = [0.00142; 0.00194; 0.00482; 0.00321; 0.00210; 0.00178; 0.00212; 0.00350; 0.00267; 0.00390];
delta = [0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001];
% Pmin =[150; 100; 50; 50; 100; 100; 100; 100; 100; 100];
% Pmax =[600; 400; 200; 200; 350; 500; 300; 300; 300; 300];
n = 10;
Pr = 2500;
L = 8.6;
%--------------------------------------Program start here--------------------------------
while eps >= 0.001
L =
syms P
% P = sym('P');
% L = sym('L');
F = sym('F');
Lambda = sym('Lambda');
% n = length(alpha);
for i = 1:n
F(i) = alpha(i) + beta(i).*P + gamma(i).*P.^2 + delta(i).*P.^3;
disp(F(i))
Lambda = diff(F(i),P) - L == 0;
disp(Lambda);
S = solve(Lambda, P);
disp(S)
S1 = max(S)
% disp(S1)
X(i) = S1
% W = vpa(X(i),6)
double(X)
end
Pt = sum(X)
disp(Pt)
double(Pt)
eps = 2500 - double(Pt)
double(eps)
end
i want to increase L until i get eps >=0.01
for example i wanna increase L 0.01.....l=8.01,8.02,8.03
댓글 수: 1
Image Analyst
2021년 12월 28일
eps is a built-in function that gives the smallest number that can be represented. It's value depends on the value of the variable. Like eps around 2 is different than eps around 10^30. That is why eps() can take a input number.
You should not use eps as the name of your own custom variable because it is a built-in function.
채택된 답변
DGM
2021년 12월 28일
편집: DGM
2021년 12월 28일
This can be simplified, but let's start with the given approach.
format long
alpha = [510; 310; 78; 120; 350; 490; 53; 150; 170; 149];
beta = [7.20; 7.85; 7.98; 7.10; 7.30; 6.90; 8.01; 7.30; 7.42; 7.16];
gamma = [0.00142; 0.00194; 0.00482; 0.00321; 0.00210; 0.00178; 0.00212; 0.00350; 0.00267; 0.00390];
delta = [0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001];
n = 10;
Pr = 2500;
L = 8.6;
dL = 0.01;
maxep = 0.001;
syms P
F = sym('F',[1 n]);
X = sym('X',[1 n]);
ep = 1; % eps() is a built-in function; avoid overloading it
while ep >= maxep
for k = 1:n
F(k) = alpha(k) + beta(k).*P + gamma(k).*P.^2 + delta(k).*P.^3;
Lambda = diff(F(k),P) - L == 0;
S = solve(Lambda, P);
X(k) = max(S);
end
Pt = sum(X);
ep = 2500 - double(Pt)
L = L+dL;
end
I doubt that negative eps is intended, so if not, you'll have to adjust the step size accordingly. Also, it should suffice to solve for P first instead of doing it every time. The following is an example of both.
% note that the solution doesn't depend on alpha
beta = [7.20; 7.85; 7.98; 7.10; 7.30; 6.90; 8.01; 7.30; 7.42; 7.16];
gamma = [0.00142; 0.00194; 0.00482; 0.00321; 0.00210; 0.00178; 0.00212; 0.00350; 0.00267; 0.00390];
delta = [0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001];
n = 10;
Pr = 2500;
L = 8.6;
dL = 0.01;
maxep = 0.001;
ep = 1; % eps() is a built-in function; avoid overloading it
while ep >= maxep
X1 = (sqrt(gamma.^2 + 3*L*delta - 3*beta.*delta) - gamma)./(3*delta);
X2 = -(sqrt(gamma.^2 + 3*L*delta - 3*beta.*delta) + gamma)./(3*delta);
ep = 2500 - double(sum(max(X1,X2)));
if ep < 0
L = L-dL;
dL = dL/2;
L = L+dL;
ep = 1; % reset to dummy value
elseif ep > maxep
L = L+dL;
end
disp([dL L ep])
end
This can be simplified further using the available numerical solvers.
% note that the solution doesn't depend on alpha
beta = [7.20; 7.85; 7.98; 7.10; 7.30; 6.90; 8.01; 7.30; 7.42; 7.16];
gamma = [0.00142; 0.00194; 0.00482; 0.00321; 0.00210; 0.00178; 0.00212; 0.00350; 0.00267; 0.00390];
delta = [0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001];
n = 10;
Pr = 2500;
L = 8.6;
eptarget = 0.0005; % some specific value instead of a maximum
Ltarget = fzero(@(L) findep(beta,gamma,delta,L)-eptarget,L) % find L
findep(beta,gamma,delta,Ltarget) % test L
function ep = findep(beta,gamma,delta,L)
X1 = (sqrt(gamma.^2 + 3*L*delta - 3*beta.*delta) - gamma)./(3*delta);
X2 = -(sqrt(gamma.^2 + 3*L*delta - 3*beta.*delta) + gamma)./(3*delta);
ep = 2500 - double(sum(max(X1,X2)));
end
댓글 수: 4
DGM
2021년 12월 29일
If you feel that the question has been satisfactorily answered, you can click Accept. That way the question is moved to the "answered" queue where it may be more attractive to other people searching for similar information in the future.
추가 답변 (0개)
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!