Too many input arguments error
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello! I'm new to MATLAB and this is the first time i use this kind of software. Currently I'm trying to solve an optimization equation. To solve this equation and find the maximum profit, i need to implemented some algorithm. Here is the solution algorithm for this equation :
- Set fixed parameters.
- Set n=1
- Compute x(1) using the initial value of g and x : x(1) = f(n, g initial, x initial)
- Compute g(1) using the initial value : g(1) = f(n, x(1), g initial)
- Repeat step 3-4 until x and g converge ((i.e., the values of x and g do not change significantly between iterations).
- Compute the total profit for the current n
- Set n=2
- Repeat step 3-6
- Compare the profit fot n=1 and n=2, if the prpofit for n=1 < n=2, continue to step 10. If not, stop.
- Set n = n+1 (next value of n)
- Repeat step 3-9
I try to write the MATLAB code with the help of some source i found here and I also try to use chat gpt. I'm not sure my code are correct, but here is my code :
function [optimal_n, optimal_x, optimal_g, optimal_profit] = optimize_profit()
L = 4;
w = 0.064;
ww = 2;
R = 250;
Kf = 750;
cf = 0.1;
m = 0.2;
Kp = 500;
hp = 0.05;
Kr = 100;
hr = 0.1;
k = 6.87;
q = 0.12;
a = 80;
b = 0.2;
Pv = 1;
pf = 2;
pp = 3;
pr = 5;
c = 30;
S = 100;
Sv = 200;
v = 150;
vp = 200;
y = 1000;
z = 120;
f = 0.9;
l = 0.15;
Ik = 0.1;
M = 0.5;
bk = 0.2;
% Initial parameters
n = 1;
tol = 1e-5; % Tolerance for comparing profit
max_iterations = 100; % Max number of iterations for convergence
% Define your functions for x and g here
xa = @(n, x, g) (2^(-1 / (1 - b)) * c *x* (-pf - pp) * (a * (1 - b) * g * x^2 * y)^(1 / (1 - b)) / (L + g * L * y)^(1 / (1 - b))) ...
- 2^(-1 / (1 - b)) * pr * x * ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(1 / (1 - b)) ...
- ((1 + c) * x * Kr * n + Kf + Kp) / n ...
- ((1 + c) * 2^(-1 / (1 - b)) * c * x * (-v * n - vp) * (a * (1 - b) * g * x^2 * y)^(1 / (1 - b)) / (n * (L + g * L * y)^(1 / (1 - b)))) ...
- (1 + c) * (-S * n^2 - Sv) / (n^2 * (L + g * L * y)^(1 / (1 - b))) ...
+ x * (2^(-1 / (1 - b)) * Pv * w * ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(1 / (1 - b)) + (cf * f + b * k * m) * (k * n * x + k / q * (log(1 + z) - log(1 + exp(-n * q * x) * z))) / f*ww) ...
- (1 + c) * x^2 * (2^(-(1 + 2 / (1 - b))) * f*ww * (g + hp) * (-1 + n) * ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(2 / (1 - b)) ...
* (2^(-1 / (1 - b)) * ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(1 / (1 - b)) + 2^(1 - 1 / (1 - b)) * a * g * x^2 * y * ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(-1 + 1 / (1 - b)) / (L * (1 + g * y))) ...
+ (cf * f + b * k * m) * (k * n - exp(-n * q * x) * k * z / (1 + exp(-n * q * x) * z))) / (f*ww * x);
xb = @(n, x, g) (2^(1 - 1 / (1 - b)) * a * g * pr * y * ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(-1 + 1 / (1 - b))) ...
+ (2^(1 - 1 / (1 - b)) * a * c * g * (pf / x + pp / x) * x * y * ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(-1 + 1 / (1 - b))) ...
- (2^(1 - 1 / (1 - b)) * a * g * (M - x) * x * y * ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(-1 + 1 / (1 - b))) ...
+ (2 * a * g * l * pr * y * ((-(a * (b - 1) * g * x^2 * y) / (L + g * L * y))^(1 / (1 - b)))^b) ...
+ (2^(1 - 1 / (1 - b)) * a * g * (v / x + vp / (n * x)) * x * y * ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(-1 + 1 / (1 - b))) ...
+ (2^(1 - 2 / (1 - b)) * a * g * (g + hp) * (-1 + n) * y * ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(-1 + 2 / (1 - b)) * (-1 / R + 2^(-1 / (1 - b)) * x * ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(1 / (1 - b)))) / (L * (1 + g * y));
xc = @(n, x, g) 4 * a^2 * (b - 1) * b * g^2 * l * pr * x^2 * y^2 ...
* ((-(a * (b - 1) * g * x^2 * y) / (L + g * L * y))^(1 / (1 - b)))^(-1 + b) ...
* (-(a * (b - 1) * g * x^2 * y) / (L + g * L * y))^(-1 + 1 / (1 - b));
xd = @(n, x, g) 2^(1 - 1 / (1 - b)) * a * g * Pv * w * y ...
* ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(-1 + 1 / (1 - b));
xe = @(n, x, g) 2^(1 - 2 / (1 - b)) * a * g * (g + hp) * y ...
* ((a * (1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(-1 + 2 / (1 - b));
xf = @(n, x, g) 4 * a * (b - 1) * g * (g + hr) * x * y ...
* (-(a * (b - 1) * g * x^2 * y) / (L + g * L * y))^(-1 + 1 / (1 - b));
xg = @(n, x, g) 2^(-1 / (1 - b))*((a*(1 - b) * g * x^2 * y) / (L * (1 + g * y)))^(1 / (1 - b));
xh = @(n, x, g) L*(1+(g*y));
xj = @(n, x, g) (1-b)*(L+(g*L*y))^2;
xk = @(n, x, g) f*L*ww*(1+(g*y));
xl = @(n, x, g) L*R*1+(g*y);
xm = @(n, x, g) (1-b)*(L+(g*L*y));
xn = @(n, x, g) (xb(n, x, g) / xh(n, x, g))+xg(n, x, g)-(1+c)*((-xc(n, x, g) / xj(n, x, g))+(xd(n, x, g) / xk(n, x, g))+(xe(n, x, g) / xl(n, x, g))+(xf(n, x, g)/xm(n, x, g)));
x_func = @(n, g) (xa(n, g)/xn(n, g))^(1/3); % Example T function
ga = @(n, x, g) -(2*a*l*p*r*x*y*(a*(b-1)*x^(2)*y)^(b/(1-b)));
gb =@(n, x, g) 2^(-1/(1-b))*(cpf+cpp-pr)*(a*(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(1-b));
gl =@(n, x, g) g^2*((c-1)*(a*(1-b)*x^(2)*y)^(2/(1-b))*(2^((b-3)/(1-b))*(n-1)+2^((b-3)/(1-b))));
gm =@(n, x, g) (1+c)*g^(1+b)*2^(2/(1-b))*(n-1)*(a*(1-b)*x^(2)*y)^(-1)*(-a*(1-b)*g*x^(2)*y^(2)+a*(1-b)*x^(2)*y);
gn =@(n, x, g) 2*a*b*g^(2*b-1)*l*pr*x*y*(a*(b-1)*x^(2)*y)^((2*b-1)/(1-b))*(-a*(1-b)*g*x^(2)*y^(2)+a*(1-b)*x^(2)*y);
ghh =@(n, x, g) (L+(g*L*y))^((2-b)/(1-b));
gjj =@(n, x, g) (L+(g*L*y))^(1/(1-b));
gkk =@(n, x, g) (L+(g*L*y))^(2/(1-b));
gll =@(n, x, g) (1-b)*(L+(g*L*y))^((2-b)/(1-b));
gmm =@(n, x, g) (1-b)*(L+(g*L*y))^(2/(1-b));
glla =@(n, x, g) (gh/ghh)-((gj+gk)/gjj)+(gl/(R*x*gkk))-(gm/(R*x*gmm))+(gn/gll);
g_func =@(n, x, g) gdda/glla; % Example k function
profit_func =@(n, x, g) (pr / x) * ((a * (1 - b) * x^2) / (2 * L) * (y * g) / (1 + y * g))^(1 / (1 - b)) ...
- (c + 1) * (Kr / x + Kp / (n * x) + Kf / (n * x) ...
+ (hr + g) / x * (2 * x * (-a * (b - 1) * g * x^2 * y / (L + g * L * y))^(1 / (1 - b))) ...
+ (hp + g) / (2 * R * x) * ((a * (1 - b) * x^2) / (2 * L) * (y * g) / (1 + y * g))^(2 / (1 - b)) ...
+ ((hp + g) * (n - 1)) / (2 * x) * ((a * (1 - b) * x^2) / (2 * L) * (y * g) / (1 + y * g))^(2 / (1 - b)) ...
* (x * ((a * (1 - b) * x^2) / (2 * L) * (y * g) / (1 + y * g))^(1 / (1 - b)) - 1 / R) ...
+ S / x^2 + (v / x + vp / (n * x)) * ((a * (1 - b) * x^2) / (2 * L) * (y * g) / (1 + y * g))^(1 / (1 - b)) ...
+ Sv / (n * x)^2 + (Pv * w) / (x * f * ww) * ((a * (1 - b) * x^2) / (2 * L) * (y * g) / (1 + y * g))^(1 / (1 - b)) ...
+ ((cf * f + m * bk) / (x * f * ww)) * (k * n * x + k / q * (log(1 + z * exp(-q * n * x)) - log(1 + z))) ) ...
+ c * (pp / x + pf / x) * ((a * (1 - b) * x^2) / (2 * L) * (y * g) / (1 + y * g))^(1 / (1 - b)) ...
+ (pr / x) * l * (2 * a * g * x^2 * y * ((-a * (b - 1) * g * x^2 * y / (L + g * L * y))^(1 / (1 - b)))^b / (L + g * L * y) ...
- ((a * (1 - b) * x^2) / (2 * L) * (y * g) / (1 + y * g))^(1 / (1 - b)) * (M - x));
optimal_profit = 0;
optimal_n = n;
optimal_x = 0;
optimal_g = 0;
while true
% Step 2: Calculate initial x(1) using initial n and g(0)
x_prev = x_func(n, 0, 0); % Assume initial g(0) = 0
% Step 3: Calculate initial g(1) using x(1)
g_prev = g_func(n, x_prev, 0); % Assume initial x(0) = 0
iteration = 1;
while iteration <= max_iterations
% Step 4: Calculate x(i+1) using g(i)
x_new = x_func(n, x_prev, g_prev);
% Step 5: Calculate g(i+1) using x(i+1)
g_new = g_func(n, x_new, g_prev);
% Check for convergence
if abs(x_new - x_prev) < tol && abs(g_new - g_prev) < tol
break;
end
% Update previous values with new values
x_prev = x_new;
g_prev = g_new;
iteration = iteration + 1;
end
% Calculate total profit for this value of n
profit_n = profit_func(n, x_new, g_new);
% Compare profit with current optimal profit
if profit_n > optimal_profit
optimal_profit = profit_n;
optimal_n = n;
optimal_x = x_new;
optimal_g = g_new;
% Move to the next value of n
n = n + 1;
else
% If profit does not increase, stop the algorithm
break;
end
end
% Output optimal results
fprintf('Optimal n: %d\n', optimal_n);
fprintf('Optimal x: %f\n', optimal_x);
fprintf('Optimal g: %f\n', optimal_g);
fprintf('Optimal Profit: %f\n', optimal_profit);
end
But right now, when i try to run my code I encounter this error :
Error using profit_optimize>@(n,g)(xa(n,g)/xn(n,g))^(1/3)
Too many input arguments.
Error in profit_optimize (line 118)
x_prev = x_func(n, 0, 0); % Assume initial g(0) = 0
I already try to find solution from similar question that have been asked here, but I'm still not sure how to fix this. Do you haveany idea or suggestion what I should do to solve this issue?
댓글 수: 1
Steven Lord
2024년 7월 10일
As a stylistic comment, that's a lot of anonymous functions using variables whose names aren't that descriptive. Unless a, b, c, etc. are "terms of art" (those names have some sort of special meaning in your domain) I would recommend:
1) Writing your objective function as a local function in this file, not a collection of anonymous functions that are then combined. Alternately you could write your objective function as the main function in its own file and leave the implementation of the optimization algorithm in this file.
2) Using more descriptive variable names (profit instead of a or resistance instead of d for example.)
3) Adding comments explaining why the code does what it's doing. People often put comments explaining what it's doing, and that can be useful if what it's doing isn't obvious or if there are nuances (like if you computed (x-y).*(x+y) instead of x.^2-y.^2 to avoid catastrophic cancellation.) But I'd avoid an obvious comment (like "% Move to the next value of n") unless you're using that comment to document the algorithm you're implementing and that's one of the steps in the description of the algorithm. [Starting off your code by writing the steps of the algorithm you're implementing as comments and then filling in the implementation of each step below the corresponding comment is IMO a reasonable approach.]
Doing the first will aid when (not if) you need to debug your code. You can put breakpoints on individual lines of your objective function and examine the values of the variables. It may also allow you to avoid recomputing the same quantity multiple times; a quick scan of your code shows that L*(1+(g*y)) appears (in various forms) in a lot of your anonymous functions. Define a variable (with a well-chosen name, as per the second suggestion) to store that variable and just reuse it wherever you need it. You might even find that some of the complexity in your expressions cancels out leaving you with simpler expressions.
Doing the second and third will aid in readability of your code. Six months or six weeks from now (or six days if you're unlucky and/or extremely busy!), when you revisit the code will you remember what "b = 0.2;" represents?
답변 (1개)
Torsten
2024년 7월 10일
편집: Torsten
2024년 7월 10일
Your function xa and xn have three inputs (n,x,g), not two (n,g). Thus
x_func = @(n, g) (xa(n, g)/xn(n, g))^(1/3); % Example T function
is wrong.
You'll have a similar problem here:
gdda =@(n, x, g) (ga/gcc)-(gb/gaa)+(gc/gbb)+(gd/(f*ww*gbb))+(ge/gee)+(gf/gff);
glla =@(n, x, g) (gh/ghh)-((gj+gk)/gjj)+(gl/(R*x*gkk))-(gm/(R*x*gmm))+(gn/gll);
g_func =@(n, x, g) gdda/glla; % Example k function
All function handles (gh,ghh,gj,...) must be used with there full list of inputs, thus gh(n,x,g) instead of gh, ghh(n,x,g) instead of ghh and so on.
There might be other places in your code with similar errors.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!